mirror of
https://github.com/Sea-Haven-Industries/proposal-system.git
synced 2026-09-30 18:13:14 +00:00
Keychain token storage, Axios client with async token injection, Cognito PKCE auth via react-native-app-auth, Redux auth slice, TanStack Query config, offline draft queue with NetInfo auto-submit, and SHOC theme.
41 lines
1 KiB
TypeScript
41 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,
|
|
};
|
|
}
|