Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions apps/mobile/src/context/AuthContext.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<void>;
logout: () => Promise<void>;
refreshUser: () => Promise<void>;
enterDemoMode: () => Promise<void>;
}


// ── Context ───────────────────────────────────────────────────────────────────

Expand All @@ -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([
Expand Down Expand Up @@ -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<User>('/api/profiles/me', newToken).catch(() => null);
Expand All @@ -98,6 +94,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
}
}, []);


// ── Logout ──

const logout = useCallback(async () => {
Expand Down Expand Up @@ -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;
}
}
Loading