RefreshTokenRepository.cs
1,507 bytes
| 1 | using App.Domain.Contracts; |
|---|---|
| 2 | using App.Domain.Identity; |
| 3 | using Microsoft.EntityFrameworkCore; |
| 4 | |
| 5 | namespace App.DAL.EF.Repositories; |
| 6 | |
| 7 | public class RefreshTokenRepository : BaseRepository<AppRefreshToken>, IRefreshTokenRepository |
| 8 | { |
| 9 | public RefreshTokenRepository(AppDbContext dbContext) : base(dbContext) |
| 10 | { |
| 11 | } |
| 12 | |
| 13 | public async Task<IEnumerable<AppRefreshToken>> GetUserActiveTokensAsync(Guid userId, string refreshTokenValue) |
| 14 | { |
| 15 | var now = DateTime.UtcNow; |
| 16 | return await DbContext.RefreshTokens |
| 17 | .Where(t => t.AppUserId == userId && |
| 18 | ((t.RefreshToken == refreshTokenValue && t.ExpirationDT > now) || |
| 19 | (t.PreviousRefreshToken == refreshTokenValue && t.PreviousExpirationDT > now))) |
| 20 | .ToListAsync(); |
| 21 | } |
| 22 | |
| 23 | public async Task<IEnumerable<AppRefreshToken>> GetUserTokensByValueAsync(Guid userId, string refreshTokenValue) |
| 24 | { |
| 25 | return await DbContext.RefreshTokens |
| 26 | .Where(t => t.AppUserId == userId && |
| 27 | (t.RefreshToken == refreshTokenValue || t.PreviousRefreshToken == refreshTokenValue)) |
| 28 | .ToListAsync(); |
| 29 | } |
| 30 | |
| 31 | public async Task<int> RemoveExpiredForUserAsync(Guid userId) |
| 32 | { |
| 33 | return await DbContext.RefreshTokens |
| 34 | .Where(t => t.AppUserId == userId && t.ExpirationDT < DateTime.UtcNow) |
| 35 | .ExecuteDeleteAsync(); |
| 36 | } |
| 37 | |
| 38 | public void Remove(AppRefreshToken token) |
| 39 | { |
| 40 | DbContext.RefreshTokens.Remove(token); |
| 41 | } |
| 42 | } |
| 43 | |