Example #1
0
        public async Task <AccessRefreshTokensDto> GenerateAccessRefreshTokens(Guid userId, string userName, string email)
        {
            var refreshToken = _jwtFactory.GenerateRefreshToken();

            var oldRefreshTokens = await _dbContext.RefreshTokens
                                   .Where(rt => rt.UserId == userId)
                                   .ToListAsync();

            _dbContext.RemoveRange(oldRefreshTokens);

            _dbContext.RefreshTokens.Add(new RefreshTokenModel
            {
                Token     = refreshToken,
                ExpiresIn = DateTime.UtcNow.AddSeconds(AuthConstants.RefreshTokenLifetimeInSeconds),
                UserId    = userId,
            });

            await _dbContext.SaveChangesAsync();

            var accessToken = await _jwtFactory.GenerateAccessToken(userId, userName, email);

            return(new AccessRefreshTokensDto
            {
                AccessToken = accessToken,
                RefreshToken = refreshToken
            });
        }
Example #2
0
        public async Task <AccessTokenDTO> GenerateAccessToken(int userId, string userName, string email)
        {
            var refreshToken = _jwtFactory.GenerateRefreshToken();

            _context.RefreshTokens.Add(new RefreshToken
            {
                Token  = refreshToken,
                UserId = userId
            });

            await _context.SaveChangesAsync();

            var accessToken = await _jwtFactory.GenerateAccessToken(userId, userName, email);

            return(new AccessTokenDTO(accessToken, refreshToken));
        }