added registration page

This commit is contained in:
c-d-p
2025-04-23 20:53:40 +02:00
parent 2c911d2ef4
commit 10e5a3c489
6 changed files with 235 additions and 13 deletions

View File

@@ -24,6 +24,7 @@ interface AuthContextData {
user: UserData | null; // Add user data to context
login: (username: string, password: string) => Promise<void>;
logout: () => Promise<void>;
register: (username: string, password: string, name: string) => Promise<void>; // Add register function
}
const AuthContext = createContext<AuthContextData>({
@@ -32,6 +33,7 @@ const AuthContext = createContext<AuthContextData>({
user: null, // Initialize user as null
login: async () => { throw new Error('AuthContext not initialized'); },
logout: async () => { throw new Error('AuthContext not initialized'); },
register: async () => { throw new Error('AuthContext not initialized'); }, // Add register initializer
});
interface AuthProviderProps {
@@ -145,6 +147,33 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
}
}, [fetchUserData]); // Added fetchUserData dependency
const register = useCallback(async (username: string, password: string, name: string) => {
console.log("[AuthContext] register: Function called with:", username, name);
try {
// Call the backend register endpoint
const response = await apiClient.post('/auth/register', {
username,
password,
name,
}, {
headers: {
'accept': 'application/json',
'Content-Type': 'application/json',
},
});
console.log('[AuthContext] register: Registration successful:', response.data);
// Optionally, you could automatically log the user in here
// For now, we'll just let the user log in manually after registering
// Or display a success message and navigate back to login
} catch (error: any) {
console.error("[AuthContext] register: Caught Error Object:", error);
// Rethrow the error so the UI can handle it (e.g., display specific messages)
throw error;
}
}, []); // No dependencies needed for register itself
const logout = useCallback(async () => {
console.log('[AuthContext] logout: Logging out.');
const refreshToken = await getRefreshToken();
@@ -171,7 +200,8 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
user: userState, // Provide user state
login,
logout,
}), [isAuthenticatedState, isLoading, userState, login, logout]); // Added userState dependency
register, // Add register to context value
}), [isAuthenticatedState, isLoading, userState, login, logout, register]); // Added register dependency
// ... (rest of the component: Provider, useAuth, AuthLoadingScreen) ...
return (