forked from Ynov/ptitspas-ynov-back
corrected auth
This commit is contained in:
parent
bc47dac791
commit
05c1a61090
@ -8,8 +8,9 @@ import { JwtService } from '@nestjs/jwt';
|
|||||||
import * as bcrypt from 'bcrypt';
|
import * as bcrypt from 'bcrypt';
|
||||||
import { RegisterDto } from '../user/dto/register.dto';
|
import { RegisterDto } from '../user/dto/register.dto';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { RoleType, StatutUtilisateurType } from 'src/entities/users.entity';
|
import { RoleType, StatutUtilisateurType, Users } from 'src/entities/users.entity';
|
||||||
import { LoginDto } from '../user/dto/login.dto';
|
import { LoginDto } from '../user/dto/login.dto';
|
||||||
|
import { DeepPartial } from 'typeorm';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthService {
|
export class AuthService {
|
||||||
@ -23,23 +24,12 @@ 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 expiresIn = this.configService.get<string>('jwt.expiresIn');
|
||||||
|
|
||||||
const [accessToken, refreshToken] = await Promise.all([
|
const [accessToken, refreshToken] = await Promise.all([
|
||||||
this.jwtService.signAsync(
|
this.jwtService.signAsync({ sub: userId, email, role }, { secret, expiresIn }),
|
||||||
{ sub: userId, email, role },
|
this.jwtService.signAsync({ sub: userId }, { secret, expiresIn }),
|
||||||
{
|
|
||||||
secret: this.configService.get<string>('jwt.secret'),
|
|
||||||
expiresIn: this.configService.get<string>('jwt.access_token_expires'),
|
|
||||||
},
|
|
||||||
),
|
|
||||||
this.jwtService.signAsync(
|
|
||||||
{ sub: userId },
|
|
||||||
{
|
|
||||||
secret: this.configService.get<string>('jwt.refresh_token_secret'),
|
|
||||||
expiresIn: this.configService.get<string>(
|
|
||||||
'jwt.refresh_token_expires',
|
|
||||||
),
|
|
||||||
},
|
|
||||||
),
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -52,15 +42,23 @@ export class AuthService {
|
|||||||
* Connexion utilisateur
|
* Connexion utilisateur
|
||||||
*/
|
*/
|
||||||
async login(dto: LoginDto) {
|
async login(dto: LoginDto) {
|
||||||
const user = await this.usersService.findByEmail(dto.email);
|
try {
|
||||||
|
const user = await this.usersService.findByEmailOrNull(dto.email);
|
||||||
|
|
||||||
// Vérification du mot de passe
|
if (!user) {
|
||||||
const isMatch = await bcrypt.compare(dto.password, user.password_hash);
|
throw new UnauthorizedException('Identifiants invalides');
|
||||||
if (!isMatch) {
|
}
|
||||||
|
|
||||||
|
const isMatch = await bcrypt.compare(dto.password, user.password_hash);
|
||||||
|
if (!isMatch) {
|
||||||
|
throw new UnauthorizedException('Identifiants invalides');
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.generateTokens(user.id, user.email, user.role);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erreur de connexion :', error);
|
||||||
throw new UnauthorizedException('Identifiants invalides');
|
throw new UnauthorizedException('Identifiants invalides');
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.generateTokens(user.id, user.email, user.role);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -87,31 +85,29 @@ export class AuthService {
|
|||||||
* Inscription utilisateur lambda (parent ou assistante maternelle)
|
* Inscription utilisateur lambda (parent ou assistante maternelle)
|
||||||
*/
|
*/
|
||||||
async register(registerDto: RegisterDto) {
|
async register(registerDto: RegisterDto) {
|
||||||
// Vérifier si l'email existe déjà
|
const exists = await this.usersService.findByEmailOrNull(registerDto.email);
|
||||||
const exists = await this.usersService.findByEmail(registerDto.email).catch(() => null);
|
|
||||||
if (exists) {
|
if (exists) {
|
||||||
throw new ConflictException('Email déjà utilisé');
|
throw new ConflictException('Email déjà utilisé');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filtrer les rôles autorisés
|
|
||||||
const allowedRoles = new Set<RoleType>([RoleType.PARENT, RoleType.ASSISTANTE_MATERNELLE]);
|
const allowedRoles = new Set<RoleType>([RoleType.PARENT, RoleType.ASSISTANTE_MATERNELLE]);
|
||||||
if (!allowedRoles.has(registerDto.role)) {
|
if (!allowedRoles.has(registerDto.role)) {
|
||||||
registerDto.role = RoleType.PARENT;
|
registerDto.role = RoleType.PARENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Statut par défaut
|
|
||||||
registerDto.status = StatutUtilisateurType.EN_ATTENTE;
|
registerDto.status = StatutUtilisateurType.EN_ATTENTE;
|
||||||
|
|
||||||
// Création de l'utilisateur (UserService gère le hash)
|
if (!registerDto.consent_photo) {
|
||||||
let user;
|
registerDto.consent_photo_at = null;
|
||||||
try {
|
} else if (registerDto.consent_photo_at) {
|
||||||
user = await this.usersService.create(registerDto);
|
const date = new Date(registerDto.consent_photo_at);
|
||||||
} catch (error) {
|
if (isNaN(date.getTime())) {
|
||||||
console.error('Erreur pendant la creation du user : ', error);
|
registerDto.consent_photo_at = null;
|
||||||
throw new ConflictException('Impossible de creer le user')
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Génération des tokens
|
const user = await this.usersService.createUser(registerDto);
|
||||||
|
|
||||||
const tokens = await this.generateTokens(user.id, user.email, user.role);
|
const tokens = await this.generateTokens(user.id, user.email, user.role);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -126,4 +122,5 @@ export class AuthService {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user