|
| 1 | +# OAuth Authentication |
| 2 | + |
| 3 | +The OAuth plugin enables OAuth2-based authentication in AdminForth, allowing users to sign in using their Google, GitHub, or other OAuth2 provider accounts. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +To install the plugin: |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install @adminforth/oauth --save |
| 11 | +npm install @adminforth/google-oauth-adapter --save # for Google OAuth |
| 12 | +``` |
| 13 | + |
| 14 | +## Configuration |
| 15 | + |
| 16 | +### 1. OAuth Provider Setup |
| 17 | + |
| 18 | +You need to get the client ID and client secret from your OAuth2 provider. |
| 19 | + |
| 20 | +For Google: |
| 21 | +1. Go to the [Google Cloud Console](https://console.cloud.google.com) |
| 22 | +2. Create a new project or select an existing one |
| 23 | +3. Go to "APIs & Services" → "Credentials" |
| 24 | +4. Create credentials for OAuth 2.0 client IDs |
| 25 | +5. Select application type: "Web application" |
| 26 | +6. Add your application's name and redirect URI |
| 27 | +7. Set the redirect URI to `http://your-domain/oauth/callback` |
| 28 | +8. Add the credentials to your `.env` file: |
| 29 | + |
| 30 | +```bash |
| 31 | +GOOGLE_CLIENT_ID=your_google_client_id |
| 32 | +GOOGLE_CLIENT_SECRET=your_google_client_secret |
| 33 | +``` |
| 34 | + |
| 35 | +### 2. Plugin Configuration |
| 36 | + |
| 37 | +Configure the plugin in your user resource file: |
| 38 | + |
| 39 | +```typescript title="./resources/adminuser.ts" |
| 40 | +import OAuth2Plugin from '@adminforth/oauth'; |
| 41 | +import AdminForthAdapterGoogleOauth2 from '@adminforth/google-oauth-adapter'; |
| 42 | + |
| 43 | +// ... existing resource configuration ... |
| 44 | + |
| 45 | +plugins: [ |
| 46 | + new OAuth2Plugin({ |
| 47 | + adapters: [ |
| 48 | + new AdminForthAdapterGoogleOauth2({ |
| 49 | + clientID: process.env.GOOGLE_CLIENT_ID, |
| 50 | + clientSecret: process.env.GOOGLE_CLIENT_SECRET, |
| 51 | + redirectUri: 'http://localhost:3000/oauth/callback', |
| 52 | + }), |
| 53 | + ], |
| 54 | + emailField: 'email', // Required: field that stores the user's email |
| 55 | + emailConfirmedField: 'email_confirmed' // Optional: field to track email verification |
| 56 | + }), |
| 57 | +] |
| 58 | +``` |
| 59 | + |
| 60 | +### 3. Email Confirmation |
| 61 | + |
| 62 | +The plugin supports automatic email confirmation for OAuth users. To enable this: |
| 63 | + |
| 64 | +1. Add the `email_confirmed` field to your database schema: |
| 65 | + |
| 66 | +```prisma title='./schema.prisma' |
| 67 | +model adminuser { |
| 68 | + // ... existing fields ... |
| 69 | + email_confirmed Boolean @default(false) |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +2. Run the migration: |
| 74 | + |
| 75 | +```bash |
| 76 | +npx prisma migrate dev --name add-email-confirmed-to-adminuser |
| 77 | +``` |
| 78 | + |
| 79 | +3. Configure the plugin with `emailConfirmedField`: |
| 80 | + |
| 81 | +```typescript title="./resources/adminuser.ts" |
| 82 | +new OAuth2Plugin({ |
| 83 | + // ... adapters configuration ... |
| 84 | + emailField: 'email', |
| 85 | + emailConfirmedField: 'email_confirmed' // Enable email confirmation tracking |
| 86 | +}), |
| 87 | +``` |
| 88 | + |
| 89 | +When using OAuth: |
| 90 | +- New users will have their email automatically confirmed (`email_confirmed = true`) |
| 91 | +- Existing users will have their email marked as confirmed upon successful OAuth login |
| 92 | +- The `email_confirmed` field must be a boolean type |
0 commit comments