how to use middleware function of next if next-auth already exports middleware
Answered
DhrishP1 posted this in #help-forum
DhrishP1OP
export { auth as middleware } from '@/auth'
export async function middleware(req:NextRequest){
console.log('hi')
}how can I use middleware function to do other things , as the docs show that only middleware function is used in middlware.ts
Answered by B33fb0n3
you can create a normal middleware (https://nextjs.org/docs/app/building-your-application/routing/middleware#example) and then check the auth inside your middleware like this:
Like that you have your old functionality, but you can still add stuff (either before your signed in check or after your signed in check)
const token = await getToken({req: request})
if(!token)
return redirectToSignIn(request.nextUrl, request.url)Like that you have your old functionality, but you can still add stuff (either before your signed in check or after your signed in check)
6 Replies
@DhrishP1
export { auth as middleware } from '@/auth'
export async function middleware(req:NextRequest){
console.log('hi')
}
how can I use middleware function to do other things , as the docs show that only middleware function is used in middlware.ts
you can create a normal middleware (https://nextjs.org/docs/app/building-your-application/routing/middleware#example) and then check the auth inside your middleware like this:
Like that you have your old functionality, but you can still add stuff (either before your signed in check or after your signed in check)
const token = await getToken({req: request})
if(!token)
return redirectToSignIn(request.nextUrl, request.url)Like that you have your old functionality, but you can still add stuff (either before your signed in check or after your signed in check)
Answer
@B33fb0n3 you can create a *normal* middleware (https://nextjs.org/docs/app/building-your-application/routing/middleware#example) and then check the auth inside your middleware like this:
const token = await getToken({req: request})
if(!token)
return redirectToSignIn(request.nextUrl, request.url)
Like that you have your *old* functionality, but you can still add stuff (either before your signed in check or after your signed in check)
DhrishP1OP
got it , thanks alot
@DhrishP1 got it , thanks alot
happy to help. Please mark solution
@B33fb0n3 happy to help. Please mark solution
DhrishP1OP
didnt get you