Download src/hooks/useAuth.ts from SherlockHolmes786/tradewithlive: direct link, hf CLI and curl.
- Browser
- Download file 3.07 kB
-
https://huggingface.co/SherlockHolmes786/tradewithlive/resolve/main/src/hooks/useAuth.ts
- Command line
-
hf download hf://SherlockHolmes786/tradewithlive/src/hooks/useAuth.ts
-
curl -L -o useAuth.ts https://huggingface.co/SherlockHolmes786/tradewithlive/resolve/main/src/hooks/useAuth.ts
3.07 kB
| import { useState, useEffect, useCallback } from 'react'; | |
| import { User, AuthSession, LoginCredentials } from '../types/auth'; | |
| const ADMIN_EMAIL = 'tradewithoutstrategy@gmail.com'; | |
| const ADMIN_PASSWORD = 'admin123'; // In production, use proper password hashing | |
| const initializeUsers = (): User[] => { | |
| const existingUsers = JSON.parse(localStorage.getItem('users') || '[]'); | |
| // Add admin user if not exists | |
| if (!existingUsers.some((u: User) => u.email === ADMIN_EMAIL)) { | |
| const adminUser: User = { | |
| id: 'admin_1', | |
| email: ADMIN_EMAIL, | |
| password: ADMIN_PASSWORD, | |
| role: 'admin', | |
| name: 'Admin', | |
| createdAt: new Date().toISOString(), | |
| }; | |
| existingUsers.push(adminUser); | |
| localStorage.setItem('users', JSON.stringify(existingUsers)); | |
| } | |
| return existingUsers; | |
| }; | |
| export const useAuth = () => { | |
| const [session, setSession] = useState<AuthSession | null>(null); | |
| const [isLoading, setIsLoading] = useState(false); | |
| const [error, setError] = useState<string | null>(null); | |
| // Initialize users and check for existing session | |
| useEffect(() => { | |
| initializeUsers(); | |
| const existingSession = localStorage.getItem('auth_session'); | |
| if (existingSession) { | |
| try { | |
| setSession(JSON.parse(existingSession)); | |
| } catch (err) { | |
| console.error('Error parsing session:', err); | |
| localStorage.removeItem('auth_session'); | |
| } | |
| } | |
| }, []); | |
| const login = useCallback(async (credentials: LoginCredentials): Promise<boolean> => { | |
| setIsLoading(true); | |
| setError(null); | |
| try { | |
| const users = JSON.parse(localStorage.getItem('users') || '[]'); | |
| const user = users.find((u: User) => | |
| u.email === credentials.email && u.password === credentials.password | |
| ); | |
| if (!user) { | |
| setError('Invalid email or password'); | |
| return false; | |
| } | |
| const newSession: AuthSession = { | |
| user, | |
| isLoggedIn: true, | |
| loginTime: new Date().toISOString(), | |
| }; | |
| setSession(newSession); | |
| localStorage.setItem('auth_session', JSON.stringify(newSession)); | |
| return true; | |
| } catch (err) { | |
| setError('Login failed. Please try again.'); | |
| console.error('Login error:', err); | |
| return false; | |
| } finally { | |
| setIsLoading(false); | |
| } | |
| }, []); | |
| const logout = useCallback(() => { | |
| setSession(null); | |
| localStorage.removeItem('auth_session'); | |
| }, []); | |
| const isAdmin = session?.user.role === 'admin'; | |
| const isInfluencer = session?.user.role === 'influencer'; | |
| const isViewer = session?.user.role === 'viewer'; | |
| const isLoggedIn = session?.isLoggedIn || false; | |
| // Check if current user is the owner of the influencer account | |
| const isInfluencerOwner = useCallback((influencerId: string): boolean => { | |
| return session?.user.influencerId === influencerId; | |
| }, [session]); | |
| return { | |
| session, | |
| user: session?.user, | |
| isLoggedIn, | |
| isAdmin, | |
| isInfluencer, | |
| isViewer, | |
| isLoading, | |
| error, | |
| login, | |
| logout, | |
| isInfluencerOwner, | |
| }; | |
| }; | |