What is @types/passport-google-oauth20?
@types/passport-google-oauth20 provides TypeScript type definitions for the passport-google-oauth20 package, which is a Passport strategy for authenticating with Google using the OAuth 2.0 API.
What are @types/passport-google-oauth20's main functionalities?
Google OAuth 2.0 Strategy
This feature allows you to set up the Google OAuth 2.0 strategy for Passport. It involves configuring the strategy with your Google client ID, client secret, and callback URL, and defining a function to handle the user profile returned by Google.
const GoogleStrategy = require('passport-google-oauth20').Strategy;
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: 'http://www.example.com/auth/google/callback'
},
function(accessToken, refreshToken, profile, done) {
User.findOrCreate({ googleId: profile.id }, function (err, user) {
return done(err, user);
});
}));
Authentication Middleware
This feature demonstrates how to set up routes for initiating Google authentication and handling the callback after authentication. The middleware uses the Google strategy to authenticate users and handle success or failure scenarios.
app.get('/auth/google',
passport.authenticate('google', { scope: ['https://www.googleapis.com/auth/plus.login'] }));
app.get('/auth/google/callback',
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/');
});
Other packages similar to @types/passport-google-oauth20
passport-facebook
passport-facebook is a Passport strategy for authenticating with Facebook using the OAuth 2.0 API. It is similar to passport-google-oauth20 but is used for Facebook authentication instead of Google.
passport-twitter
passport-twitter is a Passport strategy for authenticating with Twitter using the OAuth 1.0a API. It provides similar functionality to passport-google-oauth20 but is used for Twitter authentication.
passport-github
passport-github is a Passport strategy for authenticating with GitHub using the OAuth 2.0 API. It offers similar features to passport-google-oauth20 but is used for GitHub authentication.