Пример #1
0
        /// <summary>
        /// Authenticate with user's email and password
        /// </summary>
        public async Task <LoginResponse> AuthenticateAsync(string email, string inputPassword)
        {
            await Task.Delay(0); // just because we dont have a remote(db) call here

            if (!_dummyUsersService.ContainsKey(email))
            {
                return(new LoginResponse <GenericError>
                {
                    Success = false,
                    Data = new GenericError("‍User does not exists ...") // i don't like to give those informations ...
                });
            }

            var user = _dummyUsersService[email]; // get from your user storage

            if (_hasherService.VerifyHashedPassword(user, user.Password, inputPassword) != PasswordVerificationResult.Success)
            {
                return(new LoginResponse <GenericError>
                {
                    Success = false,
                    Data = new GenericError("Invalid credentials 🤷‍")
                });
            }

            var userClaims = new[] {
                new Claim(type: "sub", user.Id.ToString(), ClaimValueTypes.String),
                new Claim(type: ClaimTypes.Name, user.Name, ClaimValueTypes.String),
                new Claim(type: ClaimTypes.Email, user.Email, ClaimValueTypes.Email),
                new Claim(type: ClaimTypes.Role, "user's role"),
            };

            user.Password = null; // Make your own UserDto without password property! This is just a demo project.
            var dto = new LoginResponse <DummyUser>
            {
                Success = true,
                Data    = user,
                Token   = _jwtProviderService.GenerateToken(userClaims),
            };

            return(dto);
        }