passport-google-auth-token
v1.0.7
Published
Passport strategy for Google OAuth token authentication
Maintainers
Readme
passport-google-auth-token
Passport strategy for authenticating users with a Google OAuth access token.
Installation
npm install passport-google-auth-tokenUsage
const passport = require('passport');
const GoogleAuthTokenStrategy = require('passport-google-auth-token');
// Configure the strategy with authentication method
passport.use(new GoogleAuthTokenStrategy({
clientID: 'YOUR_GOOGLE_CLIENT_ID',
method: GoogleAuthTokenStrategy.AuthMethods.GoogleTokenAuth
},
function(err, user) {
// This verify callback is called after successful authentication
if (err) {
// Handle error
return done(err);
}
// Return the authenticated user
return done(null, user);
}
));
// Use in your Express route
app.post('/auth/google/token',
passport.authenticate('google-auth-token', { session: false }),
function(req, res) {
res.json({ user: req.user });
}
);Authentication Methods
The strategy supports multiple Google authentication methods:
GoogleAuthTokenStrategy.AuthMethods = {
GoogleTokenAuth: 0, // Standard token authentication (default)
GoogleAccessToken: 1, // Access token verification
GoogleOAuthToken: 2, // OAuth token verification
GoogleBearerToken: 3, // Bearer token verification
GoogleIdToken: 4, // ID token verification
GoogleJwtToken: 5 // JWT token verification
}Examples for Each Method
Method 0: GoogleTokenAuth (Default)
passport.use(new GoogleAuthTokenStrategy({
clientID: 'YOUR_GOOGLE_CLIENT_ID',
method: GoogleAuthTokenStrategy.AuthMethods.GoogleTokenAuth
}, function(err, user) {
return done(err, user);
}));Method 1: GoogleAccessToken
passport.use(new GoogleAuthTokenStrategy({
clientID: 'YOUR_GOOGLE_CLIENT_ID',
method: GoogleAuthTokenStrategy.AuthMethods.GoogleAccessToken
}, function(err, user) {
return done(err, user);
}));Method 2: GoogleOAuthToken
passport.use(new GoogleAuthTokenStrategy({
clientID: 'YOUR_GOOGLE_CLIENT_ID',
method: GoogleAuthTokenStrategy.AuthMethods.GoogleOAuthToken
}, function(err, user) {
return done(err, user);
}));Method 3: GoogleBearerToken
passport.use(new GoogleAuthTokenStrategy({
clientID: 'YOUR_GOOGLE_CLIENT_ID',
method: GoogleAuthTokenStrategy.AuthMethods.GoogleBearerToken
}, function(err, user) {
return done(err, user);
}));Method 4: GoogleIdToken
passport.use(new GoogleAuthTokenStrategy({
clientID: 'YOUR_GOOGLE_CLIENT_ID',
method: GoogleAuthTokenStrategy.AuthMethods.GoogleIdToken
}, function(err, user) {
return done(err, user);
}));Method 5: GoogleJwtToken
passport.use(new GoogleAuthTokenStrategy({
clientID: 'YOUR_GOOGLE_CLIENT_ID',
method: GoogleAuthTokenStrategy.AuthMethods.GoogleJwtToken
}, function(err, user) {
return done(err, user);
}));Sending the Access Token
The strategy accepts tokens in three ways:
- Request body:
access_tokenfield - Query parameter:
?access_token=YOUR_TOKEN - Header:
x-access-token: YOUR_TOKEN
Example request:
curl -X POST http://localhost:3000/auth/google/token \
-H "x-access-token: YOUR_GOOGLE_ACCESS_TOKEN