Skip to main content

OAuth / Social Recipe

node-auth supports Google, GitHub, and any custom OAuth 2.0 provider via GenericOAuthStrategy.


OAuth flow

Account conflicts

If the OAuth provider email matches an existing local account, node-auth returns a 409 Conflict. Use IPendingLinkStore to resolve conflicts automatically. See Account Linking.


Step 1: Configure your OAuth app

Register your application in the provider's developer console and note the Client ID and Client Secret. Set the callback URL to:

https://yourapp.com/auth/oauth/<provider>/callback

Step 2: Create a strategy

import { GoogleStrategy } from '@nik2208/node-auth';

const googleStrategy = new GoogleStrategy({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
callbackUrl: 'https://yourapp.com/auth/oauth/google/callback',
findOrCreateUser: async (profile) => {
// Always use providerAccountId + provider for lookup — never email alone
// (prevents account-takeover via email spoofing)
let user = await userStore.findByProviderAccount?.('google', profile.id);
if (!user) {
user = await userStore.create({
email: profile.email,
firstName: profile.name,
providerAccountId: profile.id,
loginProvider: 'google',
isEmailVerified: true,
});
}
return user;
},
});

Step 3: Mount the router

app.use('/auth', auth.router({
googleStrategy,
githubStrategy,
// or for custom providers:
oauthStrategies: [discordStrategy],
}));

Add this to your IUserStore to enable safe provider-based lookup (avoids email-based account takeover):

async findByProviderAccount(provider: string, providerAccountId: string): Promise<BaseUser | null> {
return db('users').where({ loginProvider: provider, providerAccountId }).first() ?? null;
}

OAuth Endpoints

MethodPathDescription
GET/auth/oauth/googleRedirect to Google OAuth
GET/auth/oauth/google/callbackGoogle OAuth callback
GET/auth/oauth/githubRedirect to GitHub OAuth
GET/auth/oauth/github/callbackGitHub OAuth callback
GET/auth/oauth/:nameRedirect to custom provider
GET/auth/oauth/:name/callbackCustom provider callback