- Fix TIME object vs string comparison in scheduler preventing adaptive med reminders from ever firing (#12, #6) - Add frequency filtering to midnight schedule creation for every_n_days meds - Require start_date and interval_days for every_n_days medications - Add refresh token support (30-day) to API and bot for persistent sessions (#13) - Add "trusted device" checkbox to frontend login for long-lived sessions (#7) - Auto-refresh expired tokens in both bot (apiRequest) and frontend (api.ts) - Restore bot sessions from cache on restart using refresh tokens - Duration-aware routine scheduling conflict detection (#11) - Add conflict check when starting routine sessions against medication times - Add diagnostic logging to notification delivery channels Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
107 lines
2.7 KiB
TypeScript
107 lines
2.7 KiB
TypeScript
'use client';
|
|
|
|
import React, { createContext, useContext, useState, useEffect, useCallback } from 'react';
|
|
import api from '@/lib/api';
|
|
import { User } from '@/types';
|
|
|
|
interface AuthContextType {
|
|
user: User | null;
|
|
token: string | null;
|
|
isLoading: boolean;
|
|
isAuthenticated: boolean;
|
|
login: (username: string, password: string, trustDevice?: boolean) => Promise<void>;
|
|
register: (username: string, password: string) => Promise<void>;
|
|
logout: () => void;
|
|
refreshUser: () => Promise<void>;
|
|
}
|
|
|
|
const AuthContext = createContext<AuthContextType | undefined>(undefined);
|
|
|
|
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
|
const [user, setUser] = useState<User | null>(null);
|
|
const [token, setToken] = useState<string | null>(null);
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
|
|
const refreshUser = useCallback(async () => {
|
|
const storedToken = api.auth.getToken();
|
|
if (!storedToken) {
|
|
setIsLoading(false);
|
|
return;
|
|
}
|
|
|
|
setToken(storedToken);
|
|
try {
|
|
const tokenParts = storedToken.split('.');
|
|
if (tokenParts.length === 3) {
|
|
const payload = JSON.parse(atob(tokenParts[1]));
|
|
const userId = payload.sub;
|
|
if (userId) {
|
|
const userData = await api.user.get(userId);
|
|
setUser(userData);
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Failed to refresh user:', error);
|
|
api.auth.logout();
|
|
setToken(null);
|
|
setUser(null);
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
refreshUser();
|
|
}, [refreshUser]);
|
|
|
|
const login = async (username: string, password: string, trustDevice = false) => {
|
|
const result = await api.auth.login(username, password, trustDevice);
|
|
const storedToken = api.auth.getToken();
|
|
setToken(storedToken);
|
|
|
|
const tokenParts = storedToken!.split('.');
|
|
const payload = JSON.parse(atob(tokenParts[1]));
|
|
const userId = payload.sub;
|
|
|
|
if (userId) {
|
|
const userData = await api.user.get(userId);
|
|
setUser(userData);
|
|
}
|
|
};
|
|
|
|
const register = async (username: string, password: string) => {
|
|
await api.auth.register(username, password);
|
|
};
|
|
|
|
const logout = () => {
|
|
api.auth.logout();
|
|
setToken(null);
|
|
setUser(null);
|
|
};
|
|
|
|
return (
|
|
<AuthContext.Provider
|
|
value={{
|
|
user,
|
|
token,
|
|
isLoading,
|
|
isAuthenticated: !!user,
|
|
login,
|
|
register,
|
|
logout,
|
|
refreshUser,
|
|
}}
|
|
>
|
|
{children}
|
|
</AuthContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useAuth() {
|
|
const context = useContext(AuthContext);
|
|
if (context === undefined) {
|
|
throw new Error('useAuth must be used within an AuthProvider');
|
|
}
|
|
return context;
|
|
}
|