Пример #1
0
        public async Task <RefreshToken> Generate(User user)
        {
            await this._refreshTokenRepository.DeleteAll(user);

            var token = RefreshToken.Create(user);

            await this._refreshTokenRepository.Save(token);

            return(token);
        }
Пример #2
0
        public async Task <IActionResult> Authenticate(AuthenticateUserRequest request)
        {
            var user = await _authService.Authenticate(request.Username, request.Password);

            if (user is null)
            {
                return(Unauthorized());
            }

            var token = _jwtService.GenerateToken(user.Id, user.Username);

            var refreshTokenValue = _refreshTokenService.GenerateRefreshToken();

            var refreshToken = RefreshToken.Create(refreshTokenValue, user.Id);

            _dbContext.RefreshTokens.Add(refreshToken);

            await _dbContext.SaveChangesAsync();

            return(Ok(new { user.Username, token, refreshToken = refreshToken.Token, RefreshTokenExpiresAt = refreshToken.ExpirationDate }));
        }