Complete setup guide for Google OAuth authentication.
Google OAuth is the most popular authentication method for web applications. This guide walks you through setting up Google OAuth with @warpy-auth-sdk/core.
Before you can use Google OAuth, you need to set up a project in Google Cloud Console:
http://localhost:3000/api/auth/callback/googlehttps://yourdomain.com/api/auth/callback/googleAdd the Google OAuth credentials to your environment variables:
# .env.local
AUTH_SECRET=your-secret-key-min-32-chars-long
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
GOOGLE_REDIRECT_URI=http://localhost:3000/api/auth/callback/googleImport and configure the Google OAuth provider:
Minimal configuration for Google OAuth
import { google } from '@warpy-auth-sdk/core';
const googleProvider = google({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
redirectUri: process.env.GOOGLE_REDIRECT_URI!,
});Google OAuth provider supports additional configuration options. PKCE (S256) is enabled by default.
Full configuration with custom scopes and options
import { google } from '@warpy-auth-sdk/core';
const googleProvider = google({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
redirectUri: process.env.GOOGLE_REDIRECT_URI!,
scope: [
'openid',
'email',
'profile',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile'
],
});Google OAuth supports various scopes for different permissions:
openid - OpenID Connect authenticationemail - Access to user's email addressprofile - Access to user's basic profile informationhttps://www.googleapis.com/auth/userinfo.email - Detailed email accesshttps://www.googleapis.com/auth/userinfo.profile - Detailed profile accesshttps://www.googleapis.com/auth/calendar - Google Calendar accesshttps://www.googleapis.com/auth/drive - Google Drive accessGoogle OAuth returns user data in this format:
interface GoogleUser {
id: string; // Google user ID
email: string; // User's email address
name: string; // User's full name
picture: string; // User's profile picture URL
verified_email: boolean; // Email verification status
locale: string; // User's locale
}Here's a complete example of Google OAuth integration:
Full implementation with error handling
import { authMiddleware } from '@warpy-auth-sdk/core/next';
import { google } from '@warpy-auth-sdk/core';
const handler = authMiddleware(
{
secret: process.env.AUTH_SECRET!,
provider: google({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
redirectUri: process.env.GOOGLE_REDIRECT_URI!,
scope: ['openid', 'email', 'profile'],
}),
callbacks: {
async user(u) {
// Save user to your database
// Return the user object that will be stored in the session
return {
id: u.id,
email: u.email,
name: u.name,
picture: u.picture,
};
},
jwt: (token) => {
// Add any custom claims to the JWT
return token;
},
session: (session) => {
// Customize the session object
return session;
},
},
},
{
basePath: '/api/auth',
successRedirect: '/dashboard',
errorRedirect: '/login',
}
);
export function proxy(request: NextRequest) {
const p = request.nextUrl.pathname;
if (p.startsWith('/api/auth')) return handler(request);
return NextResponse.next();
}Test your Google OAuth integration:
npm run devhttp://localhost:3000/api/auth/signin/googleHere are solutions to common Google OAuth issues:
Error: "redirect_uri_mismatch"
Solution: Ensure the redirect URI in your Google Cloud Console exactly matches the one in your environment variables.
Error: "invalid_client"
Solution: Check that your Client ID and Client Secret are correct and not expired.
Error: "access_denied"
Solution: Make sure your OAuth consent screen is properly configured and published.
For production deployment:
Now that you have Google OAuth working, you can: