Update favicon and site title

This commit is contained in:
CycroftX
2026-02-03 20:30:11 +05:30
parent 61423f951a
commit cbc7cd1bd3
19 changed files with 3276 additions and 42 deletions

View File

@@ -0,0 +1,124 @@
import React, { createContext, useContext, useState, useEffect } from 'react';
import { login as authLogin, logout as authLogout, checkUserStatus, getStoredAuth, storeAuth, clearAuth, AuthUser, AuthError } from '@/services/auth';
import { useToast } from '@/hooks/use-toast';
interface AuthContextType {
user: AuthUser | null;
isAuthenticated: boolean;
isLoading: boolean;
login: (username: string, password: string) => Promise<void>;
logout: () => Promise<void>;
}
const AuthContext = createContext<AuthContextType | undefined>(undefined);
export const useAuth = () => {
const context = useContext(AuthContext);
if (!context) {
throw new Error('useAuth must be used within an AuthProvider');
}
return context;
};
export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
const [user, setUser] = useState<AuthUser | null>(null);
const [isLoading, setIsLoading] = useState(true);
const { toast } = useToast();
// Check for existing auth on mount
useEffect(() => {
const checkAuth = async () => {
const storedAuth = getStoredAuth();
if (storedAuth) {
try {
// Verify token is still valid
await checkUserStatus(storedAuth.username, storedAuth.token);
setUser(storedAuth);
} catch (error) {
console.error('Auth verification failed:', error);
if (error instanceof AuthError && error.isInvalidToken) {
clearAuth();
toast({
title: 'Session Expired',
description: 'Your session has expired. Please login again.',
variant: 'destructive',
});
}
}
}
setIsLoading(false);
};
checkAuth();
}, []);
const login = async (username: string, password: string) => {
try {
setIsLoading(true);
const response = await authLogin(username, password);
// Store auth data
storeAuth(response);
// Set user state
const authUser: AuthUser = {
username: response.username,
token: response.token,
first_name: response.user?.first_name,
last_name: response.user?.last_name,
email: response.user?.email,
profile_photo: response.user?.profile_photo,
};
setUser(authUser);
toast({
title: 'Login Successful',
description: `Welcome back, ${username}!`,
});
} catch (error) {
console.error('Login error:', error);
const errorMessage = error instanceof Error ? error.message : 'Login failed';
toast({
title: 'Login Failed',
description: errorMessage,
variant: 'destructive',
});
throw error;
} finally {
setIsLoading(false);
}
};
const logout = async () => {
try {
if (user) {
await authLogout(user.username, user.token);
}
} catch (error) {
console.error('Logout error:', error);
} finally {
clearAuth();
setUser(null);
toast({
title: 'Logged Out',
description: 'You have been successfully logged out.',
});
}
};
const value: AuthContextType = {
user,
isAuthenticated: !!user,
isLoading,
login,
logout,
};
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
};