A
A
angelzzz2019-04-07 17:55:26
Express.js
angelzzz, 2019-04-07 17:55:26

How to pass login success information to facebook using passport.js in redux?

I also use the MERN stack for passport.js authorization
. In the react component (shortened version of the code), I have a normal login and through facebook:

class Login extends Component {
  render() {
    return (
      <a href="/" onClick={this.onLogin}>Enter via email</a>
      <a href="https://localhost:5000/api/users/auth/facebook">Enter via facebook</a>
    )
  }
}

in server.js
// Passport middleware
app.use(passport.initialize());
// Passport Config
require("./config/jwt")(passport);
require("./config/facebook")(passport);
passport facebook strategy - facebook.js

const passport = require('passport')
const FacebookStrategy = require('passport-facebook').Strategy;
const mongoose = require("mongoose");
const User = mongoose.model("users");
const keys = require("./keys");

module.exports = passport => {
  passport.use(
    new FacebookStrategy(
      {
        clientID: keys.facebook.clientID,
        clientSecret: keys.facebook.clientSecret,
        callbackURL: "https://localhost:5000/api/users/auth/facebook/callback",
        profileFields: ['id', 'emails', 'name', 'picture.type(large)']
      },
      (accessToken, refreshToken, profile, done) => {
        // console.log("FacebookStrategy", profile);
        User.findOne({
          // 'facebook.id': profile.id 
          $or: [
            { 'facebook.id': profile.id },
            { 'email': profile.emails[0].value }
          ]
        })
          .then(user => {
            if (user) {
              console.log("__user already exists", user);
              if (user.facebook.id == undefined) {
                user.facebook.id = profile.id;
                user.facebook.token = accessToken;
                user.facebook.email = profile.emails[0].value;
                user.facebook.name = profile.name.givenName + ' ' + profile.name.familyName;
                user.save();
              }

              return done(null, user);
            } else {
              console.log("__user doens't exist", user);
              const newUser = new User({
                facebook: {
                  id: profile.id,
                  token: accessToken,
                  email: (profile.emails) ? profile.emails[0].value : undefined,
                },
                name: profile.name.givenName + ' ' + profile.name.familyName,
                email: (profile.emails) ? profile.emails[0].value : undefined,
                privilege: 1
              });

              newUser
                .save()
                .then(user => console.log("passport facebook save user -> ", user))
                .catch(err => console.log("passport facebook save err -> ", err))

              return done(null, newUser);
            };
          })
          .catch(err => console.log("Passport err ->  ", err));
      }
    ));
};

in api/routes.js
router.get("/auth/facebook",
  passport.authenticate('facebook', { session: false, scope: ['email'] }) //, { session: false, scope: ['email'] })
);

router.get('/auth/facebook/callback',
  passport.authenticate('facebook', {
    session: false,
    scope: ['email'],
    successRedirect: 'https://localhost:3000',
    failureRedirect: 'https://localhost:3000/login'
  }), (req, res) => {
    console.log("facebook success");
    res.redirect('https://localhost:3000');
    res.render('json', { data: JSON.stringify(req.user.access_token) });
  }
);

From the backend everything works - the user logs in. In case of normal login (JWT) I use onClick which calls authAction which passes payload from db to redux. But I can't figure out how to send information about successful authorization via facebook to redux.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-04-07
@rockon404

As an option:
res.cookie(TOKEN_KEY, req.user.access_token);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question