Only redirect on sign out if viewing protected page

This commit is contained in:
Sam Becker 2025-03-01 16:55:42 -06:00
parent 7d0443d78c
commit 5acc6948c0
4 changed files with 10 additions and 14 deletions

View File

@ -131,8 +131,7 @@ export default function AdminAppMenu({
}, {
label: 'Sign Out',
icon: <PiSignOutBold size={15} />,
action: () => signOutAction()
.then(() => clearAuthStateAndRedirect?.(false)),
action: () => signOutAction().then(clearAuthStateAndRedirect),
});
return (

View File

@ -10,7 +10,7 @@ import {
signIn,
signOut,
} from '@/auth';
import { PATH_ADMIN_PHOTOS, PATH_SIGN_IN } from '@/app/paths';
import { PATH_ADMIN_PHOTOS } from '@/app/paths';
import type { Session } from 'next-auth';
import { redirect } from 'next/navigation';
@ -44,9 +44,6 @@ export const signInAction = async (
export const signOutAction = async () =>
signOut({ redirect: false });
export const signOutAndRedirectAction = async (redirectTo = PATH_SIGN_IN) =>
signOut({ redirectTo });
export const getAuthAction = async () => auth();
export const logClientAuthUpdate = async (data: Session | null | undefined) =>

View File

@ -39,7 +39,7 @@ export type AppStateContext = {
setUserEmail?: Dispatch<SetStateAction<string | undefined>>
isUserSignedIn?: boolean
isUserSignedInEager?: boolean
clearAuthStateAndRedirect?: (shouldRedirect?: boolean) => void
clearAuthStateAndRedirect?: () => void
// ADMIN
adminUpdateTimes?: Date[]
registerAdminUpdate?: () => void

View File

@ -20,8 +20,8 @@ import {
clearAuthEmailCookie,
hasAuthEmailCookie,
} from '@/auth/client';
import { useRouter } from 'next/navigation';
import { PATH_SIGN_IN } from '@/app/paths';
import { useRouter, usePathname } from 'next/navigation';
import { isPathAdmin, PATH_SIGN_IN } from '@/app/paths';
import { INITIAL_UPLOAD_STATE, UploadState } from '@/admin/upload';
export default function AppStateProvider({
@ -31,6 +31,8 @@ export default function AppStateProvider({
}) {
const router = useRouter();
const pathname = usePathname();
const { previousPathname } = usePathnames();
// CORE
@ -117,18 +119,16 @@ export default function AppStateProvider({
}
}, [userEmail, refreshAdminData, adminData]);
console.log({ userEmail, isUserSignedIn, isUserSignedInEager });
const registerAdminUpdate = useCallback(() =>
setAdminUpdateTimes(updates => [...updates, new Date()])
, []);
const clearAuthStateAndRedirect = useCallback((shouldRedirect = true) => {
const clearAuthStateAndRedirect = useCallback(() => {
setUserEmail(undefined);
setIsUserSignedInEager(false);
clearAuthEmailCookie();
if (shouldRedirect) { router.push(PATH_SIGN_IN); }
}, [router]);
if (isPathAdmin(pathname)) { router.push(PATH_SIGN_IN); }
}, [router, pathname]);
const startUpload = useCallback((onStart?: () => void) => {
if (uploadInputRef.current) {