refresh token
This commit is contained in:
@@ -57,7 +57,9 @@ export class TokensService {
|
|||||||
const accessToken = await this.jwtService.signAsync(tokenPayload, { expiresIn: `${accessExpire}m` });
|
const accessToken = await this.jwtService.signAsync(tokenPayload, { expiresIn: `${accessExpire}m` });
|
||||||
const refreshToken = this.generateRefreshToken();
|
const refreshToken = this.generateRefreshToken();
|
||||||
|
|
||||||
await this.storeRefreshToken(payload.userId, payload.restId, refreshToken, em);
|
// Skip validation when called from refresh token flow (when em is provided and in transaction)
|
||||||
|
const skipValidation = !!em && em.isInTransaction();
|
||||||
|
await this.storeRefreshToken(payload.userId, payload.restId, refreshToken, em, skipValidation);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
accessToken: { token: accessToken, expire: dayjs().add(accessExpire, 'minute').valueOf() },
|
accessToken: { token: accessToken, expire: dayjs().add(accessExpire, 'minute').valueOf() },
|
||||||
@@ -76,14 +78,22 @@ export class TokensService {
|
|||||||
const accessToken = await this.jwtService.signAsync(tokenPayload, { expiresIn: `${accessExpire}m` });
|
const accessToken = await this.jwtService.signAsync(tokenPayload, { expiresIn: `${accessExpire}m` });
|
||||||
const refreshToken = this.generateRefreshToken();
|
const refreshToken = this.generateRefreshToken();
|
||||||
|
|
||||||
await this.storeRefreshTokenAdmin(payload.adminId, payload.restId, refreshToken, em);
|
// Skip validation when called from refresh token flow (when em is provided and in transaction)
|
||||||
|
const skipValidation = !!em && em.isInTransaction();
|
||||||
|
await this.storeRefreshTokenAdmin(payload.adminId, payload.restId, refreshToken, em, skipValidation);
|
||||||
return {
|
return {
|
||||||
accessToken: { token: accessToken, expire: dayjs().add(accessExpire, 'minute').valueOf() },
|
accessToken: { token: accessToken, expire: dayjs().add(accessExpire, 'minute').valueOf() },
|
||||||
refreshToken: { token: refreshToken, expire: dayjs().add(refreshExpire, 'day').valueOf() },
|
refreshToken: { token: refreshToken, expire: dayjs().add(refreshExpire, 'day').valueOf() },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async storeRefreshToken(userId: string, restId: string, refreshToken: string, em?: EntityManager) {
|
async storeRefreshToken(
|
||||||
|
userId: string,
|
||||||
|
restId: string,
|
||||||
|
refreshToken: string,
|
||||||
|
em?: EntityManager,
|
||||||
|
skipValidation = false,
|
||||||
|
) {
|
||||||
const entityManager = em || this.em.fork();
|
const entityManager = em || this.em.fork();
|
||||||
const isExternalTransaction = !!em && em.isInTransaction();
|
const isExternalTransaction = !!em && em.isInTransaction();
|
||||||
|
|
||||||
@@ -96,8 +106,11 @@ export class TokensService {
|
|||||||
const refreshExpire = this.configService.getOrThrow<number>('REFRESH_TOKEN_EXPIRE');
|
const refreshExpire = this.configService.getOrThrow<number>('REFRESH_TOKEN_EXPIRE');
|
||||||
const expiresAt = dayjs().add(refreshExpire, 'day').toDate();
|
const expiresAt = dayjs().add(refreshExpire, 'day').toDate();
|
||||||
|
|
||||||
const user = await entityManager.findOne(User, { id: userId });
|
// Skip User validation when refreshing (user is already validated via refresh token)
|
||||||
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
|
if (!skipValidation) {
|
||||||
|
const user = await entityManager.findOne(User, { id: userId });
|
||||||
|
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
const restaurant = await entityManager.findOne(Restaurant, { id: restId });
|
const restaurant = await entityManager.findOne(Restaurant, { id: restId });
|
||||||
if (!restaurant) throw new BadRequestException(UserMessage.Rest_NOT_FOUND);
|
if (!restaurant) throw new BadRequestException(UserMessage.Rest_NOT_FOUND);
|
||||||
@@ -128,7 +141,13 @@ export class TokensService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async storeRefreshTokenAdmin(adminId: string, restId: string, refreshToken: string, em?: EntityManager) {
|
async storeRefreshTokenAdmin(
|
||||||
|
adminId: string,
|
||||||
|
restId: string,
|
||||||
|
refreshToken: string,
|
||||||
|
em?: EntityManager,
|
||||||
|
skipValidation = false,
|
||||||
|
) {
|
||||||
const entityManager = em || this.em.fork();
|
const entityManager = em || this.em.fork();
|
||||||
const isExternalTransaction = !!em && em.isInTransaction();
|
const isExternalTransaction = !!em && em.isInTransaction();
|
||||||
|
|
||||||
@@ -141,8 +160,11 @@ export class TokensService {
|
|||||||
const refreshExpire = this.configService.getOrThrow<number>('REFRESH_TOKEN_EXPIRE');
|
const refreshExpire = this.configService.getOrThrow<number>('REFRESH_TOKEN_EXPIRE');
|
||||||
const expiresAt = dayjs().add(refreshExpire, 'day').toDate();
|
const expiresAt = dayjs().add(refreshExpire, 'day').toDate();
|
||||||
|
|
||||||
const admin = await entityManager.findOne(Admin, { id: adminId });
|
// Skip Admin validation when refreshing (admin is already validated via refresh token)
|
||||||
if (!admin) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
|
if (!skipValidation) {
|
||||||
|
const admin = await entityManager.findOne(Admin, { id: adminId });
|
||||||
|
if (!admin) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
const restaurant = await entityManager.findOne(Restaurant, { id: restId });
|
const restaurant = await entityManager.findOne(Restaurant, { id: restId });
|
||||||
if (!restaurant) throw new BadRequestException(UserMessage.Rest_NOT_FOUND);
|
if (!restaurant) throw new BadRequestException(UserMessage.Rest_NOT_FOUND);
|
||||||
|
|||||||
Reference in New Issue
Block a user