Report: Implementing Role-Based Authentication with Google OAuth using Auth.js v5 #12837
Unanswered
mohammddarwesh
asked this question in
Help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Overview
This solution implements role‑based authentication using Google OAuth via Auth.js (NextAuth.js v5 beta) in a Next.js application. The user is allowed to choose a role at sign‑up, and that role is securely passed to the database using a cookie. Additionally, the solution uses a new database field,
isRoleLocked
, to ensure that the role is only set during the first sign‑up and cannot be altered unintentionally. The following sections describe the components and steps involved.1. Environment and Dependencies
Environment Variables
The application requires the following environment variables:
Dependencies
Install the necessary packages:
2. Database Setup with Prisma
Prisma Schema Changes
Extend your Prisma schema to include the following fields in the
User
model:role
: to store the chosen role.isRoleLocked
: a boolean flag to indicate if the role has been set permanently.File:
prisma/schema.prisma
After modifying your schema, run:
3. Cookie Service (Simplified)
Since we’re simplifying by removing encryption, we use the plain
js-cookie
library for the client-side cookie management.File:
lib/cookies.ts
4. Role Validator and Public Types
a. Public Types
Define an enum for roles for type safety.
File:
types/index.ts
b. Type Augmentation for Auth.js
Augment NextAuth types to include a custom
role
field.File:
types/next-auth.d.ts
Ensure your
tsconfig.json
includes these types.5. Auth.js Configuration
The configuration is split into a common configuration for edge compatibility and a full configuration that uses Prisma.
a. Common Configuration
File:
auth.config.ts
b. Full Configuration with Adapter and Sign-In Logic
This configuration merges the common config with the Prisma adapter and implements a sign‑in callback that reads the role from a cookie, validates it, and updates the user record in the database. It also sets
isRoleLocked
so that the role cannot be changed once set.File:
auth.ts
c. API Route for Auth.js
Ensure your authentication API route runs on Node.js (for Prisma compatibility).
File:
app/api/auth/[...nextauth]/route.ts
6. Custom Sign-In Page
This page lets the user choose a role (e.g., CUSTOMER, ADMIN, SELLER) and saves it in a cookie before initiating the Google OAuth flow.
File:
app/auth/signin/page.tsx
7. Minimal Middleware (Optional)
If you need middleware that protects routes, you can create a minimal middleware file (which runs on Edge and uses only core functionality).
File:
middleware.ts
Summary
Prisma & Database:
Your Prisma schema includes fields for
role
andisRoleLocked
. The Prisma client is initialized inlib/prisma.ts
.Encryption & Cookie Service:
(Optional encryption removed) – Cookie service in
lib/cookies.ts
stores the chosen role plainly.Type Safety & Role Validation:
types/index.ts
defines aUserRole
enum.types/next-auth.d.ts
augments Auth.js types to includerole
.lib/roleValidator.ts
validates roles.Auth.js Configuration:
auth.config.ts
holds the common configuration (edge-compatible).auth.ts
merges the common config with Prisma adapter and sign‑in logic that reads the role from a cookie, validates it, updates/creates the user record, and locks the role.Custom Sign-In Page:
The sign‑in page allows users to choose a role before authenticating via Google, storing the role in a cookie.
Middleware:
A minimal middleware example is provided.
This solution ensures that during the first sign‑in, the user's chosen role (from the sign‑in page) is passed via a cookie to the Auth.js sign‑in callback, where it’s validated and stored in the database along with an
isRoleLocked
flag to prevent further changes. Feel free to share and adjust this solution as needed.Beta Was this translation helpful? Give feedback.
All reactions