fix login
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import React, { useEffect, useState } from 'react';
|
import React, { useEffect, useRef, useState } from 'react';
|
||||||
import { useNavigate, useSearchParams } from 'react-router-dom';
|
import { useNavigate, useSearchParams } from 'react-router-dom';
|
||||||
import { useAuth } from '../../contexts/AuthContext';
|
import { useAuth } from '../../contexts/AuthContext';
|
||||||
import { Box, CircularProgress, Typography, Alert, Button } from '@mui/material';
|
import { Box, CircularProgress, Typography, Alert, Button } from '@mui/material';
|
||||||
@@ -8,8 +8,12 @@ const LoginCallback: React.FC = () => {
|
|||||||
const [searchParams] = useSearchParams();
|
const [searchParams] = useSearchParams();
|
||||||
const { login } = useAuth();
|
const { login } = useAuth();
|
||||||
const [error, setError] = useState<string>('');
|
const [error, setError] = useState<string>('');
|
||||||
|
const hasCalledLogin = useRef(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (hasCalledLogin.current) return;
|
||||||
|
hasCalledLogin.current = true;
|
||||||
|
|
||||||
const handleCallback = async () => {
|
const handleCallback = async () => {
|
||||||
const code = searchParams.get('code');
|
const code = searchParams.get('code');
|
||||||
const errorParam = searchParams.get('error');
|
const errorParam = searchParams.get('error');
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import React, { createContext, useContext, useState, useEffect, ReactNode } from 'react';
|
import React, { createContext, useCallback, useContext, useState, useEffect, ReactNode } from 'react';
|
||||||
import { AuthContextType, AuthState, User } from '../types/auth.types';
|
import { AuthContextType, AuthState, User } from '../types/auth.types';
|
||||||
import { authService } from '../services/auth';
|
import { authService } from '../services/auth';
|
||||||
import { getToken, setToken, removeToken, getUser, setUser, removeUser } from '../utils/storage';
|
import { getToken, setToken, removeToken, getUser, setUser, removeUser } from '../utils/storage';
|
||||||
@@ -61,7 +61,7 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
|
|||||||
initializeAuth();
|
initializeAuth();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const login = async (code: string): Promise<void> => {
|
const login = useCallback(async (code: string): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
setState((prev) => ({ ...prev, isLoading: true }));
|
setState((prev) => ({ ...prev, isLoading: true }));
|
||||||
|
|
||||||
@@ -94,9 +94,9 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
|
|||||||
notification.showError('Anmeldung fehlgeschlagen. Bitte versuchen Sie es erneut.');
|
notification.showError('Anmeldung fehlgeschlagen. Bitte versuchen Sie es erneut.');
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
}, [notification]);
|
||||||
|
|
||||||
const logout = (): void => {
|
const logout = useCallback((): void => {
|
||||||
// Call backend logout (fire and forget)
|
// Call backend logout (fire and forget)
|
||||||
authService.logout().catch((error) => {
|
authService.logout().catch((error) => {
|
||||||
console.error('Backend logout failed:', error);
|
console.error('Backend logout failed:', error);
|
||||||
@@ -119,9 +119,9 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
|
|||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
window.location.href = '/login';
|
window.location.href = '/login';
|
||||||
}, 1000);
|
}, 1000);
|
||||||
};
|
}, [notification]);
|
||||||
|
|
||||||
const refreshAuth = async (): Promise<void> => {
|
const refreshAuth = useCallback(async (): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
const user = await authService.getCurrentUser();
|
const user = await authService.getCurrentUser();
|
||||||
setUser(user);
|
setUser(user);
|
||||||
@@ -130,7 +130,7 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
|
|||||||
console.error('Failed to refresh user data:', error);
|
console.error('Failed to refresh user data:', error);
|
||||||
logout();
|
logout();
|
||||||
}
|
}
|
||||||
};
|
}, [logout]);
|
||||||
|
|
||||||
const value: AuthContextType = {
|
const value: AuthContextType = {
|
||||||
...state,
|
...state,
|
||||||
|
|||||||
Reference in New Issue
Block a user