When trying to access the login page already logged in redirect to dashboard
Unanswered
Thrianta posted this in #help-forum
ThriantaOP
import { getToken } from 'next-auth/jwt'
import { withAuth } from 'next-auth/middleware'
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export default withAuth(
async function middleware(request: NextRequest) {
const token = await getToken({
req: request,
secret: process.env.NEXTAUTH_SECRET
})
console.log(request.nextUrl.pathname)
if (!token) {
return NextResponse.redirect(new URL('/login', request.url))
} else {
return NextResponse.next()
}
},
{
callbacks: {
authorized: ({ req, token }) => {
if (token) return true
return false
}
},
pages: {
signIn: '/login',
signOut: '/login',
error: '/error'
}
}
)
export const config = {
matcher: [
'/((?!api|_next/static|_next/image|favicon.ico|/css/|/fonts/).*)',
'/dashboard/:path*',
'/login'
]
}1 Reply
Can you explain more about what you're trying to do?