[GH-ISSUE #678] Get user role in Next.js middleware #8374

Closed
opened 2026-04-13 03:26:50 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @franciscohanna92 on GitHub (Nov 27, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/678

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). Original GitHub issue: https://github.com/better-auth/better-auth/issues/678 **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.
GiteaMirror added the locked label 2026-04-13 03:26:50 -05:00
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.

<!-- gh-comment-id:2505314646 --> @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#8374