I’m using NextAuth for authentication and I’m using middleware to protect my routes. Whenever I visit the /contact
page, I’m getting redirected to the /api/auth/signin
page which is usual and expected behaviour. But when I’m authenticated, I’m still in the Sign In page. I checked my Network tab, where it showed that I was redirected back to /contact
page followed by /api/auth/signin
page.
Here’s my middleware.ts
// export { default } from 'next-auth/middleware'
import { NextRequest, NextResponse } from 'next/server'
import { withAuth } from 'next-auth/middleware'
import { getSession } from 'next-auth/react'
export default withAuth(
function middleware(req: NextRequest) {
console.log('middleware')
},
{
callbacks: {
authorized({ req, token }) {
if (token) return true
},
},
},
)
export const config = { matcher: ['/contact'] }
If I’m signing in, how do I get redirected to the /contact
page and not get redirected to the sign in page again? And even if I’m visiting the Sign In page even after authentication, how do I get redirected to the default callback URL?