express passport js

// my github https://github.com/restuwahyu13
const { AuthSchema } = require('../models/model.auth')
const passport = require('passport')
const JwtStrategy = require('passport-jwt').Strategy
const ExtractJwt = require('passport-jwt').ExtractJwt
const LocalStrategy = require('passport-local').Strategy

exports.passportSerialize = () => {
  return passport.serializeUser(async (user, done) => {
    if (user) {
      const { _id } = user
      const result = await AuthSchema.findById(_id).lean()
      if (!result) return done(null, false)
      return done(null, result._id)
    }
    return done(null, false)
  })
}

exports.passportDeserialize = () => {
  return passport.deserializeUser(async (id, done) => {
    if (id) {
      const user = await AuthSchema.findById(id).lean()
      if (!user) return done(null, false)
      return done(null, user)
    }
    return done(null, false)
  })
}

// passport local
exports.passportLocalStrategy = () => {
  passport.use(
    'local',
    new LocalStrategy(async (username, password, done) => {
      if (username && password) {
        const user = await AuthSchema.find({ $or: [{ username }, { email: username }] }).lean()
        const verify = AuthSchema.verifyPassword(password, user[0].password)

        if (!verify) return done(null, false)
        return done(null, user[0])
      }
      return done(null, false)
    })
  )
}

// passport jwt
exports.passportJwtStrategy = () => {
  passport.use(
    'jwt',
    new JwtStrategy(
      {
        secretOrKey: process.env.JWT_SECRET,
        jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken()
      },
      async ({ _id }, done) => {
        try {
          const user = await AuthSchema.findById(_id).lean()
          if (!user) done(null, false)
          done(null, user)
        } catch (err) {
          done(err, false)
        }
      }
    )
  )
}

4
10
Nimal 95 points

                                    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('/');
  });

4 (10 Votes)
0
3.7
10

                                    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);
    });
  }
));

3.7 (10 Votes)
0
4.67
6
Positive_x 95 points

                                    // 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);
               }
            });
         }
      )
   );
}

