Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions front/src/app/auth/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import React, { useState } from 'react';
import Link from 'next/link';
import { useRouter } from 'next/navigation';
import { ApiError, loginUser } from '@/shared/utils/api';
import { useAuth } from '@/shared/hooks/useAuth';
import { useActiveCharacter } from '@/shared/contexts/CharacterContext';
import { useToast } from '@/shared/contexts/ToastContext';
import { Button } from '@/shared/components/ui/Button';
import { Input } from '@/shared/components/ui/Input';
Expand All @@ -12,6 +14,8 @@ import { Mail, Lock, LogIn, AlertCircle } from 'lucide-react';

export default function LoginPage() {
const router = useRouter();
const { refresh } = useAuth();
const { refreshActiveCharacter } = useActiveCharacter();
const { showToast } = useToast();
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
Expand All @@ -25,6 +29,8 @@ export default function LoginPage() {

try {
await loginUser({ email, password });
// Propager la session dans toute l'app (Header, personnage actif…)
await Promise.all([refresh(), refreshActiveCharacter()]);
showToast('Connexion réussie ! Heureux de vous revoir.', 'success');
router.push('/');
router.refresh();
Expand Down
5 changes: 5 additions & 0 deletions front/src/app/auth/signup/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, { useState } from 'react';
import Link from 'next/link';
import { useRouter } from 'next/navigation';
import { ApiError, registerUser } from '@/shared/utils/api';
import { useAuth } from '@/shared/hooks/useAuth';
import { useToast } from '@/shared/contexts/ToastContext';
import { Button } from '@/shared/components/ui/Button';
import { Input } from '@/shared/components/ui/Input';
Expand All @@ -12,6 +13,7 @@ import { Mail, Lock, UserPlus, AlertCircle, CheckCircle2 } from 'lucide-react';

export default function SignupPage() {
const router = useRouter();
const { refresh } = useAuth();
const { showToast } = useToast();
const [email, setEmail] = useState('');
const [username, setUsername] = useState('');
Expand Down Expand Up @@ -41,6 +43,9 @@ export default function SignupPage() {
password_confirmation: confirmPassword,
});

// La session est ouverte dès l'inscription : propager dans toute l'app
await refresh();

if (response.user.email_verified_at) {
showToast('Bienvenue ! Votre compte a été créé avec succès.', 'success');
router.push('/');
Expand Down
3 changes: 3 additions & 0 deletions front/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Footer from "@/shared/components/Footer";
import ChatBox from "@/features/chat/components/ChatBox";
import { CharacterProvider } from "@/shared/contexts/CharacterContext";
import { ToastProvider } from "@/shared/contexts/ToastContext";
import { AuthProvider } from "@/shared/hooks/useAuth";

const inter = Inter({ subsets: ["latin"], variable: "--font-inter" });
const outfit = Outfit({ subsets: ["latin"], variable: "--font-outfit" });
Expand Down Expand Up @@ -57,6 +58,7 @@ export default function RootLayout({
<html lang="fr">
<body className={`${inter.variable} ${outfit.variable} ${starWars.variable} font-sans bg-black min-h-screen text-gray-200 antialiased`}>
<ToastProvider>
<AuthProvider>
<CharacterProvider>
<div className="min-h-screen bg-black flex flex-col">
<Header />
Expand All @@ -69,6 +71,7 @@ export default function RootLayout({
<ChatBox />
</div>
</CharacterProvider>
</AuthProvider>
</ToastProvider>
</body>
</html>
Expand Down
9 changes: 5 additions & 4 deletions front/src/features/profile/components/ProfileContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { Badge } from '@/shared/components/ui/Badge';
import { Avatar } from '@/shared/components/ui/Avatar';
import { CategoryHeader } from '@/shared/components/forum/CategoryHeader';
import { cn } from '@/shared/utils/cn';
import { apiFetch, fetchCurrentUser, logoutUser } from '@/shared/utils/api';
import { apiFetch, fetchCurrentUser } from '@/shared/utils/api';
import { useAuth } from '@/shared/hooks/useAuth';
import { useActiveCharacter } from '@/shared/contexts/CharacterContext';

// Système de grades forum (HRP)
Expand All @@ -34,6 +35,7 @@ export default function ProfileContent({ userId }: ProfileContentProps) {
const [currentUser, setCurrentUser] = useState<any>(null);
const [profileData, setProfileData] = useState<any>(null);
const [characters, setCharacters] = useState<any[]>([]);
const { logout } = useAuth();
const { activeCharacter, setActiveCharacter } = useActiveCharacter();
const [switchingId, setSwitchingId] = useState<string | null>(null);

Expand Down Expand Up @@ -80,9 +82,8 @@ export default function ProfileContent({ userId }: ProfileContentProps) {
const isOwnProfile = !userId || userId === currentUser?.id;

const handleLogout = async () => {
await logoutUser();
router.push('/');
router.refresh();
await logout();
window.location.href = '/';
};

const handleSwitchCharacter = async (e: React.MouseEvent, charId: string) => {
Expand Down
33 changes: 30 additions & 3 deletions front/src/shared/hooks/useAuth.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
'use client';

import { useCallback, useEffect, useState } from 'react';
import React, { createContext, useCallback, useContext, useEffect, useState } from 'react';
import type { AuthUser } from '@/shared/types/auth';
import { fetchCurrentUser, logoutUser } from '@/shared/utils/api';

export function useAuth() {
interface AuthContextType {
user: AuthUser | null;
loading: boolean;
refresh: () => Promise<AuthUser | null>;
logout: () => Promise<void>;
setUser: (user: AuthUser | null) => void;
}

const AuthContext = createContext<AuthContextType | undefined>(undefined);

/**
* État d'authentification global : un seul fetch de l'utilisateur courant,
* partagé par toute l'app (Header inclus). Après login/inscription, appeler
* refresh() (ou setUser) pour propager la session partout.
*/
export function AuthProvider({ children }: { children: React.ReactNode }) {
const [user, setUser] = useState<AuthUser | null>(null);
const [loading, setLoading] = useState(true);

Expand All @@ -24,5 +39,17 @@ export function useAuth() {
setUser(null);
}, []);

return { user, loading, refresh, logout, setUser };
return React.createElement(
AuthContext.Provider,
{ value: { user, loading, refresh, logout, setUser } },
children
);
}

export function useAuth() {
const context = useContext(AuthContext);
if (context === undefined) {
throw new Error('useAuth must be used within an AuthProvider');
}
return context;
}
Loading