cambios en el refresh token
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
// auth.controllers
|
||||||
import { Public } from '@/common/decorators';
|
import { Public } from '@/common/decorators';
|
||||||
import { JwtRefreshGuard } from '@/common/guards/jwt-refresh.guard';
|
import { JwtRefreshGuard } from '@/common/guards/jwt-refresh.guard';
|
||||||
import { RefreshTokenDto } from '@/features/auth/dto/refresh-token.dto';
|
import { RefreshTokenDto } from '@/features/auth/dto/refresh-token.dto';
|
||||||
@@ -51,7 +52,7 @@ export class AuthController {
|
|||||||
// return { message: 'Password reset link sent to your email' };
|
// return { message: 'Password reset link sent to your email' };
|
||||||
// }
|
// }
|
||||||
|
|
||||||
@UseGuards(JwtRefreshGuard)
|
// @UseGuards(JwtRefreshGuard)
|
||||||
@Public()
|
@Public()
|
||||||
@HttpCode(200)
|
@HttpCode(200)
|
||||||
@Patch('refresh')
|
@Patch('refresh')
|
||||||
@@ -63,7 +64,7 @@ export class AuthController {
|
|||||||
// console.log("refreshTokenDto",refreshTokenDto);
|
// console.log("refreshTokenDto",refreshTokenDto);
|
||||||
// console.log(typeof refreshTokenDto);
|
// console.log(typeof refreshTokenDto);
|
||||||
|
|
||||||
const data = await this.authService.refreshToken(refreshTokenDto,req['user'].sub);
|
const data = await this.authService.refreshToken(refreshTokenDto);
|
||||||
// console.log("data",data);
|
// console.log("data",data);
|
||||||
|
|
||||||
if (!data) {
|
if (!data) {
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// auth.service
|
||||||
import { envs } from '@/common/config/envs';
|
import { envs } from '@/common/config/envs';
|
||||||
import { Env, validateString } from '@/common/utils';
|
import { Env, validateString } from '@/common/utils';
|
||||||
import { DRIZZLE_PROVIDER } from '@/database/drizzle-provider';
|
import { DRIZZLE_PROVIDER } from '@/database/drizzle-provider';
|
||||||
@@ -261,10 +262,16 @@ export class AuthService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Refresh User Access Token
|
//Refresh User Access Token
|
||||||
async refreshToken(dto: RefreshTokenDto,user_id:number): Promise<RefreshTokenInterface> {
|
async refreshToken(dto: RefreshTokenDto): Promise<RefreshTokenInterface> {
|
||||||
// const { user_id } = dto;
|
const { user_id, refresh_token } = dto;
|
||||||
// const user_id = 1;
|
// const user_id = 1;
|
||||||
|
|
||||||
|
const validation = await this.jwtService.verifyAsync(refresh_token, {
|
||||||
|
secret: envs.refresh_token_secret,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!validation) throw new UnauthorizedException('Invalid refresh token');
|
||||||
|
|
||||||
const session = await this.drizzle
|
const session = await this.drizzle
|
||||||
.select()
|
.select()
|
||||||
.from(sessions)
|
.from(sessions)
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// refresh-token
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { IsNumber, IsString } from 'class-validator';
|
import { IsNumber, IsString } from 'class-validator';
|
||||||
|
|
||||||
@@ -8,7 +9,7 @@ export class RefreshTokenDto {
|
|||||||
})
|
})
|
||||||
refresh_token: string;
|
refresh_token: string;
|
||||||
|
|
||||||
// @ApiProperty()
|
@ApiProperty()
|
||||||
// @IsNumber()
|
@IsNumber()
|
||||||
// user_id: number;
|
user_id: number;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
|
// refreshtoken
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { tokensSchema } from './login';
|
import { tokensSchema } from './login';
|
||||||
|
|
||||||
// Esquema para el refresh token
|
// Esquema para el refresh token
|
||||||
export const refreshTokenSchema = z.object({
|
export const refreshTokenSchema = z.object({
|
||||||
|
user_id: z.number(),
|
||||||
token: z.string(),
|
token: z.string(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
//auth.config
|
||||||
import { SignInAction } from '@/feactures/auth/actions/login-action';
|
import { SignInAction } from '@/feactures/auth/actions/login-action';
|
||||||
import { resfreshTokenAction } from '@/feactures/auth/actions/refresh-token-action';
|
import { resfreshTokenAction } from '@/feactures/auth/actions/refresh-token-action';
|
||||||
import { CredentialsSignin, NextAuthConfig, Session, User } from 'next-auth';
|
import { CredentialsSignin, NextAuthConfig, Session, User } from 'next-auth';
|
||||||
@@ -146,7 +147,7 @@ const authConfig: NextAuthConfig = {
|
|||||||
// 4. Si el token de acceso ha expirado pero el refresh token es válido, renovar
|
// 4. Si el token de acceso ha expirado pero el refresh token es válido, renovar
|
||||||
// console.log("Renovando token de acceso...");
|
// console.log("Renovando token de acceso...");
|
||||||
try {
|
try {
|
||||||
const res = await resfreshTokenAction({ token: token.refresh_token as string });
|
const res = await resfreshTokenAction({ token: token.refresh_token as string, user_id: token.user.id as number});
|
||||||
|
|
||||||
if (!res || !res.tokens) {
|
if (!res || !res.tokens) {
|
||||||
throw new Error('Fallo en la respuesta de la API de refresco.');
|
throw new Error('Fallo en la respuesta de la API de refresco.');
|
||||||
@@ -185,6 +186,7 @@ const authConfig: NextAuthConfig = {
|
|||||||
return session;
|
return session;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
} satisfies NextAuthConfig;
|
} satisfies NextAuthConfig;
|
||||||
|
|
||||||
export default authConfig;
|
export default authConfig;
|
||||||
|
|||||||
Reference in New Issue
Block a user