forked from Ynov/ptitspas-ynov-back
auth profile added
This commit is contained in:
parent
de725dffd7
commit
99e742f840
@ -1,15 +1,22 @@
|
||||
import { Body, Controller, Post, UseGuards } from '@nestjs/common';
|
||||
import { Body, Controller, Get, Post, Req, UnauthorizedException, UseGuards } from '@nestjs/common';
|
||||
import { LoginDto } from '../user/dto/login.dto';
|
||||
import { AuthService } from './auth.service';
|
||||
import { Public } from 'src/common/decorators/public.decorator';
|
||||
import { RegisterDto } from '../user/dto/register.dto';
|
||||
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
import { AuthGuard } from 'src/common/guards/auth.guard';
|
||||
import type { Request } from 'express';
|
||||
import { UserService } from '../user/user.service';
|
||||
import { ProfileResponseDto } from '../user/dto/profile_response.dto';
|
||||
|
||||
@ApiTags('Authentification')
|
||||
@Controller('auth')
|
||||
export class AuthController {
|
||||
constructor(private readonly authService: AuthService) { }
|
||||
constructor(
|
||||
private readonly authService: AuthService,
|
||||
private readonly userService: UserService,
|
||||
) { }
|
||||
|
||||
|
||||
@Public()
|
||||
@ApiOperation({ summary: 'Connexion' })
|
||||
@ -32,11 +39,25 @@ export class AuthController {
|
||||
return this.authService.refreshTokens(refreshToken);
|
||||
}
|
||||
|
||||
// @Get('me')
|
||||
// @UseGuards(AuthGuard)
|
||||
// @ApiBearerAuth('access-token')
|
||||
// @ApiOperation({ summary: "Recuperer le profil de l'utilisateur connecte"})
|
||||
// getProfile(@Request() req) {
|
||||
// return req.user;
|
||||
// }
|
||||
@Get('me')
|
||||
@UseGuards(AuthGuard)
|
||||
@ApiBearerAuth('access-token')
|
||||
@ApiOperation({ summary: "Récupérer le profil complet de l'utilisateur connecté" })
|
||||
@ApiResponse({ status: 200, type: ProfileResponseDto })
|
||||
async getProfile(@Req() req: Request): Promise<ProfileResponseDto> {
|
||||
if (!req.user || !req.user.sub) {
|
||||
throw new UnauthorizedException('Utilisateur non authentifié');
|
||||
}
|
||||
|
||||
const user = await this.userService.findOne(req.user.sub);
|
||||
return {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
role: user.role,
|
||||
prenom: user.prenom ?? '',
|
||||
nom: user.nom ?? '',
|
||||
statut: user.statut,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -24,12 +24,14 @@ export class AuthService {
|
||||
* Génère un access_token et un refresh_token
|
||||
*/
|
||||
async generateTokens(userId: string, email: string, role: RoleType) {
|
||||
const secret = this.configService.get<string>('jwt.secret');
|
||||
const expiresIn = this.configService.get<string>('jwt.expiresIn');
|
||||
const accessSecret = this.configService.get<string>('jwt.accessSecret');
|
||||
const accessExpiresIn = this.configService.get<string>('jwt.accessExpiresIn');
|
||||
const refreshSecret = this.configService.get<string>('jwt.refreshSecret');
|
||||
const refreshExpiresIn = this.configService.get<string>('jwt.refreshExpiresIn');
|
||||
|
||||
const [accessToken, refreshToken] = await Promise.all([
|
||||
this.jwtService.signAsync({ sub: userId, email, role }, { secret, expiresIn }),
|
||||
this.jwtService.signAsync({ sub: userId }, { secret, expiresIn }),
|
||||
this.jwtService.signAsync({ sub: userId, email, role }, { secret: accessSecret, expiresIn: accessExpiresIn }),
|
||||
this.jwtService.signAsync({ sub: userId }, { secret: refreshSecret, expiresIn: refreshExpiresIn }),
|
||||
]);
|
||||
|
||||
return {
|
||||
@ -70,7 +72,7 @@ export class AuthService {
|
||||
async refreshTokens(refreshToken: string) {
|
||||
try {
|
||||
const payload = await this.jwtService.verifyAsync(refreshToken, {
|
||||
secret: this.configService.get<string>('jwt.refresh_token_secret'),
|
||||
secret: this.configService.get<string>('jwt.refreshSecret'),
|
||||
});
|
||||
|
||||
const user = await this.usersService.findOne(payload.sub);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user