register function in Author contexts

This commit is contained in:
2025-11-11 18:33:40 +05:30
parent 90e6a85fff
commit 3bf0a5839c
2 changed files with 19 additions and 1 deletions

View File

@@ -12,6 +12,22 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<string | null>(null);
/** 🔹 Register new user */
const register = async (username: string, email: string, password: string) => {
try {
setLoading(true);
setError(null);
const res = await api.post('/auth/register', { username, email, password });
return res.data; // returns PublicUser from backend
} catch (err: any) {
console.error('Registration failed:', err);
setError(err.response?.data?.detail || 'Registration failed');
} finally {
setLoading(false);
}
};
/** 🔹 Login and store JWT token */
const login = async (email: string, password: string) => {
try {
@@ -52,7 +68,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
setAuthors(res.data);
} catch (err: any) {
console.error('Failed to fetch authors:', err);
setError(err.message || 'Failed to fetch authors');
setError(err.response?.data?.detail || 'Failed to fetch authors');
} finally {
setLoading(false);
}
@@ -85,6 +101,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
error,
login,
logout,
register,
refreshAuthors,
}}
>

View File

@@ -14,6 +14,7 @@ export interface AuthContextModel {
loading: boolean;
error: string | null;
login: (email: string, password: string) => Promise<void>;
register: (username: string, email: string, password: string) => Promise<void>;
logout: () => void;
refreshAuthors: () => Promise<void>;
}