OAuth Authentication
The OAuth plugin enables OAuth2-based authentication in AdminForth, allowing users to sign in using their Google, GitHub, Facebook or other OAuth2 provider accounts.
Installation
To install the plugin:
npm install @adminforth/oauth --save
npm install @adminforth/google-oauth-adapter --save # for Google OAuth
Configuration
This section provides a step-by-step guide to configure the OAuth plugin for Google authentication. See OAuth2 Providers for other providers.
1. OAuth Provider Setup (Google Example)
You need to get the client ID and client secret from your OAuth2 provider.
- Go to the Google Cloud Console and log in.
- Create a new project or select an existing one
- Go to
APIs & Services
→Credentials
- Create credentials for OAuth 2.0 client IDs
- Select application type: "Web application"
- Add your application's name and redirect URI
- In "Authorized redirect URIs", add next URI:
https://your-domain/oauth/callback
,http://localhost:3500/oauth/callback
. Please remember to include BASE_URL in the URI if you are using it in project e.g.https://your-domain/base/oauth/callback
- Add the credentials to your
.env
file:
GOOGLE_OAUTH_CLIENT_ID=your_google_client_id
GOOGLE_OAUTH_CLIENT_SECRET=your_google_client_secret
2. Plugin Configuration
Configure the plugin in your user resource file:
import OAuthPlugin from '@adminforth/oauth';
import AdminForthAdapterGoogleOauth2 from '@adminforth/google-oauth-adapter';
declare global {
namespace NodeJS {
interface ProcessEnv {
GOOGLE_OAUTH_CLIENT_ID: string;
GOOGLE_OAUTH_CLIENT_SECRET: string;
}
}
}
// ... existing resource configuration ...
plugins: [
new OAuthPlugin({
adapters: [
new AdminForthAdapterGoogleOauth2({
clientID: process.env.GOOGLE_OAUTH_CLIENT_ID,
clientSecret: process.env.GOOGLE_OAUTH_CLIENT_SECRET,
}),
],
emailField: 'email', // Required: field that stores the user's email
}),
]
3. Email Confirmation
The plugin supports automatic email confirmation for OAuth users. To enable this:
- Add the
email_confirmed
field to your database schema:
model adminuser {
// ... existing fields ...
email_confirmed Boolean @default(false)
}
- Run the migration:
npx prisma migrate dev --name add-email-confirmed-to-adminuser
- Configure the plugin with
emailConfirmedField
:
new OAuthPlugin({
// ... adapters configuration ...
emailField: 'email',
emailConfirmedField: 'email_confirmed' // Enable email confirmation tracking
}),
When using OAuth:
- New users will have their email automatically confirmed (
email_confirmed = true
) - Existing users will have their email marked as confirmed upon successful OAuth login
- The
email_confirmed
field must be a boolean type
4. Open Signup
By default, users must exist in the system before they can log in with OAuth. You can enable automatic user creation for new OAuth users with the openSignup
option:
new OAuthPlugin({
// ... adapters configuration ...
openSignup: {
enabled: true,
defaultFieldValues: { // Set default values for new users
role: 'user',
},
},
}),
5. UI Customization
You can customize the UI of the OAuth login buttons by using the iconOnly
and pill
options.
new OAuthPlugin({
// ... adapters configuration ...
iconOnly: true, // Show only provider icons without text
pill: true, // Use pill-shaped buttons instead of rectangular
}),
OAuth2 Providers
Facebook Adapter
Install Adapter:
npm install @adminforth/facebook-oauth-adapter --save
- Go to the Facebook Developers
- Go to
My apps
- Create a new project or select an existing one (choose Authenticate and request data from users with Facebook Login)
- Go to
Use Cases
-Authenticate and request data from users with Facebook Login
->Customize
and add email permissions - Go to
App Settings
->Basic
- Get App ID and App secret
- Add the credentials to your
.env
file:
FACEBOOK_OAUTH_CLIENT_ID=your_facebook_client_id
FACEBOOK_OAUTH_CLIENT_SECRET=your_facebook_client_secret
Add the adapter to your plugin configuration:
import AdminForthAdapterFacebookOauth2 from '@adminforth/facebook-oauth-adapter';
// ... existing resource configuration ...
plugins: [
new OAuthPlugin({
adapters: [
...
new AdminForthAdapterFacebookOauth2({
clientID: process.env.FACEBOOK_OAUTH_CLIENT_ID,
clientSecret: process.env.FACEBOOK_OAUTH_CLIENT_SECRET,
}),
],
}),
]
Need custom provider?
Just fork any existing adapter e.g. Google and adjust it to your needs.
This is really easy, you have to change several then 10 lines of code in this file
Then just publish it to npm and install it in your project.