feat: handling SSR on home page
This commit is contained in:
85
lib/server-auth.ts
Normal file
85
lib/server-auth.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { cookies } from "next/headers";
|
||||
import { COOKIE_NAME } from "./auth-utils";
|
||||
import { EvaluationService } from "@/services/evaluation-service";
|
||||
import { TeamsService } from "@/services/teams-service";
|
||||
import { SkillsService } from "@/services/skills-service";
|
||||
import { SkillCategory, Team } from "./types";
|
||||
|
||||
/**
|
||||
* Récupère l'ID utilisateur depuis le cookie côté serveur
|
||||
*/
|
||||
export async function getUserIdFromCookie(): Promise<number | null> {
|
||||
const cookieStore = await cookies();
|
||||
const userIdCookie = cookieStore.get("peakSkills_userId");
|
||||
|
||||
if (!userIdCookie?.value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const userId = parseInt(userIdCookie.value);
|
||||
return isNaN(userId) ? null : userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère l'évaluation complète de l'utilisateur côté serveur
|
||||
*/
|
||||
export async function getServerUserEvaluation() {
|
||||
const userId = await getUserIdFromCookie();
|
||||
|
||||
if (!userId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const evaluationService = new EvaluationService();
|
||||
|
||||
// Récupérer d'abord le profil utilisateur
|
||||
const userProfile = await evaluationService.getUserById(userId);
|
||||
|
||||
if (!userProfile) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Puis charger l'évaluation avec le profil
|
||||
const userEvaluation = await evaluationService.loadUserEvaluation(
|
||||
userProfile
|
||||
);
|
||||
|
||||
return userEvaluation;
|
||||
} catch (error) {
|
||||
console.error("Failed to get user evaluation:", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Charge les catégories de compétences côté serveur depuis PostgreSQL
|
||||
*/
|
||||
export async function getServerSkillCategories(): Promise<SkillCategory[]> {
|
||||
try {
|
||||
return await SkillsService.getSkillCategories();
|
||||
} catch (error) {
|
||||
console.error("Failed to load skill categories:", error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Charge les équipes côté serveur depuis PostgreSQL
|
||||
*/
|
||||
export async function getServerTeams(): Promise<Team[]> {
|
||||
try {
|
||||
return await TeamsService.getTeams();
|
||||
} catch (error) {
|
||||
console.error("Failed to load teams:", error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Vérifie simplement si l'utilisateur est authentifié via le cookie
|
||||
*/
|
||||
export async function isUserAuthenticated(): Promise<boolean> {
|
||||
const userId = await getUserIdFromCookie();
|
||||
return !!userId;
|
||||
}
|
||||
Reference in New Issue
Block a user