NextAuth + Nextjs 13 infinite redirection
Answered
Tibetan Terrier posted this in #help-forum
Tibetan TerrierOP
Hey! I am setting up auth with nextauth. Here are a few details:
1. Using Postgres with prisma adapter
2. Using discord as provider
middleware.ts (placed in the src directory, works):
sign-in.tsx
I think what is happening is:
I login and the useEffect in sign in fires redirecting me to the dashboard but the middleware cannot fetch the session and it redirects me back to the sign-in back but I am logged in so I again redirected and it continues.
Am I using the middleware correct and can it be used in my current nextauth settings?
1. Using Postgres with prisma adapter
2. Using discord as provider
middleware.ts (placed in the src directory, works):
export { default } from "next-auth/middleware"
export const config = { matcher: ["/dashboard", "/order"] }sign-in.tsx
'use client';
import {Button} from "@acme/ui";
import {signIn, useSession} from "next-auth/react";
import {useRouter} from "next/navigation";
import {useEffect} from "react";
export default function SignInPage() {
const {data: session} = useSession()
const router = useRouter()
useEffect(() => {
if (session) {
console.log("session", session)
router.push("/dashboard")
}
}, [session]);
return (
<div className="flex flex-col items-center justify-center min-h-screen py-12 px-4 sm:px-6 lg:px-8">
<Button onClick={async () => {
try {
await signIn("discord")
router.push("/dashboard")
} catch (e) {
console.error(e)
}
}} variant="outline">
<DiscordIcon className="mr-2 h-4 w-4"/>
Sign In With Discord
</Button>
</div>
)
}I think what is happening is:
I login and the useEffect in sign in fires redirecting me to the dashboard but the middleware cannot fetch the session and it redirects me back to the sign-in back but I am logged in so I again redirected and it continues.
Am I using the middleware correct and can it be used in my current nextauth settings?
Answered by fuma
1. Normally, Yes. NextAuth handles it under the hood.
2. As I know, no
3. Nothing is changed, despite the session is now stored in a JWT token rather than the database.
2. As I know, no
3. Nothing is changed, despite the session is now stored in a JWT token rather than the database.
12 Replies
Can you show your current configuration of next auth?
You need
You need
jwt strategy in order to use middlewareTibetan TerrierOP
damn then i can’t use middleware i am using sessions. Can you recommend what can I use with this config:
https://gist.github.com/imprakharshukla/a4e3d554a4da57944885599a6cbab6e3
https://gist.github.com/imprakharshukla/a4e3d554a4da57944885599a6cbab6e3
It doesn't matter what you are using. Normally, we won't use the
db strategy. JWT strategy works greatLearn more about [session option](https://next-auth.js.org/configuration/options#session)
Tibetan TerrierOP
huh makes sense, im sorry im kinda new to next auth. So i can use jwt sessions with providers as well.
1. Is it a plug and play replacement since ive built a significant portion of the app.
2. Are there any drawbacks against using database
3. Do things like useSession and getServerSession remain the same?
1. Is it a plug and play replacement since ive built a significant portion of the app.
2. Are there any drawbacks against using database
3. Do things like useSession and getServerSession remain the same?
@fuma It doesn't matter what you are using. Normally, we won't use the `db` strategy. JWT strategy works great
Tibetan TerrierOP
also if you dont mind me asking, how can I implement what i wanted to do with sessions stored in the db?
Just useEffects to check the change in the session from useSession?
Just useEffects to check the change in the session from useSession?
@Tibetan Terrier huh makes sense, im sorry im kinda new to next auth. So i can use jwt sessions with providers as well.
1. Is it a plug and play replacement since ive built a significant portion of the app.
2. Are there any drawbacks against using database
3. Do things like useSession and getServerSession remain the same?
1. Normally, Yes. NextAuth handles it under the hood.
2. As I know, no
3. Nothing is changed, despite the session is now stored in a JWT token rather than the database.
2. As I know, no
3. Nothing is changed, despite the session is now stored in a JWT token rather than the database.
Answer
@Tibetan Terrier also if you dont mind me asking, how can I implement what i wanted to do with sessions stored in the db?
Just useEffects to check the change in the session from useSession?
with
useEffect, you can't protect your page because it has been sent to the client before you redirecting the user to sign in pageInstead, check it in your server component or
getServerSideProps with getServerSessionTibetan TerrierOP
ah that makes so much sense
thank you so much :)