token
This commit is contained in:
@@ -11,6 +11,7 @@ import { RefreshToken } from '../../users/entities/refresh-token.entity';
|
||||
import { User } from '../../users/entities/user.entity';
|
||||
import { ITokenPayload } from '../interfaces/IToken-payload';
|
||||
import { Restaurant } from 'src/modules/restaurants/entities/restaurant.entity';
|
||||
import { RestRepository } from 'src/modules/restaurants/repositories/rest.repository';
|
||||
|
||||
@Injectable()
|
||||
export class TokensService {
|
||||
@@ -19,6 +20,7 @@ export class TokensService {
|
||||
private readonly configService: ConfigService,
|
||||
private readonly jwtService: JwtService,
|
||||
private readonly em: EntityManager,
|
||||
private readonly restRepository: RestRepository,
|
||||
) {}
|
||||
|
||||
async generateTokens(user: User, rest: Restaurant, em?: EntityManager) {
|
||||
@@ -107,6 +109,9 @@ export class TokensService {
|
||||
|
||||
if (!token) throw new UnauthorizedException(AuthMessage.INVALID_REFRESH_TOKEN);
|
||||
|
||||
const rest = await this.restRepository.findOne({ id: token.user.id });
|
||||
if (!rest) throw new UnauthorizedException(AuthMessage.INVALID_REFRESH_TOKEN);
|
||||
|
||||
if (dayjs(token.expiresAt).isBefore(dayjs())) {
|
||||
entityManager.remove(token);
|
||||
await entityManager.flush();
|
||||
@@ -118,7 +123,7 @@ export class TokensService {
|
||||
entityManager.remove(token);
|
||||
|
||||
// Generate new tokens within the same transaction
|
||||
const tokens = await this.generateTokens(token.user, entityManager);
|
||||
const tokens = await this.generateTokens(token.user, rest, entityManager);
|
||||
|
||||
// Commit the transaction
|
||||
await entityManager.flush();
|
||||
|
||||
Reference in New Issue
Block a user