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