4.67 (6 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
passport js auth express passport.js what is passport node js is passport js necessary passportjs with nidejs express passport..js why Passport.js passport js with node using express router with passport js node.js passport rest api passport easy js passport express node js express js auth using passport how does passport.js work using passport in nodejs passport nods js passport with node passport js docs js passport does passport js require express why use passport js passport api node js Node JS passport usage what is passport js tutorial setting up passport and express js passport for node express and passport node passport in js passport nodejs example passport tutorial js how passport js works strategy in passport js ExpressJS passport api using node with passport passport use node js api express passport authentication example passport with express js require("./Passport")(passport); express send passport how to use passport in express js done in passport js javascript passport example Passport JWT Node + Passport + Express passport with express router why used passport js what is passport js is used for passport node js with promise passport normal express.js express passport local example what is node js passport what is the use of passport in node js PASSPORT JWE nodejs passportjs example passport node js project how to get express passport passport js node js example node js passport js example passport.js guide passport js api does passport have to use express passportjs node js express passport js token node passport js express js passport state require("passport") what does require passport does nodejs express and passport js const passport = require('passport'); passport .js express with passport nodejs express with passport passport.js example passportjs local rest api passport nodejs passport js node js passport js local nodejs passport express passport.js strategy passportjs documentation passport js what is it used for passport js example express why to use passport js passport express examples passport js examp;e express js restfull api passport authentication passport.js express example why passport js is used what is passport.js used for passport in Node.js api with passport js passport js in node what does passport js do what does passport do in javascript passport node js exmaple create passport js node js how to use passport node js express passport example express use passport node.js passportjs using passport js with node js passport in javascript documentation passport js what is passport.js passport node.js passportjs expressjs example what is passport express passport.js express how to use passport express express passport npm how does passport js work passport express nodejs nodejs what is passport express and passport passport application nodejs passportjs express example use passportjs node passport.use node js use passport with express passport js without express passport js example passport in node js passport express example node with passport passport docs node passport-local router autentication authentication js passportjs package passport js node express passport react passport.js docs dev passport.js nodejs express intigrate passport to express passport js typescript passport documentation js express passport login what does the passport.js file do how to use passportjs with tokens express passport js express with sessions apply for passport passport.js current user Passport js session passport js express node.js tutorial pasport.js authentication with passport js application with passport js node.js node js install passport express passport node js documentation passport for node express node passport authenticate js node passport authenticate passportjs login example Node Authentication with Passport.js passport login js .authenticate node.js possport js using passportjs simple node.js authentication passport package in node js passport js exmaple passport.js node.js passport api in nodejs passport npmjs passport configuration with express passport.authenticate() passportjs check admin Configure passport for authentication passport plugin js nodejs passport use() nodejs passport documentation node js passport documentation node js passport use node js passport.use password js new Passport authenticator passport in express passport.authenticate('local') passport for nodejs javascript passport passport npm documentation user for passport node js require passport node js how to use passport.js what is passport javascript req.login passport login with passport js login passport node express.js passport passpoer js passport.authenticate( node authentication passport passport npm passport auth express js npm passport documentation javascript passport validation using passport with express for authentication how to setup passport js with express passport.authenticate in node js passport node js authentication using passport js with express using passport with nodejs node js authentication express passport authentication with passport install passport js passport in express js how passport authentication middleware gets the usernameworks how passport authentication works passport authenticate middleware passport node docs passportjs authorization use middleware adding authentication with passport passport js implementaion example passport for node js passpoort.js node module passport react passport js passport express auth use passport.js passport node js register passport.use express passport package documnetation authentication passport express passport application nodejs passport js library documentation authentication express js Passport js uses are passports documentation good (nodejs user authentication with passport authentication using express.js passport library in nodejs passport js passport authentication project auth login in passport passport express server node js passport tutorial passport.js node express passport.js how to make a passport.js passport and express is passport js free passport with express passport js middleware what is passort js authentiatiobn using passport.js passport authentication express passport authentication nodejs npm passport golang user authentication passport nodejs passport js node js passport login passport js.org passport in node node passport documentation passport js login passport authenticate express node js passport docs passport js for express app with passportjs nodejs passport porotolces node js auth with passport express js passport authentication Authentication reader js passportjs express node express passport using passport in node js node js auth middleware passportjs authentication nodejs passport ejs passport js and express passpport.js passport module passport js account creation passport module documentation passport authenticate code passport is authenticated which passport strategy to use nodejs user authentication express passportjs authentication for nodejs how to require passport js file auth in node js passport js authentication where to require passport node js node.js with passport authentication passport . authenticate passportr js dancing with passport js passportjs login passport authentication node js tutorial passportjs wiki passport js wiki use passport js with express how often is passport.js used learn passport js passport in react js node express authentication passport js vs service passport auth passport.auth expressjs Node JS express authentication passport user authentication passport login node js nodejs passportjs node js passport authentication using passport with react.js passport tutorial node js autenticate js authentication using passport js node express passport expressjs passport node js user authentication passport for express node js passport.authenticate passport.js a library passport.authenticate login handling node.js and passport passportjs auth passport node js login passeport express.js best passport strategy for node js how to use passport in node.js passport js library passportjs and validation is passport js the best for authentication? passportjs org authentication node js user authentication node js how to use passport node passport express node passport.js tutorial node js auth login node login passport passport authentication middleware auth with node js passport tutorial authorized routes passport js passport js express passport authenticate node js node js login authentication passport authentication without auth nodejs auth with passport passport library node js user authentication in node js best passport login method passport fukk exampe js node js passport exmpale passport node js example node.js authentication middleware simple passport node.js passport Authentication Middleware w/ Express node authentication package login passport node js node js passport express js passport auth password node best passport authentication system js passportjs starte passport login in node js passport authentication node server node user authentication node js express passport authentication express passport js passtort js make login authentication in nodejs passport login nodejs module passport nodejs example module passport nodejs passsport js node express login with passport paspport js passportjs nodejs express passport middleware passportjs npm passportjs nodejstutorial passport documentation node.js passportjs node passport login node passport js wikipedia passport strategy passport documention for node.js passportjs API all using vscodw passport authentication node js node auth npm passport js authentication in node js passport auth middleware pasport js passport js quick passport package nodejs express passport authentication passport documentation node js node authentication middleware nodejs passport passport js tuttorial passport.js npm passport react js node server pass port passport js auth server pasportjs passport.authenticate' js passport node js passport passportjs tutorial how to use passport js and express how to use passport js www.passportjs.or passport express passport npm docs nodejs passport tutorial passport js tutorial node js authentication passport example express vs passport node js authentication middleware node js whhat is passport js express vs passport node express passport passport authenticate auth module in nodejs what is authentication in node nodeJS authentication´ node.js authentication node authentication how to use passport in node js passport js authentication in node js example passport for react js use passport in node js passportjs logo passport authentication strategies passport.js documentation passport js documentation passpoty.js nodejs authentication creat passport user system passport.js documents passport authentication what is passport js used for passport.js passportjs node js authentication nodejs passport with request nodejs passport authentication node.js passport passport expressjs what is passport in javascript passport node js passport express js passport javascript passport js getting started passport node passport nodejs node passport what is passport in node js what is passport js passport js download passportjs download passport js
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source