Exemple #1
0
        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();
        }
Exemple #2
0
        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));
            }
        }