NextAuth keeps deleting session?
Answered
Bighead carp posted this in #help-forum
Bighead carpOP
Code:
And I have one more client code: (RequireAuth wrapper)
My guess is it's due to
https://pastebin.com/xQuib4Y5 -> Next handlerAnd I have one more client code: (RequireAuth wrapper)
"use client";
import { signOut, useSession } from "next-auth/react";
import { useRouter } from "next/navigation";
import { useEffect } from "react";
export function RequireAuth({ children }: { children: React.ReactNode }) {
const router = useRouter();
const { data: session } = useSession();
useEffect(() => {
console.log(session);
const performSessionCheck = async () => {
if (!session || session.error === "RefreshAccessTokenError") {
await signOut({ redirect: false });
console.log("redirecting to signin fault refresh token?");
console.log(session);
router.push("/auth/signin");
}
};
performSessionCheck();
}, [router, session]);
return <>{children}</>;
}My guess is it's due to
useSession hook as it returns null, probably since it did not load data yetAnswered by Bighead carp
export function RequireAuth({ children }: { children: React.ReactNode }) {
const router = useRouter();
const { data: session, status } = useSession();
useEffect(() => {
console.log(session);
const performSessionCheck = async () => {
if (status == "unauthenticated" || (status !== "loading" && session!.error === "RefreshAccessTokenError")) {
await signOut({ redirect: false });
console.log("redirecting to signin fault refresh token?");
router.push("/auth/signin");
}
};
performSessionCheck();
}, [router, status, session]);
return <>{children}</>;
}Ok, I changed to this, and it seems to work now.
1 Reply
Bighead carpOP
export function RequireAuth({ children }: { children: React.ReactNode }) {
const router = useRouter();
const { data: session, status } = useSession();
useEffect(() => {
console.log(session);
const performSessionCheck = async () => {
if (status == "unauthenticated" || (status !== "loading" && session!.error === "RefreshAccessTokenError")) {
await signOut({ redirect: false });
console.log("redirecting to signin fault refresh token?");
router.push("/auth/signin");
}
};
performSessionCheck();
}, [router, status, session]);
return <>{children}</>;
}Ok, I changed to this, and it seems to work now.
Answer