chore(repo): fix jwt token strategies + fix AccountsEntity
This commit is contained in:
parent
0148082d0a
commit
d6ffa909aa
@ -1,5 +1,5 @@
|
||||
export interface TokenPayload {
|
||||
userId: number;
|
||||
sub: number;
|
||||
iat?: number;
|
||||
exp?: number;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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,
|
||||
|
Reference in New Issue
Block a user