diff --git a/apps/mobile/src/context/AuthContext.tsx b/apps/mobile/src/context/AuthContext.tsx index 4852a16..9c77043 100644 --- a/apps/mobile/src/context/AuthContext.tsx +++ b/apps/mobile/src/context/AuthContext.tsx @@ -1,3 +1,4 @@ + import React, { createContext, useContext, useState, useEffect, useCallback, ReactNode } from 'react'; import AsyncStorage from '@react-native-async-storage/async-storage'; import { get } from '../services/api'; @@ -10,31 +11,24 @@ const FIRST_LAUNCH_KEY = 'devcard.firstLaunch'; // ── Types ───────────────────────────────────────────────────────────────────── -interface User { + +const TOKEN_KEY = 'devcard_auth_token'; +const API_BASE_URL = process.env.EXPO_PUBLIC_API_URL ?? 'http://localhost:3000'; + +type User = { id: string; - email: string; username: string; - displayName: string; - bio: string | null; - pronouns: string | null; - role: string | null; - company: string | null; - avatarUrl: string | null; - accentColor: string; - defaultCardId: string | null; -} + email: string; +}; -interface AuthContextType { +type AuthContextType = { user: User | null; token: string | null; - isAuthenticated: boolean; isLoading: boolean; isFirstLaunch: boolean; login: (token: string) => Promise; logout: () => Promise; - refreshUser: () => Promise; - enterDemoMode: () => Promise; -} + // ── Context ─────────────────────────────────────────────────────────────────── @@ -49,6 +43,7 @@ export function AuthProvider({ children }: { children: ReactNode }) { // ── Hydrate token from AsyncStorage on mount ── useEffect(() => { + const hydrate = async () => { try { const [storedToken, launchFlag] = await Promise.all([ @@ -87,6 +82,7 @@ export function AuthProvider({ children }: { children: ReactNode }) { const login = useCallback(async (newToken: string) => { setToken(newToken); + try { await AsyncStorage.setItem(TOKEN_KEY, newToken); const userData = await get('/api/profiles/me', newToken).catch(() => null); @@ -98,6 +94,7 @@ export function AuthProvider({ children }: { children: ReactNode }) { } }, []); + // ── Logout ── const logout = useCallback(async () => { @@ -148,8 +145,6 @@ export function AuthProvider({ children }: { children: ReactNode }) { export function useAuth(): AuthContextType { const context = useContext(AuthContext); - if (!context) { - throw new Error('useAuth must be used within an AuthProvider'); - } + if (!context) throw new Error('useAuth must be used within AuthProvider'); return context; -} +} \ No newline at end of file