This repository contains a Next.js application that serves as a centralized authentication service for multiple client applications (e.g., chat.com
and sora.com
). It leverages NextAuth.js for authentication and integrates with a database using Prisma. The app supports multiple authentication providers, secure password handling, and seamless redirection back to client apps.
- OAuth Providers: Login with Google and GitHub.
- Credentials-Based Authentication: Login using email and password.
- Prisma Integration: Database support for managing users, sessions, and accounts.
- JWT Sessions: Stateless, secure, and scalable session management.
- Custom Authentication Pages: Tailored login and error pages.
- Callbacks for Custom Behavior: Extendable JWT and session management.
-
Client Application Initiates Login:
- The client app redirects the user to this centralized authentication service.
-
Authentication Service Handles Login:
- The app authenticates the user via Google, GitHub, or credentials.
-
Redirection Back to Client App:
- After successful authentication, the user is redirected back to the client application with a session token.
-
Token Validation:
- The client app validates the token with the authentication service.
-
Clone the repository:
git clone https://github.com/your-repo/centralized-auth.git cd centralized-auth
-
Install dependencies:
npm install
-
Configure environment variables:
- Create a
.env
file at the root of the project and add the following variables:DATABASE_URL=your-database-url GOOGLE_CLIENT_ID=your-google-client-id GOOGLE_CLIENT_SECRET=your-google-client-secret GITHUB_CLIENT_ID=your-github-client-id GITHUB_CLIENT_SECRET=your-github-client-secret NEXTAUTH_SECRET=your-secret-key
- Create a
-
Run database migrations:
npx prisma migrate dev
-
Start the development server:
npm run dev
/pages/api/auth/[...nextauth].ts
: NextAuth.js configuration file./pages/auth
: Custom authentication pages (e.g., login)./prisma
: Prisma schema and database management./components
: Reusable UI components.
-
Client App Integration:
- Redirect users to the centralized auth service for login.
- Provide a
redirect_uri
query parameter to specify where users should be redirected after authentication.
Example:
https://auth.open.ai/api/auth/signin?redirect_uri=https://chat.com/callback
-
Token Validation:
- Use the provided session token to authenticate API requests or maintain user sessions on the client app.
This project is licensed under the MIT License.