Spaces:
Sleeping
Sleeping
Download frontend-backup/src/contexts/AuthContext.tsx from edichat/edi.chat.test: direct link, hf CLI and curl.
- Browser
- Download file 5.65 kB
-
https://huggingface.co/spaces/edichat/edi.chat.test/resolve/main/frontend-backup/src/contexts/AuthContext.tsx
- Command line
-
hf download hf://spaces/edichat/edi.chat.test/frontend-backup/src/contexts/AuthContext.tsx
-
curl -L -o AuthContext.tsx https://huggingface.co/spaces/edichat/edi.chat.test/resolve/main/frontend-backup/src/contexts/AuthContext.tsx
5.65 kB
| import React, { createContext, useContext, useReducer, useEffect, ReactNode } from 'react'; | |
| import { User, LoginRequest, ApiResponse, LoginResponse } from '../types'; | |
| import { authAPI } from '../services/api'; | |
| import toast from 'react-hot-toast'; | |
| interface AuthState { | |
| user: User | null; | |
| token: string | null; | |
| isLoading: boolean; | |
| isAuthenticated: boolean; | |
| } | |
| type AuthAction = | |
| | { type: 'AUTH_START' } | |
| | { type: 'AUTH_SUCCESS'; payload: { user: User; token: string } } | |
| | { type: 'AUTH_FAILURE' } | |
| | { type: 'LOGOUT' } | |
| | { type: 'UPDATE_USER'; payload: User }; | |
| interface AuthContextType extends AuthState { | |
| login: (credentials: LoginRequest) => Promise<boolean>; | |
| loginWithMyID: (credentials: LoginRequest) => Promise<boolean>; | |
| logout: () => void; | |
| updateUser: (user: User) => void; | |
| updateProfile: (data: Partial<User>) => Promise<boolean>; | |
| } | |
| const AuthContext = createContext<AuthContextType | undefined>(undefined); | |
| const authReducer = (state: AuthState, action: AuthAction): AuthState => { | |
| switch (action.type) { | |
| case 'AUTH_START': | |
| return { ...state, isLoading: true }; | |
| case 'AUTH_SUCCESS': | |
| return { | |
| ...state, | |
| isLoading: false, | |
| isAuthenticated: true, | |
| user: action.payload.user, | |
| token: action.payload.token, | |
| }; | |
| case 'AUTH_FAILURE': | |
| return { | |
| ...state, | |
| isLoading: false, | |
| isAuthenticated: false, | |
| user: null, | |
| token: null, | |
| }; | |
| case 'LOGOUT': | |
| return { | |
| ...state, | |
| isAuthenticated: false, | |
| user: null, | |
| token: null, | |
| }; | |
| case 'UPDATE_USER': | |
| return { | |
| ...state, | |
| user: action.payload, | |
| }; | |
| default: | |
| return state; | |
| } | |
| }; | |
| // TEMPORARY: BYPASS AUTHENTICATION FOR DEVELOPMENT | |
| const mockUser: User = { | |
| id: '507f1f77bcf86cd799439011', | |
| ugaId: '811999999', | |
| email: 'dev@uga.edu', | |
| firstName: 'Development', | |
| lastName: 'User', | |
| role: 'student', | |
| college: 'Franklin College of Arts & Sciences', | |
| major: 'Computer Science', | |
| classification: 'senior' | |
| } as User; | |
| const initialState: AuthState = { | |
| user: mockUser, | |
| token: 'dev-bypass-token', | |
| isLoading: false, | |
| isAuthenticated: true, | |
| }; | |
| export const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => { | |
| const [state, dispatch] = useReducer(authReducer, initialState); | |
| useEffect(() => { | |
| // TEMPORARY: SKIP AUTH INITIALIZATION FOR DEVELOPMENT | |
| // Authentication is bypassed - user is always authenticated with mock data | |
| console.log('🚧 DEV MODE: Authentication bypassed'); | |
| }, []); | |
| const login = async (credentials: LoginRequest): Promise<boolean> => { | |
| dispatch({ type: 'AUTH_START' }); | |
| try { | |
| const response: ApiResponse<LoginResponse> = await authAPI.login(credentials); | |
| if (response.success && response.data) { | |
| const { user, token } = response.data; | |
| localStorage.setItem('uga-chat-token', token); | |
| dispatch({ type: 'AUTH_SUCCESS', payload: { user, token } }); | |
| toast.success(`Welcome back, ${user.firstName}!`); | |
| return true; | |
| } else { | |
| dispatch({ type: 'AUTH_FAILURE' }); | |
| toast.error(response.error || 'Login failed'); | |
| return false; | |
| } | |
| } catch (error: any) { | |
| dispatch({ type: 'AUTH_FAILURE' }); | |
| toast.error(error.message || 'Login failed'); | |
| return false; | |
| } | |
| }; | |
| const loginWithMyID = async (credentials: LoginRequest): Promise<boolean> => { | |
| dispatch({ type: 'AUTH_START' }); | |
| try { | |
| const response: ApiResponse<LoginResponse> = await authAPI.loginWithMyID(credentials); | |
| if (response.success && response.data) { | |
| const { user, token } = response.data; | |
| localStorage.setItem('uga-chat-token', token); | |
| dispatch({ type: 'AUTH_SUCCESS', payload: { user, token } }); | |
| toast.success(`Welcome, ${user.firstName}!`); | |
| return true; | |
| } else { | |
| dispatch({ type: 'AUTH_FAILURE' }); | |
| toast.error(response.error || 'MyID login failed'); | |
| return false; | |
| } | |
| } catch (error: any) { | |
| dispatch({ type: 'AUTH_FAILURE' }); | |
| toast.error(error.message || 'MyID login failed'); | |
| return false; | |
| } | |
| }; | |
| const logout = async () => { | |
| try { | |
| await authAPI.logout(); | |
| } catch (error) { | |
| // Continue with logout even if API call fails | |
| } finally { | |
| localStorage.removeItem('uga-chat-token'); | |
| dispatch({ type: 'LOGOUT' }); | |
| toast.success('Logged out successfully'); | |
| } | |
| }; | |
| const updateUser = (user: User) => { | |
| dispatch({ type: 'UPDATE_USER', payload: user }); | |
| }; | |
| const updateProfile = async (data: Partial<User>): Promise<boolean> => { | |
| try { | |
| const response: ApiResponse<User> = await authAPI.updateProfile(data); | |
| if (response.success && response.data) { | |
| dispatch({ type: 'UPDATE_USER', payload: response.data }); | |
| toast.success('Profile updated successfully'); | |
| return true; | |
| } else { | |
| toast.error(response.error || 'Failed to update profile'); | |
| return false; | |
| } | |
| } catch (error: any) { | |
| toast.error(error.message || 'Failed to update profile'); | |
| return false; | |
| } | |
| }; | |
| const value: AuthContextType = { | |
| ...state, | |
| login, | |
| loginWithMyID, | |
| logout, | |
| updateUser, | |
| updateProfile, | |
| }; | |
| return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>; | |
| }; | |
| export const useAuth = () => { | |
| const context = useContext(AuthContext); | |
| if (context === undefined) { | |
| throw new Error('useAuth must be used within an AuthProvider'); | |
| } | |
| return context; | |
| }; |