public async Task <IActionResult> OAuth(int pinId) { var accessToken = await _plexOAuthManager.GetAccessTokenFromPin(pinId); if (accessToken.IsNullOrEmpty()) { // Looks like we are not authenticated. return(new JsonResult(new { errorMessage = "Could not authenticate with Plex" })); } // Let's look for the users account var account = await _plexOAuthManager.GetAccount(accessToken); // Get the ombi user var user = await _userManager.FindByNameAsync(account.user.username); if (user == null) { // Could this be an email login? user = await _userManager.FindByEmailAsync(account.user.email); if (user == null) { return(new UnauthorizedResult()); } } user.MediaServerToken = account.user.authentication_token; await _userManager.UpdateAsync(user); return(await CreateToken(true, user)); }