fixes auth?
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
import { useAuth } from "react-oidc-context";
|
||||
import { useState, useEffect } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useRouter, usePathname } from "next/navigation";
|
||||
import { Source_Sans_3, Nunito } from "next/font/google";
|
||||
|
||||
const nunito = Nunito({
|
||||
@@ -11,6 +11,7 @@ const nunito = Nunito({
|
||||
export default function AuthStatus() {
|
||||
const auth = useAuth();
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
const [persistentAuthState, setPersistentAuthState] = useState<{
|
||||
isAuthenticated: boolean;
|
||||
user: any;
|
||||
@@ -32,6 +33,22 @@ export default function AuthStatus() {
|
||||
}
|
||||
}, [auth.isLoading, auth.isAuthenticated, auth.user, auth.error]);
|
||||
|
||||
useEffect(() => {
|
||||
// Redirect to / if user is definitely not logged in and not already on /
|
||||
if (
|
||||
persistentAuthState.hasInitialized &&
|
||||
!persistentAuthState.isAuthenticated &&
|
||||
pathname !== "/"
|
||||
) {
|
||||
router.push("/");
|
||||
}
|
||||
}, [
|
||||
persistentAuthState.hasInitialized,
|
||||
persistentAuthState.isAuthenticated,
|
||||
pathname,
|
||||
router,
|
||||
]);
|
||||
|
||||
// Show loading only on first load, not on subsequent navigations
|
||||
if (!persistentAuthState.hasInitialized && auth.isLoading) {
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user