WellNuo/contexts/AuthContext.tsx
Sergei a804a82512 Implement Auth, Patients List, and Dashboard
Features:
- Login screen with API integration (credentials endpoint)
- SecureStore for token management
- Patients list with health data display
- Patient dashboard with stats and quick actions
- AI Chat screen (voice_ask API integration)
- Profile screen with logout
- Full error handling and loading states
- WellNuo brand colors and UI components

API Integration:
- Base URL: eluxnetworks.net/function/well-api/api
- Auth: function=credentials
- Chat: function=voice_ask

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-12 12:58:15 -08:00

137 lines
3.2 KiB
TypeScript

import React, { createContext, useContext, useState, useEffect, useCallback, type ReactNode } from 'react';
import { api } from '@/services/api';
import type { User, LoginCredentials, ApiError } from '@/types';
interface AuthState {
user: User | null;
isLoading: boolean;
isAuthenticated: boolean;
error: ApiError | null;
}
interface AuthContextType extends AuthState {
login: (credentials: LoginCredentials) => Promise<boolean>;
logout: () => Promise<void>;
clearError: () => void;
}
const AuthContext = createContext<AuthContextType | null>(null);
export function AuthProvider({ children }: { children: ReactNode }) {
const [state, setState] = useState<AuthState>({
user: null,
isLoading: true,
isAuthenticated: false,
error: null,
});
// Check authentication on mount
useEffect(() => {
checkAuth();
}, []);
const checkAuth = async () => {
try {
const isAuth = await api.isAuthenticated();
if (isAuth) {
const user = await api.getStoredUser();
setState({
user,
isLoading: false,
isAuthenticated: !!user,
error: null,
});
} else {
setState({
user: null,
isLoading: false,
isAuthenticated: false,
error: null,
});
}
} catch (error) {
setState({
user: null,
isLoading: false,
isAuthenticated: false,
error: { message: 'Failed to check authentication' },
});
}
};
const login = useCallback(async (credentials: LoginCredentials): Promise<boolean> => {
setState((prev) => ({ ...prev, isLoading: true, error: null }));
try {
const response = await api.login(credentials.username, credentials.password);
if (response.ok && response.data) {
const user: User = {
user_id: response.data.user_id,
user_name: credentials.username,
max_role: response.data.max_role,
privileges: response.data.privileges,
};
setState({
user,
isLoading: false,
isAuthenticated: true,
error: null,
});
return true;
}
setState((prev) => ({
...prev,
isLoading: false,
error: response.error || { message: 'Login failed' },
}));
return false;
} catch (error) {
setState((prev) => ({
...prev,
isLoading: false,
error: { message: error instanceof Error ? error.message : 'Login failed' },
}));
return false;
}
}, []);
const logout = useCallback(async () => {
setState((prev) => ({ ...prev, isLoading: true }));
try {
await api.logout();
} finally {
setState({
user: null,
isLoading: false,
isAuthenticated: false,
error: null,
});
}
}, []);
const clearError = useCallback(() => {
setState((prev) => ({ ...prev, error: null }));
}, []);
return (
<AuthContext.Provider value={{ ...state, login, logout, clearError }}>
{children}
</AuthContext.Provider>
);
}
export function useAuth() {
const context = useContext(AuthContext);
if (!context) {
throw new Error('useAuth must be used within an AuthProvider');
}
return context;
}