refactor(auth): redesign authentication and identity models#481
Merged
Conversation
|
@Harxhit is attempting to deploy a commit to the Prashantkumar Khatri's projects Team on Vercel. A member of the Team first needs to authorize it. |
CI — All Checks PassedBackend — PASS
Mobile — SKIP
Web — SKIP
Last updated: |
Collaborator
|
The checks are failing, needs to fix this before merge. |
Collaborator
Author
The problem is with the workflow I will make changes. |
Signed-off-by: Harshit <harsxit04@gmail.com>
Collaborator
Author
|
Need to add cron job for the revoked refresh_tokens. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #519
Redesign the authentication system to support multiple authentication providers, secure session management, refresh token rotation, and future authentication methods such as OAuth, email/password, and phone-based login.
This PR introduces a dedicated identity layer, refresh token infrastructure, account verification fields, role management, and OAuth-ready authentication flows.
Context
The previous authentication architecture stored provider-specific authentication data directly on the
Usermodel. This made it difficult to support multiple authentication methods for a single account and limited future extensibility.This redesign separates authentication concerns from user profile data by introducing a dedicated identity model and a more robust session management system.
The new design provides a scalable foundation for:
Authentication Flow
Login
Session Management
Access and refresh tokens are now handled separately.
Access Token
Refresh Token
Refresh Token Rotation
Benefits:
Schema Changes
Roles
Introduced role-based account support.
Default role:
User Identity Layer
Added a dedicated
UserIdentitymodel.Responsibilities:
Constraints:
(provider, providerId)userIdBenefits:
User Model Improvements
Added:
emailVerified
Tracks whether a user's email has been verified.
phoneNumber
Supports future phone authentication flows.
lastSignInAt
Records the most recent successful login.
isActive
Supports account activation, suspension, and deactivation.
Refresh Token Model Improvements
Enhanced refresh token storage for secure session management.
Added:
family
Groups tokens belonging to the same login session.
tokenHash
Stores hashed refresh tokens instead of raw values.
userAgent
Tracks device and browser information.
ipHash
Stores a hashed representation of the client IP address.
Benefits:
Security Improvements
OAuth State Validation
Added CSRF protection through OAuth state verification.
Token Hashing
Refresh tokens are never stored in plaintext.
Session Tracking
Sessions now track:
Refresh Token Rotation
Refresh tokens are rotated on use and old tokens are revoked.
Account Linking Foundation
Authentication providers can be linked to a single user account through identities.
Future Work
Proofs
Screen.Recording.2026-06-11.171421.mp4