public async Task <ReasonResult <UserModel> > RegisterAsync(RegisterInputModel inputModel) { var newUser = new ApplicationUser() { UserName = inputModel.Email, Email = inputModel.Email, Alias = inputModel.Alias, CreatedDate = DateTime.UtcNow, }; var identityResult = await _userManager.CreateAsync(newUser, inputModel.Password); _logger.LogInformation("User Register, {@details}", new { Id = newUser.Id, UserName = newUser.UserName, }); if (identityResult.Succeeded) { var output = new UserModel(newUser); return(ReasonResult <UserModel> .Success(output)); } else { return(ReasonResult <UserModel> .BadRequest(identityResult.Errors.Select(x => new Reason(x.Code, x.Description)))); } }
public async Task <ReasonResult <AuthResponse> > LoginWithPasswordAsync(LoginPasswordModel inputModel) { var user = await _userManager.FindByEmailAsync(inputModel.Email); if (user != null) { var signInResult = await _signInManager.CheckPasswordSignInAsync(user, inputModel.Password, lockoutOnFailure : true); if (signInResult.Succeeded) { await _userManager.RemoveAuthenticationTokenAsync(user, AuthSettings.RefreshTokenProvider, AuthSettings.RefreshTokenName); string newRefreshToken = await _userManager.GenerateUserTokenAsync(user, AuthSettings.RefreshTokenProvider, AuthSettings.RefreshTokenName); await _userManager.SetAuthenticationTokenAsync(user, AuthSettings.RefreshTokenProvider, AuthSettings.RefreshTokenName, newRefreshToken); string accessToken = _jwtGenerator.GenerateToken(user, out var expires); var output = new AuthResponse(accessToken, newRefreshToken, expires); return(ReasonResult <AuthResponse> .Success(output)); } else { var reasons = new List <Reason>(); if (signInResult.IsLockedOut) { reasons.Add(new Reason("LockedOut", "User is locked out")); } if (signInResult.IsNotAllowed) { reasons.Add(new Reason("IsNotAllowed", "User is not allowed to sign in")); } if (!signInResult.IsLockedOut && !signInResult.IsNotAllowed && !signInResult.RequiresTwoFactor) { reasons.Add(new Reason("InvalidPassword", "Password does not match")); } return(ReasonResult <AuthResponse> .BadRequest(reasons)); } } return(ReasonResult <AuthResponse> .BadRequest(new Reason[] { new Reason("UserNotFound", "Cannot find User") })); }
public async Task <ReasonResult <AuthResponse> > LoginWithRefreshTokenAsync(LoginRefreshTokenModel inputModel) { var query = ( from userToken in _dbContext.UserTokens join applicationUser in _dbContext.ApplicationUsers on userToken.UserId equals applicationUser.Id where userToken.LoginProvider == AuthSettings.RefreshTokenProvider && userToken.Name == AuthSettings.RefreshTokenName && userToken.Value == inputModel.RefreshToken select new { ApplicationUser = applicationUser, Token = userToken } ); var data = await query.FirstOrDefaultAsync(); if (data == null) { return(ReasonResult <AuthResponse> .BadRequest(new Reason[] { new Reason("RefreshTokenNotFound", "Cannot find Refresh Token") })); } var user = data.ApplicationUser; string refreshToken = data.Token.Value; bool verify = await _userManager.VerifyUserTokenAsync(user, AuthSettings.RefreshTokenProvider, AuthSettings.RefreshTokenName, refreshToken); if (!verify) { return(ReasonResult <AuthResponse> .BadRequest(new Reason[] { new Reason("RefreshTokenInvalid", "Refresh Token is invalid") })); } string accessToken = _jwtGenerator.GenerateToken(user, out var expires); var output = new AuthResponse(accessToken, refreshToken, expires); return(ReasonResult <AuthResponse> .Success(output)); }
public async Task <ReasonResult <GameModel> > TakeTurnAsync(int id, int turnX, int turnY, ClaimsUser user) { var game = await _dbContext.Games .FirstOrDefaultAsync(x => x.Id == id); if (game == null) { return(ReasonResult <GameModel> .NotFound()); } BoardPlayer player; if (game.PlayerOne == user.Id) { player = BoardPlayer.PlayerOne; } else if (game.PlayerTwo == user.Id) { player = BoardPlayer.PlayerTwo; } else { return(ReasonResult <GameModel> .NotFound()); } if (game.GameState != GameState.InProgress) { _logger.LogInformation("Player tried to take turn on game with incorrect state, {@details}", new { GameId = game.Id, GameState = game.GameState, UserId = user.Id, }); return(ReasonResult <GameModel> .BadRequest(new[] { GameInWrongState })); } var oldDecompressedBoardState = new DecompressedBoardState(game.BoardHistory); var takeTurnResult = oldDecompressedBoardState.TryTakeTurn(turnX, turnY, player, out var newDecompressedBoardState); switch (takeTurnResult) { case TakeTurnResult.Success: { _logger.LogInformation("Player took turn, {@details}", new { GameId = game.Id, UserId = user.Id, GameState = game.GameState, X = turnX, Y = turnY, OldBoardHistory = oldDecompressedBoardState.BoardHistory, NewBoardHistory = newDecompressedBoardState.BoardHistory, Snapshot = newDecompressedBoardState.ToString(), }); UpdateGameUsingDecompressedBoardState(game, newDecompressedBoardState); await _dbContext.SaveChangesAsync(); return(ReasonResult <GameModel> .Success(new GameModel(game))); } case TakeTurnResult.NotInProgress: return(ReasonResult <GameModel> .BadRequest(new[] { GameInWrongState })); case TakeTurnResult.TileAlreadySet: return(ReasonResult <GameModel> .BadRequest(new[] { TileAlreadySet })); case TakeTurnResult.WrongPlayer: return(ReasonResult <GameModel> .BadRequest(new[] { ActingOnWrongTurn })); default: throw new Exception($"Unknown failure state reached during {nameof(DecompressedBoardState)}.{nameof(DecompressedBoardState.TryTakeTurn)}"); } }