diff --git a/apps/ebitemp-api/src/app/modules/auth/constants/token-payload.interface.ts b/apps/ebitemp-api/src/app/modules/auth/constants/token-payload.interface.ts index e12527d..d250bb5 100644 --- a/apps/ebitemp-api/src/app/modules/auth/constants/token-payload.interface.ts +++ b/apps/ebitemp-api/src/app/modules/auth/constants/token-payload.interface.ts @@ -1,5 +1,5 @@ export interface TokenPayload { - userId: number; + sub: number; iat?: number; exp?: number; } diff --git a/apps/ebitemp-api/src/app/modules/auth/strategies/jwt/jwt-access-token-auth.strategy.ts b/apps/ebitemp-api/src/app/modules/auth/strategies/jwt/jwt-access-token-auth.strategy.ts index 8032285..c3c0930 100644 --- a/apps/ebitemp-api/src/app/modules/auth/strategies/jwt/jwt-access-token-auth.strategy.ts +++ b/apps/ebitemp-api/src/app/modules/auth/strategies/jwt/jwt-access-token-auth.strategy.ts @@ -2,9 +2,9 @@ import { Inject, Injectable, UnauthorizedException } from '@nestjs/common'; import { PassportStrategy } from '@nestjs/passport'; import { ExtractJwt, Strategy } from 'passport-jwt'; import { AuthConfig, authConfig } from '../../auth.config'; -import { RequestWithUser } from '../../constants/request-with-user'; import { TokenPayload } from '../../constants/token-payload.interface'; import { UsersAuthService } from '../../users/users-auth.service'; +import { FastifyRequest } from 'fastify'; @Injectable() export class JwtAccessTokenAuthStrategy extends PassportStrategy(Strategy, 'jwt-access-token') { @@ -16,8 +16,8 @@ export class JwtAccessTokenAuthStrategy extends PassportStrategy(Strategy, 'jwt- }); } - async validate(request: RequestWithUser, payload: TokenPayload) { - const account = await this.userAuthService.getUserById(payload.userId); + async validate(request: FastifyRequest, payload: TokenPayload) { + const account = await this.userAuthService.getUserById(payload.sub); if (!account) throw new UnauthorizedException('Access Token Guard'); return account; } diff --git a/apps/ebitemp-api/src/app/modules/auth/strategies/jwt/jwt-refresh-token-auth.strategy.ts b/apps/ebitemp-api/src/app/modules/auth/strategies/jwt/jwt-refresh-token-auth.strategy.ts index 7d41f63..0d40633 100644 --- a/apps/ebitemp-api/src/app/modules/auth/strategies/jwt/jwt-refresh-token-auth.strategy.ts +++ b/apps/ebitemp-api/src/app/modules/auth/strategies/jwt/jwt-refresh-token-auth.strategy.ts @@ -3,7 +3,6 @@ import { PassportStrategy } from '@nestjs/passport'; import { FastifyRequest } from 'fastify'; import { ExtractJwt, Strategy } from 'passport-jwt'; import { authConfig, AuthConfig } from '../../auth.config'; -import { RequestWithUser } from '../../constants/request-with-user'; import { TokenPayload } from '../../constants/token-payload.interface'; import { UsersAuthService } from '../../users/users-auth.service'; @@ -11,19 +10,15 @@ import { UsersAuthService } from '../../users/users-auth.service'; export class JwtRefreshTokenAuthStrategy extends PassportStrategy(Strategy, 'jwt-refresh-token') { constructor(@Inject(authConfig.KEY) authConfig: AuthConfig, private readonly usersAuthService: UsersAuthService) { super({ - jwtFromRequest: ExtractJwt.fromExtractors([ - (request: FastifyRequest) => { - return request?.headers?.Bearer as string; - }, - ]), + jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), secretOrKey: authConfig.refreshToken.secret, passReqToCallback: true, }); } - async validate(request: RequestWithUser, payload: TokenPayload) { - const refreshToken = request.headers?.Refresh as string; - const account = this.usersAuthService.getUserByIdAndRefreshTokenPair(payload.userId, refreshToken); + async validate(request: FastifyRequest, payload: TokenPayload) { + const refreshToken = (request.headers?.authorization as string| undefined)?.replace('Bearer', '')?.trim(); + const account = this.usersAuthService.getUserByIdAndRefreshTokenPair(payload.sub, refreshToken); if (!account) throw new UnauthorizedException('Refresh Token Guard'); return account; } diff --git a/apps/ebitemp-api/src/app/modules/database/entities/accounts.entity.ts b/apps/ebitemp-api/src/app/modules/database/entities/accounts.entity.ts index 9ae2237..194c7e4 100644 --- a/apps/ebitemp-api/src/app/modules/database/entities/accounts.entity.ts +++ b/apps/ebitemp-api/src/app/modules/database/entities/accounts.entity.ts @@ -22,9 +22,6 @@ export class AccountsEntity { @Column('date', { name: 'data_scadenza', nullable: true }) dataScadenza: Date | null; - @Column('bit', { name: 'flag_primo_accesso', default: () => '(1)' }) - flagPrimoAccesso: boolean; - @Column('varchar', { name: 'ultimo_hash_refresh_token', nullable: true,