register function in Author contexts
This commit is contained in:
@@ -12,6 +12,22 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
|||||||
const [loading, setLoading] = useState<boolean>(false);
|
const [loading, setLoading] = useState<boolean>(false);
|
||||||
const [error, setError] = useState<string | null>(null);
|
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 */
|
/** 🔹 Login and store JWT token */
|
||||||
const login = async (email: string, password: string) => {
|
const login = async (email: string, password: string) => {
|
||||||
try {
|
try {
|
||||||
@@ -52,7 +68,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
|||||||
setAuthors(res.data);
|
setAuthors(res.data);
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
console.error('Failed to fetch authors:', err);
|
console.error('Failed to fetch authors:', err);
|
||||||
setError(err.message || 'Failed to fetch authors');
|
setError(err.response?.data?.detail || 'Failed to fetch authors');
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
@@ -85,6 +101,7 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
|||||||
error,
|
error,
|
||||||
login,
|
login,
|
||||||
logout,
|
logout,
|
||||||
|
register,
|
||||||
refreshAuthors,
|
refreshAuthors,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ export interface AuthContextModel {
|
|||||||
loading: boolean;
|
loading: boolean;
|
||||||
error: string | null;
|
error: string | null;
|
||||||
login: (email: string, password: string) => Promise<void>;
|
login: (email: string, password: string) => Promise<void>;
|
||||||
|
register: (username: string, email: string, password: string) => Promise<void>;
|
||||||
logout: () => void;
|
logout: () => void;
|
||||||
refreshAuthors: () => Promise<void>;
|
refreshAuthors: () => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user