Files
Reduce/frontend/src/context/AuthContext.tsx
Arkaprabha Chakraborty 2a46aa7d79 feat: version 2.1
2026-02-13 00:01:00 +05:30

63 lines
1.6 KiB
TypeScript

import { createContext, useContext, useState, useEffect, ReactNode } from 'react';
import api from '../lib/api';
interface User {
id: number;
username: string;
}
interface AuthContextType {
user: User | null;
login: (username: string, password: string) => Promise<void>;
register: (username: string, password: string) => Promise<void>;
logout: () => void;
isLoading: boolean;
}
const AuthContext = createContext<AuthContextType | undefined>(undefined);
export function AuthProvider({ children }: { children: ReactNode }) {
const [user, setUser] = useState<User | null>(null);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
// Try to authenticate with existing cookie
api
.get('/auth/me')
.then((res) => setUser(res.data))
.catch(() => setUser(null))
.finally(() => setIsLoading(false));
}, []);
const login = async (username: string, password: string) => {
const res = await api.post('/auth/login', { username, password });
setUser(res.data.user);
};
const register = async (username: string, password: string) => {
const res = await api.post('/auth/register', { username, password });
setUser(res.data.user);
};
const logout = async () => {
try {
await api.post('/auth/logout');
} catch {
// Ignore errors during logout
}
setUser(null);
};
return (
<AuthContext.Provider value={{ user, login, register, logout, isLoading }}>
{children}
</AuthContext.Provider>
);
}
export function useAuth() {
const ctx = useContext(AuthContext);
if (!ctx) throw new Error('useAuth must be used within AuthProvider');
return ctx;
}