Compare commits
No commits in common. "1dc1bc4aa3adf82a743b94bc2daeac96e8d48d79" and "5387d671629debd869671ce43898f23a7bed7ccb" have entirely different histories.
1dc1bc4aa3
...
5387d67162
@ -1,29 +0,0 @@
|
|||||||
import { CanActivate, ExecutionContext, Injectable, UnauthorizedException } from "@nestjs/common";
|
|
||||||
import { JwtService } from "@nestjs/jwt";
|
|
||||||
import { Request } from 'express';
|
|
||||||
|
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class AuthGuard implements CanActivate {
|
|
||||||
constructor(private readonly jwtService: JwtService) {}
|
|
||||||
|
|
||||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
||||||
const request = context.switchToHttp().getRequest<Request>();
|
|
||||||
const authHeader = request.headers['authorization'] as string | undefined;
|
|
||||||
|
|
||||||
if (!authHeader || !authHeader.startsWith('Bearer')) {
|
|
||||||
throw new UnauthorizedException('Token manquant ou invalide');
|
|
||||||
}
|
|
||||||
|
|
||||||
const token = authHeader.split(' ')[1];
|
|
||||||
try {
|
|
||||||
const payload = await this.jwtService.verifyAsync(token,
|
|
||||||
{ secret: process.env.JWT_SECRET },
|
|
||||||
);
|
|
||||||
request.user = payload;
|
|
||||||
return true;
|
|
||||||
} catch (error) {
|
|
||||||
throw new UnauthorizedException('Token invalide ou expire');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
11
src/main.ts
11
src/main.ts
@ -1,11 +1,8 @@
|
|||||||
import { NestFactory, Reflector } from '@nestjs/core';
|
import { NestFactory } from '@nestjs/core';
|
||||||
import { AppModule } from './app.module';
|
import { AppModule } from './app.module';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { SwaggerModule } from '@nestjs/swagger/dist/swagger-module';
|
import { SwaggerModule } from '@nestjs/swagger/dist/swagger-module';
|
||||||
import { DocumentBuilder } from '@nestjs/swagger';
|
import { DocumentBuilder } from '@nestjs/swagger';
|
||||||
import { AuthGuard } from './common/guards/auth.guard';
|
|
||||||
import { JwtService } from '@nestjs/jwt';
|
|
||||||
import { RolesGuard } from './common/guards/roles.guard';
|
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const app = await NestFactory.create(AppModule);
|
const app = await NestFactory.create(AppModule);
|
||||||
@ -23,12 +20,6 @@ async function bootstrap() {
|
|||||||
const document = SwaggerModule.createDocument(app, config);
|
const document = SwaggerModule.createDocument(app, config);
|
||||||
SwaggerModule.setup('api/docs', app, document);
|
SwaggerModule.setup('api/docs', app, document);
|
||||||
|
|
||||||
const reflector = app.get(Reflector);
|
|
||||||
app.useGlobalGuards(
|
|
||||||
new AuthGuard(app.get(JwtService)),
|
|
||||||
new RolesGuard(reflector)
|
|
||||||
);
|
|
||||||
|
|
||||||
await app.listen(port);
|
await app.listen(port);
|
||||||
console.log(`✅ P'titsPas API is running on: ${await app.getUrl()}`);
|
console.log(`✅ P'titsPas API is running on: ${await app.getUrl()}`);
|
||||||
}
|
}
|
||||||
|
|||||||
7
src/types/express/index.d.ts
vendored
7
src/types/express/index.d.ts
vendored
@ -1,7 +0,0 @@
|
|||||||
import { Users } from 'src/entities/users.entity';
|
|
||||||
|
|
||||||
declare module 'express-serve-static-core' {
|
|
||||||
interface Request {
|
|
||||||
user?: Users | any;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user