How to use the Next _middleware with next-auth?
Unanswered
Capelin posted this in #help-forum
CapelinOP
In the documentation @ https://next-auth.js.org/tutorials/securing-pages-and-api-routes
it lists the following example:
It states:
export { default } from "next-auth/middleware"
export const config = { matcher: ["/dashboard"] }
Will protect the pages /dashboard from unauthed users.
However,
What if I want to protect a page from unauthed users?
What is the lowest overhead way to check if a user is authed with JWT in the middleware - while maintaining the option to run conditionals like:
if (!authToken){
redirect("/home")
}
it lists the following example:
It states:
export { default } from "next-auth/middleware"
export const config = { matcher: ["/dashboard"] }
Will protect the pages /dashboard from unauthed users.
However,
What if I want to protect a page from unauthed users?
What is the lowest overhead way to check if a user is authed with JWT in the middleware - while maintaining the option to run conditionals like:
if (!authToken){
redirect("/home")
}
5 Replies
@Capelin In the documentation @ https://next-auth.js.org/tutorials/securing-pages-and-api-routes
it lists the following example:
It states:
export { default } from "next-auth/middleware"
export const config = { matcher: ["/dashboard"] }
Will protect the pages /dashboard from unauthed users.
However,
What if I want to protect a page from unauthed users?
What is the lowest overhead way to check if a user is authed with JWT in the middleware - while maintaining the option to run conditionals like:
if (!authToken){
redirect("/home")
}
You can use middleware with a single page iirc. You'll have to check the docs for the pattern matching..
Other than that im pretty sure next auth has hooks like
Other than that im pretty sure next auth has hooks like
useSession() or something that you can use to check if the user is signed in or not, then redirect them away.@Clown You can use middleware with a single page iirc. You'll have to check the docs for the pattern matching..
Other than that im pretty sure next auth has hooks like `useSession()` or something that you can use to check if the user is signed in or not, then redirect them away.
CapelinOP
But is useSession() the recommended way to check for logged in user in the middleware?
Since the recommendation is to use export {default} from middleware, I would expect the recommended feature to also be export from middleware.
Since the recommendation is to use export {default} from middleware, I would expect the recommended feature to also be export from middleware.
CapelinOP
I see, I want to handle the redirect in the middleware.
I tried to use useSession for my design, but it forced me to use HoC which is not compatible with some of the components im using
I tried to use useSession for my design, but it forced me to use HoC which is not compatible with some of the components im using