contexte user reparer
This commit is contained in:
parent
cf029b1be4
commit
1a92228af5
@ -9,13 +9,12 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import Link from 'next/link';
|
||||
import Image from 'next/image';
|
||||
import { login } from '@/lib/api';
|
||||
import { useAuth } from '@/lib/context/auth-context';
|
||||
|
||||
export default function LoginPage() {
|
||||
const router = useRouter();
|
||||
const { login } = useAuth();
|
||||
const [email, setEmail] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [rememberMe, setRememberMe] = useState(false);
|
||||
@ -28,8 +27,8 @@ export default function LoginPage() {
|
||||
setIsLoading(true);
|
||||
|
||||
try {
|
||||
await login({ email, password });
|
||||
router.push('/dashboard');
|
||||
await login(email, password);
|
||||
// Navigation is handled by the login function in auth context
|
||||
} catch (err: any) {
|
||||
setError(err.message || 'Identifiants incorrects');
|
||||
} finally {
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
|
||||
'use client';
|
||||
|
||||
import React, { createContext, useContext, useState, useEffect } from 'react';
|
||||
import React, { createContext, useContext, useState, useEffect, useCallback } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { authApi, User } from '../api';
|
||||
|
||||
@ -22,6 +22,7 @@ interface AuthContextType {
|
||||
organizationId: string;
|
||||
}) => Promise<void>;
|
||||
logout: () => Promise<void>;
|
||||
refreshUser: () => Promise<void>;
|
||||
isAuthenticated: boolean;
|
||||
}
|
||||
|
||||
@ -32,6 +33,25 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
const [loading, setLoading] = useState(true);
|
||||
const router = useRouter();
|
||||
|
||||
// Memoized function to refresh user data
|
||||
const refreshUser = useCallback(async () => {
|
||||
try {
|
||||
if (authApi.isAuthenticated()) {
|
||||
const currentUser = await authApi.me();
|
||||
setUser(currentUser);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to refresh user:', error);
|
||||
// Token invalid, clear storage
|
||||
if (typeof window !== 'undefined') {
|
||||
localStorage.removeItem('accessToken');
|
||||
localStorage.removeItem('refreshToken');
|
||||
localStorage.removeItem('user');
|
||||
}
|
||||
setUser(null);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
// Check if user is already logged in
|
||||
const checkAuth = async () => {
|
||||
@ -62,12 +82,11 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
|
||||
const login = async (email: string, password: string) => {
|
||||
try {
|
||||
const response = await authApi.login({ email, password });
|
||||
setUser({
|
||||
...response.user,
|
||||
isEmailVerified: false,
|
||||
isActive: true
|
||||
} as User);
|
||||
await authApi.login({ email, password });
|
||||
// Fetch complete user data after login and update state
|
||||
const currentUser = await authApi.me();
|
||||
setUser(currentUser);
|
||||
// Navigate to dashboard
|
||||
router.push('/dashboard');
|
||||
} catch (error) {
|
||||
throw error;
|
||||
@ -82,12 +101,11 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
organizationId: string;
|
||||
}) => {
|
||||
try {
|
||||
const response = await authApi.register(data);
|
||||
setUser({
|
||||
...response.user,
|
||||
isEmailVerified: false,
|
||||
isActive: true
|
||||
} as User);
|
||||
await authApi.register(data);
|
||||
// Fetch complete user data after registration and update state
|
||||
const currentUser = await authApi.me();
|
||||
setUser(currentUser);
|
||||
// Navigate to dashboard
|
||||
router.push('/dashboard');
|
||||
} catch (error) {
|
||||
throw error;
|
||||
@ -109,6 +127,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
login,
|
||||
register,
|
||||
logout,
|
||||
refreshUser,
|
||||
isAuthenticated: !!user,
|
||||
};
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user