how to protect route based on role

@bekacru I used to protect routes using getSessionCookie, but now I want to protect them based on user roles. I tried using getSession() but it seems not provided. How can I check a user's role to protect specific routes or actions? it's enough if i know how to get session
No description
Solution:
You can't use getSession in middleware. Take a look at our middleware docs: https://www.better-auth.com/docs/integrations/next#middleware You can directly fetch your auth API for session data. ...
Jump to solution
4 Replies
Solution
Ping
Ping•2w ago
You can't use getSession in middleware. Take a look at our middleware docs: https://www.better-auth.com/docs/integrations/next#middleware You can directly fetch your auth API for session data. We recommend not handling protective redirects in the middleware layer, and instead treat middleware redirects as optimistic redirects. Instead, we recommend to implement proper authorization redirects in each page/route.
dgrcode
dgrcode•2w ago
Instead, we recommend to implement proper authorization redirects in each page/route
Is there an example of this? I couldn't find anything in the docs
bekacru
bekacru•2w ago
This just mans in each page component or api route, you should check if the user is authroized to access that page or not instead of at a middleware leve on nextjs. Since middlewares aren't really meant to do that.
FikirYilkal
FikirYilkalOP•2w ago
thank you 🙌

Did you find this page helpful?