基本方案:推荐人网址 - https://example.com/5a936e07df429a4bb3dd7d29
用户可以使用/auth/facebook
或/auth/twitter
路由进行登录。将它们发送给提供商并使用/auth/facebook/callback
或/auth/twitter/callback
重定向回我的网站。
我不知道如何在成功登录后将它们重定向到https://example.com/5a936e07df429a4bb3dd7d29。自动柜员机全部重定向到('/')
,这不是所需的网址。
有什么想法吗?
//FACEBOOK
app.get('/auth/facebook', passport.authenticate('facebook', { scope: ['email', 'user_location'] }));
app.get('/auth/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/' }), (req, res) => { res.redirect('/'); });
//TWITTER
app.get('/auth/twitter', passport.authenticate('twitter'));
app.get('/auth/twitter/callback', passport.authenticate('twitter', { failureRedirect: '/' }), (req, res) => { res.redirect('/'); });
/**
* Sign in with Facebook.
*/
passport.use(new FacebookStrategy({
clientID: process.env.FACEBOOK_ID,
clientSecret: process.env.FACEBOOK_SECRET,
callbackURL: '/auth/facebook/callback',
profileFields: ['name', 'email', 'link', 'locale', 'timezone', 'gender'],
passReqToCallback: true
}, (req, accessToken, refreshToken, profile, done) => {
console.log('hellow' + req.baseUrl + req.path + ' aiiiiiciiii');
console.log(req.header('referrer'));
console.log('\n');
console.log(req.originalUrl + ' aici e full pathul');
if (req.user) {
User.findOne({ facebook: profile.id }, (err, existingUser) => {
if (err) { return done(err); }
if (existingUser) {
req.flash('errors', { msg: 'There is already a Facebook account that belongs to you. Sign in with that account or delete it, then link it with your current account.' });
done(err);
} else {
User.findById(req.user.id, (err, user) => {
if (err) { return done(err); }
user.facebook = profile.id;
user.tokens.push({ kind: 'facebook', accessToken });
user.profile.name = user.profile.name || `${profile.name.givenName} ${profile.name.familyName}`;
user.profile.gender = user.profile.gender || profile._json.gender;
user.profile.picture = user.profile.picture || `https://graph.facebook.com/${profile.id}/picture?type=large`;
user.save((err) => {
req.flash('info', { msg: 'Facebook account has been linked.' });
done(err, user);
});
});
}
});
} else {
User.findOne({ facebook: profile.id }, (err, existingUser) => {
if (err) { return done(err); }
if (existingUser) {
return done(null, existingUser);
}
User.findOne({ email: profile._json.email }, (err, existingEmailUser) => {
if (err) { return done(err); }
if (existingEmailUser) {
req.flash('errors', { msg: 'There is already an account using this email address. Sign in to that account and link it with Facebook manually from Account Settings.' });
done(err);
} else {
const user = new User();
user.email = profile._json.email;
user.facebook = profile.id;
user.tokens.push({ kind: 'facebook', accessToken });
user.profile.name = `${profile.name.givenName} ${profile.name.familyName}`;
user.profile.gender = profile._json.gender;
user.profile.picture = `https://graph.facebook.com/${profile.id}/picture?type=large`;
user.profile.location = (profile._json.location) ? profile._json.location.name : '';
user.wishlists = WishListCreator(user.id);
user.save((err) => {
done(err, user);
});
}
});
});
}
}));
// Sign in with Twitter.
passport.use(new TwitterStrategy({
consumerKey: process.env.TWITTER_KEY,
consumerSecret: process.env.TWITTER_SECRET,
callbackURL: '/auth/twitter/callback',
passReqToCallback: true
}, (req, accessToken, tokenSecret, profile, done) => {
if (req.user) {
User.findOne({ twitter: profile.id }, (err, existingUser) => {
if (err) { return done(err); }
if (existingUser) {
req.flash('errors', { msg: 'There is already a Twitter account that belongs to you. Sign in with that account or delete it, then link it with your current account.' });
done(err);
} else {
User.findById(req.user.id, (err, user) => {
if (err) { return done(err); }
user.twitter = profile.id;
user.tokens.push({ kind: 'twitter', accessToken, tokenSecret });
user.profile.name = user.profile.name || profile.displayName;
user.profile.location = user.profile.location || profile._json.location;
user.profile.picture = user.profile.picture || profile._json.profile_image_url_https;
user.save((err) => {
if (err) { return done(err); }
req.flash('info', { msg: 'Twitter account has been linked.' });
done(err, user);
});
});
}
});
} else {
User.findOne({ twitter: profile.id }, (err, existingUser) => {
if (err) { return done(err); }
if (existingUser) {
return done(null, existingUser);
}
const user = new User();
// Twitter will not provide an email address. Period.
// But a person’s twitter username is guaranteed to be unique
// so we can "fake" a twitter email address as follows:
user.email = `${profile.username}@twitter.com`;
user.twitter = profile.id;
user.tokens.push({ kind: 'twitter', accessToken, tokenSecret });
user.profile.name = profile.displayName;
user.profile.location = profile._json.location;
user.profile.picture = profile._json.profile_image_url_https;
user.wishlists = WishListCreator( user.id );
user.save((err) => {
done(err, user);
});
});
}
}));