Page flashing before authentication
Answered
Giant panda posted this in #help-forum
Giant pandaOP
page flashing for some reason on a route
60 Replies
Giant pandaOP
middleware.ts
export { default } from "next-auth/middleware";flashing page :
import { getSortedPostsData } from "@/utils/posts";
import { BlogTile } from "../components/BlogTile";
export default async () => {
const allPostsData = await getSortedPostsData();
return (
<div className="h-full flex flex-col space-y-20">
<div className="text-center text-5xl">All Blogs</div>
<div className="grid grid-flow-row gap-5 decoration-0 grid-cols-4 h-full grid-rows-3">
{allPostsData.map(({ id, date, title, about }) => (
<BlogTile key={id} id={id} title={title} date={date} about={about} />
))}
</div>
</div>
);
};Blog tile component :
import { PostsMetadata } from "../../utils/posts";
import Link from "next/link";
export const BlogTile = ({ id, title, date, about }: PostsMetadata) => {
return (
<Link
href={`/posts/${id}`}
className="col-span-1 row-span-1 dark:bg-slate-600 bg-slate-300"
>
<div
className="items-center h-full justify-center flex flex-col space-y-10 p-10 decoration-0"
>
<div className="flex flex-col items-start">
<div className="font-bold text-lg">{title}</div>
<div className="text-sm mt-3">Created: {date}</div>
</div>
<div className="font-semibold">{about}</div>
</div>
</Link>
);
};when i try to go to http://localhost:3000/posts i see the contents of posts for a sec and then get redirected to signin page of next-auth
this is when no session is there
In my opinion, it might not be the middleware flashing the page tbh
Giant pandaOP
then what ?
how can middleware allow to show contents of page if no session is there 🤔
Just in case. Try disabling the middleware
Giant pandaOP
then nothing will happen right ?
i will just see the contents
We trying to fix your flashing page here
So im just trynna see where the problem is at
Giant pandaOP
i mean if you disable middleware it will just show the contents of the posts page
without redirecting to signin
American Crocodile
Didn't nextjs docs address this very issue?
"prevent flash of unauthenticated content"
I'll get it 1 sec
Find in page "flash of unauthenticated content"
Thats for pages?
Giant pandaOP
the component is fully server side
no data fetching happening in client anyways
@Clown Thats for pages?
American Crocodile
Oh my bad lol
@Giant panda i mean if you disable middleware it will just show the contents of the posts page
Maybe i just dont know this enough but that literally shouldn't happen unless that middleware provided by nextauth has a problem.
The middleware is handled before rendering of the pages iirc
The middleware is handled before rendering of the pages iirc
American Crocodile
Yeah I was gonna suggest that
Or maybe it caches your previous session the revalidates it at the point it flashes
Try clerk
Giant pandaOP
i think it's related so me passing SessionProvider in layout component
i am trying few things and i'll let you know if i fix it
@Giant panda i think it's related so me passing SessionProvider in layout component
No no session provider should be in layout tho
So that shouldn't be an issue
Giant pandaOP
ya issue is with my middleware , it's not working actually at all 🤣
prolly i am doing some dumb stuff
Where is your middleware lol
American Crocodile
How is middleware not working 🤨
Middleware should be in the same directory as package.json
Answer
Giant pandaOP
inside app folder
Hm....
It should be at the same level as app folder
American Crocodile
It should be in ur root dir
Giant pandaOP
ya working fine now , skill issue , i am noob
ðŸ˜
@Clown It should be at the same level as app folder
American Crocodile
This
I really was pulling sherlock holmes here
Im dumb too
American Crocodile
@Giant panda league of legends PTSD flashbacks
Giant pandaOP
stupid logout button was redirecting me to singin page apparently
i feel too embarassed to stay online anymore
thanks for help @American Crocodile @Clown
ðŸ™
Which was the issue?
Middleware or signin button
Giant pandaOP
middleware
it wasn't working at all , i commented out my signout button and i was not getting to the signin page 🤣
"use client";
import { signOut, useSession } from "next-auth/react";
import { redirect } from "next/navigation";
export const SignoutButton = () => {
const { status } = useSession();
if (status === "unauthenticated") {
return redirect("/api/auth/signin");
}
return (
<div>
<button
onClick={() => {
console.log("here");
signOut();
}}
>
Signout
</button>
</div>
);
};so this basically flashed the page on client and then waits for session api to complete and then redirected me
Anyways mark the answer
Giant pandaOP
how ?
Original message was deleted