Passport strategy for using G+ Sign-In
npm install passport-google-plusPassport strategies for authenticating with the
Google+ Sign-In button.
This module lets you authenticate using Google in your Node.js applications.
By plugging into Passport, Google+ Sign-In can be easily and
unobtrusively integrated into any application or framework that supports
Connect-style middleware, including
Express.
$ npm install passport-google-plus
In the Google Developers Console make sure you have enabled the Google Plus API, otherwise your calls will fail (seen in this issue).
#### Configure Strategy
The strategy accepts a callback which is called after the user has been authenticated. The
profile and OAuth credentials can be saved or mapped to a user record.
``js
var GooglePlusStrategy = require('passport-google-plus');
passport.use(new GooglePlusStrategy({
clientId: 'YOUR_CLIENT_ID',
clientSecret: 'YOUR_CLIENT_SECRET'
},
function(tokens, profile, done) {
// Create or update user, call done() when complete...
done(null, profile, tokens);
}
));
`
#### Configure Google+ Sign-In Button
`html`
data-scope="https://www.googleapis.com/auth/plus.login"
data-clientid="YOUR_CLIENT_ID"
data-redirecturi="postmessage"
data-accesstype="offline"
data-cookiepolicy="single_host_origin"
data-callback="signInCallback">
#### Handle the callback & forward the authorization code
`js`
function signInCallback(authResult) {
if (authResult.code) {
$.post('/auth/google/callback', { code: authResult.code})
.done(function(data) {
$('#signinButton').hide();
});
} else if (authResult.error) {
console.log('There was an error: ' + authResult.error);
}
};
#### Authenticate Requests
Use passport.authenticate(), specifying the 'google' strategy, to
authenticate requests.
For example, as route middleware in an Express
application:
`js`
app.post('/auth/google/callback', passport.authenticate('google'), function(req, res) {
// Return user back to client
res.send(req.user);
});
Client-side flows are also supported for web & mobile using ID tokens. When using ID tokens, profile
data is limited to public information.
#### Configure Strategy
The strategy accepts a callback which is called after the user has been authenticated. The
profile and OAuth credentials can be saved or mapped to a user record.
`js
var GooglePlusStrategy = require('passport-google-plus');
passport.use(new GooglePlusStrategy({
clientId: 'YOUR_CLIENT_ID',
apiKey: 'YOUR_API_KEY'
},
function(tokens, profile, done) {
// Create or update user, call done() when complete...
done(null, profile, tokens);
}
));
`
#### Configure Google+ Sign-In Button
`html`
data-scope="https://www.googleapis.com/auth/plus.login"
data-clientid="YOUR_CLIENT_ID"
data-redirecturi="postmessage"
data-accesstype="online"
data-cookiepolicy="single_host_origin"
data-callback="signInCallback">
#### Handle the callback & forward the identity token
`js``
function signInCallback(authResult) {
if (authResult.code) {
$.post('/auth/google/callback', { id_token: authResult.id_token})
.done(function(data) {
$('#signinButton').hide();
});
} else if (authResult.error) {
console.log('There was an error: ' + authResult.error);
}
};