139 lines
3.5 KiB
TypeScript
139 lines
3.5 KiB
TypeScript
import { useNavigate } from 'react-router-dom';
|
|
import { useApi } from './useApi';
|
|
import { useLoader } from '../context/loaders/useLoader';
|
|
import { useToast } from '../context/toasts/useToast';
|
|
import { useApp } from '../context/AppContext';
|
|
import { User } from '../types';
|
|
|
|
export function useAuth() {
|
|
const navigate = useNavigate();
|
|
const { login } = useApp();
|
|
const { login: loginApi, register: registerApi } = useApi();
|
|
const { withLoader } = useLoader();
|
|
const { addNotification } = useToast();
|
|
|
|
const handleLogin = async (email: string, password: string) => {
|
|
try {
|
|
const response = await withLoader(() => loginApi(email, password), 'Logging in...');
|
|
|
|
// Convert UserData to User type
|
|
const user: User = {
|
|
id: parseInt(response.user.id),
|
|
username: response.user.username,
|
|
email: response.user.email,
|
|
};
|
|
|
|
// debugger
|
|
// Store in localStorage first
|
|
localStorage.setItem('token', response.access_token);
|
|
localStorage.setItem('user', JSON.stringify(user));
|
|
|
|
// Then update context
|
|
login(user, response.access_token);
|
|
|
|
// Show success toast
|
|
addNotification({
|
|
type: 'success',
|
|
title: 'Login Successful',
|
|
message: `Welcome back, ${user.username}!`,
|
|
duration: 3000,
|
|
});
|
|
|
|
// Navigate to boards
|
|
navigate('/boards');
|
|
|
|
return user;
|
|
} catch (err) {
|
|
const errorMessage = err instanceof Error ? err.message : 'Login failed. Please try again.';
|
|
|
|
// Show error toast
|
|
addNotification({
|
|
type: 'error',
|
|
title: 'Login Failed',
|
|
message: errorMessage,
|
|
duration: 5000,
|
|
});
|
|
|
|
throw err;
|
|
}
|
|
};
|
|
|
|
const handleRegister = async (userData: {
|
|
email: string;
|
|
password: string;
|
|
username: string;
|
|
first_name?: string;
|
|
last_name?: string;
|
|
}) => {
|
|
try {
|
|
const response = await withLoader(() => registerApi(userData), 'Creating account...');
|
|
|
|
// Convert UserData to User type
|
|
const user: User = {
|
|
id: parseInt(response.user.id),
|
|
username: response.user.username,
|
|
email: response.user.email,
|
|
};
|
|
|
|
// Store in localStorage first
|
|
// debugger
|
|
localStorage.setItem('token', response.access_token);
|
|
localStorage.setItem('user', JSON.stringify(user));
|
|
|
|
// Then update context
|
|
login(user, response.access_token);
|
|
|
|
// Show success toast
|
|
addNotification({
|
|
type: 'success',
|
|
title: 'Account Created',
|
|
message: `Welcome, ${user.username}!`,
|
|
duration: 3000,
|
|
});
|
|
|
|
// Navigate to boards
|
|
navigate('/boards');
|
|
|
|
return user;
|
|
} catch (err) {
|
|
const errorMessage =
|
|
err instanceof Error ? err.message : 'Registration failed. Please try again.';
|
|
|
|
// Show error toast
|
|
addNotification({
|
|
type: 'error',
|
|
title: 'Registration Failed',
|
|
message: errorMessage,
|
|
duration: 5000,
|
|
});
|
|
|
|
throw err;
|
|
}
|
|
};
|
|
|
|
const handleLogout = () => {
|
|
// Show logout toast
|
|
addNotification({
|
|
type: 'success',
|
|
title: 'Logged Out',
|
|
message: 'You have been logged out successfully.',
|
|
duration: 3000,
|
|
});
|
|
|
|
// Clear local storage
|
|
localStorage.removeItem('token');
|
|
localStorage.removeItem('user');
|
|
|
|
// Navigate to login
|
|
navigate('/login');
|
|
|
|
// Trigger page refresh to clear app context
|
|
window.location.reload();
|
|
};
|
|
|
|
return {
|
|
login: handleLogin,
|
|
register: handleRegister,
|
|
logout: handleLogout,
|
|
};
|
|
}
|