GuiSiebert
GuiSiebert
TTCTheo's Typesafe Cult
Created by GuiSiebert on 6/3/2024 in #questions
After logged in, how to redirect users back to the same URL they were before logging in?
My app sells event tickets and I'm using NextAuth. Unauthenticated users can go as far as selecting an event and then selecting the tickets they want. Once the ticket is selected, the "continue" button leads the user to "/tickets/register". In that page I verifiy if the user is authenticated. If it's not, it is redirected to: "/api/auth/signin" But after logging in, users are being redirected back to "/", when I actually want them to be redirected back to the same page they were before ("/api/auth/signin"). I obviously don't want that URL to be the standard after every authentication. It should only happen in this registering flow. (As some users might decide to log in from the homepage or anywhere else in the app). One thing I have tried was to redirect unauthenticated users this way:
const session = await getServerAuthSession();
if (!session?.user) {
redirect(`/api/auth/signin?url=${env.NEXTAUTH_URL}/tickets/register`);
}
const session = await getServerAuthSession();
if (!session?.user) {
redirect(`/api/auth/signin?url=${env.NEXTAUTH_URL}/tickets/register`);
}
Which in my dev environment took me to: http://localhost:3000/api/auth/signin?url=http%3A%2F%2Flocalhost%3A3000%2F%2Ftickets%2Fregister However it still leads me back to homepage "/" after logging in. Does anyone have a better idea?
4 replies
TTCTheo's Typesafe Cult
Created by GuiSiebert on 3/19/2024 in #questions
SessionProvider missing in app router
I've got a t3 project using next 14 with next-auth and the "new" app router. It was created yesterday (Mar-19-2024). According to the docs (next-auth page):
In your app’s entrypoint, you’ll see that your application is wrapped in a SessionProvider.
However, I don't see any <SessionProvider> in my app router project. And I'm pretty sure it is used in pages router t3 projects. Is this intentional? I do believe I'm getting or trying something wrong. Maybe I should avoid using useSession() ? Or is it ok if I wrap my application with a <SessionProvider> in ./src/app/layout.tsx? Every comment is appreciated and I'm sure other noobs will be curious about that too!
4 replies