Answer the question
In order to leave comments, you need to log in
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>
)
}
}
// 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));
}
));
};
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) });
}
);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question