Next.js Discord

Discord Forum

Getting the current url on ssr layout

Answered
American black bear posted this in #help-forum
Open in Discord
American black bearOP
Hello,

I'm wondering if it is possible to get the current URL that the user is on inside of a SSR rendered layout?
The goal is to check if the user is signed in, and if they aren't redirect them to my signIn page. However, I want to keep the users page in mind so when they login it will redirect them to the original page that they were on.

export default async function RootLayout({ children }: Readonly<{ children: React.ReactNode }>) {
    const session = await getServerSession(authOptions);
    if (!session?.user) {
        redirect(`/signin?page=${page}`);
    }

    return <div>{children}</div>;
}
Answered by Giant Angora
hi @American black bear
you need to use middlweare
View full answer

5 Replies