public async Task <ActionResult> RegisterUserAsync([FromBody] UserRegiesterRequestModel user) { var createdUser = await _userService.CreateUser(user); _logger.LogInformation("User Registered", createdUser.Id); return(Ok(createdUser)); }
public async Task <ActionResult> LoginAsync([FromBody] UserRegiesterRequestModel loginRequest) { var user = await _userService.ValidateUser(loginRequest.Email, loginRequest.Password); if (user == null) { return(Unauthorized("Please enter correct user email and password")); } //once un/pw is authenticated the ngenerate token (JWT) //var generatedToken = GenerateJWT(user); return(Ok(new { token = GenerateJWT(user) })); }
public async Task <UserRegisterResponseModel> CreateUser(UserRegiesterRequestModel requestModel) { //1. call GetUserByEmail with requestModel.Email to check if the email exists in the User Table or not //if user exists return Email already exists and throw an Conflict exceotion //if email does not exists then we can proceed in creating the User record //1. Generate a random salt //2. var hashedPassword = We take requestModel.Password and add Salt from above step and Hash them to generate Unique Hash //3. Save Email, Salt, hashedPassword along with other details that user sent like FirstName, LastName etc //4. return the /userRegisterResponseModel object with newly created Id for the User var dbUser = await _userRepository.GetUserByEmail(requestModel.Email); if (dbUser != null) { throw new Exception("Email already exists"); } var salt = _cryptoService.CreateSalt(); var hashedPassword = _cryptoService.HashPassword(requestModel.Password, salt); var user = new User { Email = requestModel.Email, Salt = salt, HashedPassword = hashedPassword, FirstName = requestModel.FirstName, LastName = requestModel.LastName }; var createdUser = await _userRepository.AddAsync(user); var response = new UserRegisterResponseModel { Id = createdUser.Id, Email = requestModel.Email, FirstName = requestModel.FirstName, LastName = requestModel.LastName }; return(response); }