fix login

This commit is contained in:
Matthias Hochmeister
2026-02-27 14:42:51 +01:00
parent 2a70c274fb
commit 36ffe7e88e
2 changed files with 12 additions and 8 deletions

View File

@@ -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 { useAuth } from '../../contexts/AuthContext';
import { Box, CircularProgress, Typography, Alert, Button } from '@mui/material';
@@ -8,8 +8,12 @@ const LoginCallback: React.FC = () => {
const [searchParams] = useSearchParams();
const { login } = useAuth();
const [error, setError] = useState<string>('');
const hasCalledLogin = useRef(false);
useEffect(() => {
if (hasCalledLogin.current) return;
hasCalledLogin.current = true;
const handleCallback = async () => {
const code = searchParams.get('code');
const errorParam = searchParams.get('error');

View File

@@ -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 { authService } from '../services/auth';
import { getToken, setToken, removeToken, getUser, setUser, removeUser } from '../utils/storage';
@@ -61,7 +61,7 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
initializeAuth();
}, []);
const login = async (code: string): Promise<void> => {
const login = useCallback(async (code: string): Promise<void> => {
try {
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.');
throw error;
}
};
}, [notification]);
const logout = (): void => {
const logout = useCallback((): void => {
// Call backend logout (fire and forget)
authService.logout().catch((error) => {
console.error('Backend logout failed:', error);
@@ -119,9 +119,9 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
setTimeout(() => {
window.location.href = '/login';
}, 1000);
};
}, [notification]);
const refreshAuth = async (): Promise<void> => {
const refreshAuth = useCallback(async (): Promise<void> => {
try {
const user = await authService.getCurrentUser();
setUser(user);
@@ -130,7 +130,7 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
console.error('Failed to refresh user data:', error);
logout();
}
};
}, [logout]);
const value: AuthContextType = {
...state,