Authentification
---
Authentication and Onboarding
Authentication in your project uses NextAuth.js, a comprehensive identity and session management framework. Here are the different aspects:
NextAuth Configuration
The file pages/api/auth/[...nextauth].ts configures the authentication options for NextAuth:
Session Management with JWT
- JWT (JSON Web Tokens): Session management is handled using
JWT, which securely stores session information in thetoken. - Callbacks:
session callback: When retrieving a session, additional user information (id,role,isOnboarded) is added tosession.user.jwt callback: When creating theJWTtoken, user information such asid,role, andisOnboardedis added.
- Role System:
- Roles (
user,admin, etc.) are assigned to users and managed via theJWTtoken. This system controls access and privileges on the platform.
- Roles (
Onboarding API
The file app/api/user/onboard/route.ts contains the onboarding API, which is called to update user information after the first login.
- User Authentication: Checks if the user is authenticated by retrieving the server session using
getServerSession. - Updating User Data: Updates user information in the database to mark onboarding as completed (
isOnboarded: true).
Onboarding Component: component/onboarding/Onboarding.tsx
The Onboarding.tsx component guides the user through the onboarding process after their first login. Here is the component code reorganized into sections for clarity:
Type Definitions and Imports
Progress Bar Component
Onboarding Steps Component
Onboarding form
Core Onboarding Component
Additional Explanations
ProgressBarComponent: Displays the user's progress through the onboarding process.OnboardingStepComponent: Represents an individual step in the onboarding process.OnboardingFormComponent: Form for entering user information (username and bio).OnboardingComponent: Manages the complete onboarding process, including step management and onboarding completion.
Explanation of the auth.ts File
The auth.ts file in /src/lib/auth provides utility functions for retrieving server-side authentication sessions. It uses getServerSession from next-auth to obtain the current user's session information. This file facilitates authentication and authorization in various contexts, such as API requests or pages requiring authentication.
Explanation of Main Functions
-
getAuthSession:- This function uses
getServerSessionto retrieve the current user session. It accepts parameters that can come from various request contexts (server-side or API). - It returns the authentication session without checking if the user is authenticated.
- This function uses
-
getRequiredAuthSession:- This function also retrieves the user session via
getServerSession, but it performs an additional check to ensure the user is authenticated. - If the session does not contain a valid user ID, it throws an "Unauthorized" error.
- It returns a session object enriched with required user information.
- This function also retrieves the user session via
Usage in the Application
getAuthSessioncan be used when you need to access the user session without requiring strict authentication, for example, to customize the content of a public page based on the logged-in user.getRequiredAuthSessionis used in routes or pages that require mandatory authentication, ensuring that only an authenticated user can access the resource.