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 { LoginDto } from '../user/dto/login.dto';
|
||||||
import { AuthService } from './auth.service';
|
import { AuthService } from './auth.service';
|
||||||
import { Public } from 'src/common/decorators/public.decorator';
|
import { Public } from 'src/common/decorators/public.decorator';
|
||||||
import { RegisterDto } from '../user/dto/register.dto';
|
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 { 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')
|
@ApiTags('Authentification')
|
||||||
@Controller('auth')
|
@Controller('auth')
|
||||||
export class AuthController {
|
export class AuthController {
|
||||||
constructor(private readonly authService: AuthService) { }
|
constructor(
|
||||||
|
private readonly authService: AuthService,
|
||||||
|
private readonly userService: UserService,
|
||||||
|
) { }
|
||||||
|
|
||||||
|
|
||||||
@Public()
|
@Public()
|
||||||
@ApiOperation({ summary: 'Connexion' })
|
@ApiOperation({ summary: 'Connexion' })
|
||||||
@ -32,11 +39,25 @@ export class AuthController {
|
|||||||
return this.authService.refreshTokens(refreshToken);
|
return this.authService.refreshTokens(refreshToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Get('me')
|
@Get('me')
|
||||||
// @UseGuards(AuthGuard)
|
@UseGuards(AuthGuard)
|
||||||
// @ApiBearerAuth('access-token')
|
@ApiBearerAuth('access-token')
|
||||||
// @ApiOperation({ summary: "Recuperer le profil de l'utilisateur connecte"})
|
@ApiOperation({ summary: "Récupérer le profil complet de l'utilisateur connecté" })
|
||||||
// getProfile(@Request() req) {
|
@ApiResponse({ status: 200, type: ProfileResponseDto })
|
||||||
// return req.user;
|
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
|
* Génère un access_token et un refresh_token
|
||||||
*/
|
*/
|
||||||
async generateTokens(userId: string, email: string, role: RoleType) {
|
async generateTokens(userId: string, email: string, role: RoleType) {
|
||||||
const secret = this.configService.get<string>('jwt.secret');
|
const accessSecret = this.configService.get<string>('jwt.accessSecret');
|
||||||
const expiresIn = this.configService.get<string>('jwt.expiresIn');
|
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([
|
const [accessToken, refreshToken] = await Promise.all([
|
||||||
this.jwtService.signAsync({ sub: userId, email, role }, { secret, expiresIn }),
|
this.jwtService.signAsync({ sub: userId, email, role }, { secret: accessSecret, expiresIn: accessExpiresIn }),
|
||||||
this.jwtService.signAsync({ sub: userId }, { secret, expiresIn }),
|
this.jwtService.signAsync({ sub: userId }, { secret: refreshSecret, expiresIn: refreshExpiresIn }),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -70,7 +72,7 @@ export class AuthService {
|
|||||||
async refreshTokens(refreshToken: string) {
|
async refreshTokens(refreshToken: string) {
|
||||||
try {
|
try {
|
||||||
const payload = await this.jwtService.verifyAsync(refreshToken, {
|
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);
|
const user = await this.usersService.findOne(payload.sub);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user