Getting the current url on ssr layout
Answered
American black bear posted this in #help-forum
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.
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>;
}5 Replies
Giant Angora
hi @American black bear
you need to use
you need to use
middlweareAnswer
@Giant Angora hi <@622469622372892726>
you need to use `middlweare`
American black bearOP
damn okay, thank you. I was trying to avoid that as it will end up costing more. Thanks anyway 🙂
Giant Angora
mark it as your answer for others
@American black bear 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.
ts
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>;
}
Korat
btw remember how nextjs work to don't leak data using layout auth checks
https://youtu.be/EGDD0rlBd8Q?si=9voian8J_mqTs-kd
https://youtu.be/EGDD0rlBd8Q?si=9voian8J_mqTs-kd
@Korat btw remember how nextjs work to don't leak data using layout auth checks
https://youtu.be/EGDD0rlBd8Q?si=9voian8J_mqTs-kd
American black bearOP
Oh wow... thanks 😵💫