Код ниже показывает, как использовать информацию профиля для предоставления пользователям персонализированных функций.
const passport = require('passport');
const GoogleDriveStrategy = require('passport-google-drive').Strategy;
const mongoose = require('mongoose');
const Keys = require('../config/keys.js');
const User = mongoose.model('drive-users');
const prettyjson = require('prettyjson');
function extractProfile (profile) {
let imageUrl = '';
if (profile.photos && profile.photos.length) {
imageUrl = profile.photos[0].value;
}
return {
id: profile.id,
displayName: profile.displayName,
image: imageUrl
};
}
passport.use(
new GoogleDriveStrategy(
{
clientID: Keys.DRIVE_CLIENT_ID,
clientSecret: Keys.DRIVE_CLIENT_SECRET,
callbackURL: '/auth/google-drive/callback',
scope : 'https://www.googleapis.com/auth/drive'
},
(accessToken, refreshToken, profile, done) => {
console.log(prettyjson.render(profile));
// Extract the minimal profile information we need from the profile object
// provided by Google
done(null, extractProfile(profile));
}
)
);
// Typically, this will be as simple as storing the user ID when serializing, and finding
// the user by ID when deserializing.
passport.serializeUser( function (user, done) {
done(null, user);
});
passport.deserializeUser( function (obj, done) {
done(null, obj);
});
//==================
// add your routes here
//==================
app.get(
'/auth/google-drive',
passport.authenticate('google-drive')
);
app.get(
'/auth/google-drive/callback',
passport.authenticate('google-drive')
);
Есть еще один образец , который использует Passport.js. Это поможет вам понять аутентификацию пользователей с помощью библиотеки Passport.js.
Вы также можете прочитать быстрый запуск Node.js, используя Google Drive для Resful API.