import React, { createContext, useContext, useEffect, useState, ReactNode } from "react"; import { useRouter, usePathname } from "expo-router"; import queryClient from "@/utils/queryClient"; import { DeviceEventEmitter } from "react-native"; import { FORCE_LOGOUT_EVENT } from "common-ui/src/lib/const-strs"; import { trpc } from "@/src/trpc-client"; import { saveJWT, getJWT, deleteJWT, saveStaff, getStaff, deleteStaff } from "@/hooks/useJWT"; import { takePendingHref } from "@/services/notif-service/pending-navigation"; import type { IdName } from '@packages/shared'; interface Staff extends IdName { regionId?: number | null; } interface StaffAuthContextType { isLoggedIn: boolean; isLoading: boolean; staff: Staff | null; regionId: number | null; login: (name: string, password: string) => Promise; logout: () => void; isLoggingIn: boolean; loginError: string | null; } const StaffAuthContext = createContext(undefined); export const StaffAuthProvider = ({ children }: { children: ReactNode }) => { const [isLoggedIn, setIsLoggedIn] = useState(false); const [isLoading, setIsLoading] = useState(true); const [staff, setStaff] = useState(null); const [loginError, setLoginError] = useState(null); const loginMutation = trpc.admin.staffUser.login.useMutation(); const router = useRouter(); const pathname = usePathname(); useEffect(() => { const checkAuth = async () => { const token = await getJWT(); if (token) { // Restore the staff identity saved at login so screens (e.g. the drawer // header) still know who is signed in after a reload. setStaff(await getStaff()); setIsLoggedIn(true); } else { setIsLoggedIn(false); if (!pathname.includes("login")) { router.replace("/login" as any); } } setIsLoading(false); }; checkAuth(); }, []); const login = async (name: string, password: string) => { setLoginError(null); loginMutation.mutate( { name, password }, { onSuccess: async (data) => { await Promise.all([ saveJWT(data.token), saveStaff(data.staff), ]); setStaff(data.staff); setIsLoggedIn(true); // A push tapped while signed out waits here; land on it first. const pendingHref = takePendingHref(); router.replace((pendingHref ?? "/(drawer)/dashboard") as any); }, onError: (error: any) => { setLoginError(error.message || "Login failed"); }, } ); }; const logout = async () => { await Promise.all([ deleteJWT(), deleteStaff(), ]); setIsLoggedIn(false); setStaff(null); queryClient.clear(); router.replace("/login" as any); }; useEffect(() => { const subscription = DeviceEventEmitter.addListener(FORCE_LOGOUT_EVENT, () => { console.log('force logout event received'); logout(); }); return () => { subscription.remove(); }; }, []); return ( {children} ); }; export const useStaffAuth = () => { const context = useContext(StaffAuthContext); if (!context) { throw new Error("useStaffAuth must be used within a StaffAuthProvider"); } return context; };