fix: login was KO by profile

This commit is contained in:
Julien Froidefond
2025-08-25 10:48:13 +02:00
parent 9a818b7205
commit d575596c71
3 changed files with 97 additions and 25 deletions

View File

@@ -1,4 +1,7 @@
import { cookies } from "next/headers";
import { UserProfile } from "@/lib/types";
import { userService } from "@/services/user-service";
// Constantes pour les cookies (définies ici car auth-service.ts a été supprimé)
export const COOKIE_NAME = "peakSkills_userId";
export const COOKIE_MAX_AGE = 30 * 24 * 60 * 60; // 30 jours
@@ -29,4 +32,52 @@ export class AuthService {
const userUuid = await this.getUserUuidFromCookie();
return !!userUuid;
}
/**
* Authentifie un utilisateur et retourne la configuration du cookie
*/
static async authenticateUser(profile: UserProfile): Promise<{
userUuid: string;
cookieConfig: {
name: string;
value: string;
options: {
maxAge: number;
httpOnly: boolean;
secure: boolean;
sameSite: "lax" | "strict" | "none";
path: string;
};
};
}> {
// Vérifier si l'utilisateur existe déjà avec ces informations
const existingUser = await userService.findUserByProfile(profile);
let userUuid: string;
if (existingUser) {
// Mettre à jour l'utilisateur existant si nécessaire
if (existingUser.teamId !== profile.teamId) {
await userService.updateUserByUuid(existingUser.uuid, profile);
}
userUuid = existingUser.uuid;
} else {
// Créer un nouvel utilisateur
userUuid = await userService.upsertUserUuid(profile);
}
return {
userUuid,
cookieConfig: {
name: COOKIE_NAME,
value: userUuid,
options: {
maxAge: COOKIE_MAX_AGE,
httpOnly: true,
secure: process.env.NODE_ENV === "production",
sameSite: "lax",
path: "/",
},
},
};
}
}

View File

@@ -101,6 +101,41 @@ export class UserService {
/**
* Récupère un utilisateur par son UUID
*/
/**
* Trouve un utilisateur par son profil (firstName, lastName)
*/
async findUserByProfile(profile: UserProfile): Promise<{
uuid: string;
teamId: string;
} | null> {
const pool = getPool();
const client = await pool.connect();
try {
const query = `
SELECT uuid_id, team_id
FROM users
WHERE first_name = $1 AND last_name = $2
`;
const result = await client.query(query, [
profile.firstName,
profile.lastName,
]);
if (result.rows.length === 0) {
return null;
}
return {
uuid: result.rows[0].uuid_id,
teamId: result.rows[0].team_id,
};
} finally {
client.release();
}
}
async getUserByUuid(userUuid: string): Promise<UserProfile | null> {
const pool = getPool();
const client = await pool.connect();