proposal-system/mobile/src/hooks/useAuth.ts
Adam Moussa 47c3b8e5eb Add mobile core: API client, auth, storage, state, and offline support
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.
2026-05-17 15:09:23 -04:00

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,
};
}