Get user role in Next.js middleware #290

Closed
opened 2026-03-13 07:40:54 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @franciscohanna92 on GitHub (Nov 27, 2024).

Is your feature request related to a problem? Please describe.
I want to protect routes under /admin based on the user role

Describe the solution you'd like
Access the user role in Next.js middleware.

Originally created by @franciscohanna92 on GitHub (Nov 27, 2024). **Is your feature request related to a problem? Please describe.** I want to protect routes under `/admin` based on the user role **Describe the solution you'd like** Access the user role in Next.js middleware.
Author
Owner

@Bekacru commented on GitHub (Nov 28, 2024):

You could simply check session.user.role === "admin". To infer role in user, make sure you infer the session type from auth.$Infer.Session instead of importing the Session type from Better Auth.

@Bekacru commented on GitHub (Nov 28, 2024): You could simply check `session.user.role === "admin"`. To infer `role` in `user`, make sure you infer the session type from `auth.$Infer.Session` instead of importing the `Session` type from Better Auth.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#290