mirror of
https://github.com/Sea-Haven-Industries/proposal-system.git
synced 2026-10-02 06:03:13 +00:00
42 lines
1 KiB
TypeScript
42 lines
1 KiB
TypeScript
|
|
import { useCallback } from 'react';
|
||
|
|
import { useSelector, useDispatch } from 'react-redux';
|
||
|
|
import { Alert } from 'react-native';
|
||
|
|
import {
|
||
|
|
selectUser,
|
||
|
|
selectIsAuthenticated,
|
||
|
|
selectAuthLoading,
|
||
|
|
selectAuthError,
|
||
|
|
logout as logoutAction,
|
||
|
|
} from '../store/slices/authSlice';
|
||
|
|
import { authApi } from '../lib/api/auth';
|
||
|
|
import type { RootState } from '../store';
|
||
|
|
|
||
|
|
export function useAuth() {
|
||
|
|
const dispatch = useDispatch();
|
||
|
|
|
||
|
|
const user = useSelector((state: RootState) => selectUser(state));
|
||
|
|
const isAuthenticated = useSelector((state: RootState) =>
|
||
|
|
selectIsAuthenticated(state),
|
||
|
|
);
|
||
|
|
const loading = useSelector((state: RootState) => selectAuthLoading(state));
|
||
|
|
const error = useSelector((state: RootState) => selectAuthError(state));
|
||
|
|
|
||
|
|
const logout = useCallback(async () => {
|
||
|
|
try {
|
||
|
|
await authApi.logout();
|
||
|
|
} catch {
|
||
|
|
// Best-effort
|
||
|
|
}
|
||
|
|
dispatch(logoutAction());
|
||
|
|
Alert.alert('Signed Out', 'You have been logged out.');
|
||
|
|
}, [dispatch]);
|
||
|
|
|
||
|
|
return {
|
||
|
|
user,
|
||
|
|
isAuthenticated,
|
||
|
|
loading,
|
||
|
|
error,
|
||
|
|
logout,
|
||
|
|
};
|
||
|
|
}
|