public async Task <string> Login(string Email, string Password) { var user = await _usersRepository.GetUser(Email); if (user != null) { var isValid = _verifyPasswords.IsPasswordValid(Password, user.Salt, user.Password); if (isValid) { return(_generateJwts.GetJwtString(user._id.ToString(), user.Email)); } } throw new LoginFailedException(); }
public async Task <IActionResult> Register([FromBody] UserRegistrationRequest user) { if (!ModelState.IsValid) { return(new BadRequestResult()); } try { var userId = await _registerUserService.RegisterUser(user.Email, user.Password); var jwt = _jwtGenerator.GetJwtString(userId.ToString(), user.Email); return(Ok(new { authToken = jwt })); } catch (DuplicateUserException) { return(new BadRequestObjectResult(new { message = "User " + user.Email + " already exists." })); } catch (Exception) { return(new StatusCodeResult(500)); } }