Admin Panel (Next.js): - Dashboard with stats - Users list with relationships (watches/watched_by) - User detail pages - Deployments list and detail pages - Devices, Orders, Subscriptions pages - OTP-based admin authentication Backend Optimizations: - Fixed N+1 query problem in admin APIs - Added pagination support - Added .range() and count support to Supabase wrapper - Optimized batch queries with lookup maps Database: - Added migrations for schema evolution - New tables: push_tokens, notification_settings - Updated access model iOS Build Scripts: - build-ios.sh, clear-apple-cache.sh - EAS configuration updates 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
151 lines
3.6 KiB
TypeScript
151 lines
3.6 KiB
TypeScript
import React, { createContext, useContext, useState, useEffect, useCallback, useRef, type ReactNode } from 'react';
|
|
import { api, setOnUnauthorizedCallback } 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();
|
|
}, []);
|
|
|
|
// Set up callback for 401 responses - auto logout
|
|
useEffect(() => {
|
|
setOnUnauthorizedCallback(() => {
|
|
api.logout().then(() => {
|
|
setState({
|
|
user: null,
|
|
isLoading: false,
|
|
isAuthenticated: false,
|
|
error: { message: 'Session expired. Please login again.' },
|
|
});
|
|
});
|
|
});
|
|
}, []);
|
|
|
|
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;
|
|
}
|