authentication in node js code example

Example 1: nodejs authentication token

Setting up our development environment and initializing our express server.
Creating our first basic route and controller.
Fleshing out our routes and controllers to add users and login users.
Creating a route and controller that will handle getting all users.

Example 2: express passport js

// my github https://github.com/restuwahyu13

const { AuthSchema } = require('../models/model.auth')
const GoogleStrategy = require('passport-google-oauth20').Strategy
const FacebookStrategy = require('passport-facebook').Strategy
const GithubStrategy = require('passport-github').Strategy

// passport facebook
exports.passportFacebook = () => {
   return passport.use(
      new FacebookStrategy(
         {
            clientID: process.env.FACEBOOK_ID,
            clientSecret: process.env.FACEBOOK_SECRET,
            callbackURL: "http://localhost:3000/auth/facebook/callback",
            profileFields: ["id", "displayName", "gender", "email", "photos"],
            enableProof: true,
         },
         (accessToken, refreshToken, profile, done) => {
            authSocialSchema.findOne({idSocial: profile.id}, (err, user) => {
               if (err) return done(err, false);
               if (!user) {
                  authSocialSchema.findOrCreate(
                     {
                        idSocial: profile.id,
                        fullname: profile.displayName,
                        email: profile.email,
                        gender: profile.gende,
                        avatar: profile.photos[0]["value"],
                        provider: profile.provider,
                        created_at: Date.now(),
                     },
                     (err, user) => {
                        if (err) return done(err, false);
                        return done(null, user);
                     }
                  );
               } else {
                  return done(null, user);
               }
            });
         }
      )
   );
}

// passport google
exports.passportGoogle = () => {
   return passport.use(
      new GoogleStrategy(
         {
            clientID: process.env.GOOGLE_ID,
            clientSecret: process.env.GOOGLE_SECRET,
            callbackURL: "http://localhost:3000/auth/google/callback",
         },
         (accessToken, refreshToken, profile, done) => {
            authSocialSchema.findOne({idSocial: profile.id}, (err, user) => {
               if (err) return done(err, false);
               if (!user) {
                  authSocialSchema.findOrCreate(
                     {
                        idSocial: profile.id,
                        fullname: profile.displayName,
                        email: profile.emails[0]["value"],
                        avatar: profile.photos[0]["value"],
                        provider: profile.provider,
                        created_at: Date.now(),
                     },
                     (err, user) => {
                        if (err) return done(err, false);
                        return done(null, user);
                     }
                  );
               } else {
                  return done(null, user);
               }
            });
         }
      )
   );
}

// passport passport github
exports.passportGithub = () => {
   return passport.use(
      new GithubStrategy(
         {
            clientID: process.env.GITHUB_ID,
            clientSecret: process.env.GITHUB_SECRET,
            callbackURL: "http://localhost:3000/auth/github/callback",
         },
         (accessToken, refreshToken, profile, done) => {
            authSocialSchema.findOne({idSocial: profile.id}, (err, user) => {
               if (err) return done(err, false);
               if (!user) {
                  authSocialSchema.findOrCreate(
                     {
                        idSocial: profile.id,
                        username: profile.username,
                        fullname: profile.displayName,
                        avatar: profile.photos[0]["value"],
                        provider: profile.provider,
                        created_at: Date.now(),
                     },
                     (err, user) => {
                        if (err) return done(err, false);
                        return done(null, user);
                     }
                  );
               } else {
                  return done(null, user);
               }
            });
         }
      )
   );
}

Example 3: passport js

app.get('/auth/google',
  passport.authenticate('google', { scope: ['profile'] }));

app.get('/auth/google/callback', 
  passport.authenticate('google', { failureRedirect: '/login' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/');
  });

Example 4: passport js

var GoogleStrategy = require('passport-google-oauth20').Strategy;

passport.use(new GoogleStrategy({
    clientID: GOOGLE_CLIENT_ID,
    clientSecret: GOOGLE_CLIENT_SECRET,
    callbackURL: "http://www.example.com/auth/google/callback"
  },
  function(accessToken, refreshToken, profile, cb) {
    User.findOrCreate({ googleId: profile.id }, function (err, user) {
      return cb(err, user);
    });
  }
));

Example 5: passport js

$ npm install passport-google-oauth20