简体   繁体   中英

social login, vue-authenticate with passport(nodejs)

I'm trying to implement facebook login with vue-authenticate and passport. I succeeded in logging into my Facebook account. And i got the 'Callback code' successfully. This is my callback url

http://localhost:8080/auth/callback?code=AQD0FgQ7I2oci0m3bqOHOBE1EV3Ri1TBnVcqs2PRT8pFNa38NIMX-eYiSr2EiWKQBMoNq1yOeo1QkDG1OiDjF_xUduK-HWMlMazsaBzoGNxiAK3FQH5KQopZ9NUnM2g-UYLpihtpsaFrRVssJkm8Xue1nyKbbWX76EPnPCIEVOfGM_JE4mbENLpp6_w8gwkTS9n8dtsNptDM72UO9zE7mj34J8Yls0A1VqmoZail0J2zwu4hJCzAzbP2FZ531Vo2tCERn2F_4DKsJ-zq_ppZWxRlKuRW9WFBL0UvsuNN_ODiRFs70P3SoK85-xHwzHJvx8VrVxmLlp5x7rVOzy2E2Jma# =

So I used axios to pass the 'Callback code' to the server. because my server code(passport-facebook) is this:

router.route('/auth/facebook/callback').get(passport.authenticate('facebook', {
      successRedirect : '/',
      failureRedirect : '/'
    }));

and axois code in Vue is

this.$http.get('/api/users/auth/facebook/callback',{
      params:{
        code : this.param
      }
    })
    .then((response) => {
      this.movies = param;
    })

but it never works.. I don't know why. just wondering, i chaneged axios code to get('api/users/') . and wrote server code like this

router.get('/',()=>{
  console.log("good");
});

it works. I can see the 'good' message in console. Let me know how you implement social sign-in!

if you want to configure Facebook login with passport So you can simply follow these Steps As mention below:

Step 1. Add passport configuration

const passport = require('passport');
app.use(passport.initialize());
app.use(passport.session());

app.get('/success', (req, res) => res.send("You have successfully logged in"));
app.get('/error', (req, res) => res.send("error logging in"));

passport.serializeUser(function(user, cb) {
  cb(null, user);
});

passport.deserializeUser(function(obj, cb) {
  cb(null, obj);
});

Step 2. Setup facebook configuration

const FacebookStrategy = require('passport-facebook').Strategy;

const FACEBOOK_APP_ID = 'your app id';
const FACEBOOK_APP_SECRET = 'your app secret';

passport.use(new FacebookStrategy({
    clientID: FACEBOOK_APP_ID,
    clientSecret: FACEBOOK_APP_SECRET,
    callbackURL: "/auth/facebook/callback"
  },
  function(accessToken, refreshToken, profile, cb) {
      return cb(null, profile);
  }
));

app.get('/auth/facebook',
  passport.authenticate('facebook'));

app.get('/auth/facebook/callback',
  passport.authenticate('facebook', { failureRedirect: '/error' }),
  function(req, res) {
    res.redirect('/success');
  });

This will fix your issue

Please refer this link https://www.sitepoint.com/passport-authentication-for-nodejs-applications/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM