FacebookTokenError: This authorization code has been used passport-js
FacebookTokenError: This authorization code has been used passport-js
我已经使用 passport.js 实现了 log in with facebook
。但有时每当我尝试登录时,它都会给我这个错误 -
FacebookTokenError: This authorisation code has been used
但事实是太随意了。有时它工作得很好。有时会报这个错误。我已经尝试了 SO 帖子中提供的所有解决方案。 None 其中有效。
我使用 React 作为前端,node.js 作为后端。对于使用 express-session 的会话中间件。
这是我用来实现的代码log in with facebook
import passport from 'passport';
import { Strategy } from 'passport-facebook';
const { FACEBOOK_APP_ID, FACEBOOK_APP_SECRET, API_URL } = process.env;
export default (app) => {
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser((user, cb) => cb(null, user));
passport.deserializeUser((obj, cb) => cb(null, obj));
passport.use(new Strategy({
clientID: FACEBOOK_APP_ID,
clientSecret: FACEBOOK_APP_SECRET,
callbackURL: `${API_URL}facebook/callback`,
profileFields: ['id', 'displayName', 'email', 'name', 'photos'],
passReqToCallback: true,
enableProof: true,
}, async (req, accessToken, refreshToken, profile, cb) => {
try {
const email = (profile.emails && profile.emails[0].value) || '';
const { displayName } = profile;
const users = await Users.findOrCreate({
where: { email },
defaults: { name: displayName },
}).catch((err) => {
console.log(err);
});
if (users.length) {
req.session.userId = users[0].get('id');
return cb(null, users[0]);
}
} catch (err) {
console.log('error during fb: ', err);
}
return cb('error in facebook');
}));
app.use('/facebook', passport.authenticate('facebook', { session: false, scope: ['public_profile', 'email'] }));
app.use('/facebook/callback', passport.authenticate('facebook', { session: false, failureRedirect: `${FRONTEND_HOST}` }), (_, res) => {
res.redirect(`${FRONTEND_HOST}`);
});
};
这是我得到的完整错误 -
我不确定您的代码的实际问题,但您需要使用:
app.get('/facebook', ...)
app.get('/facebook/callback', ...)
而不是
app.use(...)
我已经使用 passport.js 实现了 log in with facebook
。但有时每当我尝试登录时,它都会给我这个错误 -
FacebookTokenError: This authorisation code has been used
但事实是太随意了。有时它工作得很好。有时会报这个错误。我已经尝试了 SO 帖子中提供的所有解决方案。 None 其中有效。
我使用 React 作为前端,node.js 作为后端。对于使用 express-session 的会话中间件。
这是我用来实现的代码log in with facebook
import passport from 'passport';
import { Strategy } from 'passport-facebook';
const { FACEBOOK_APP_ID, FACEBOOK_APP_SECRET, API_URL } = process.env;
export default (app) => {
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser((user, cb) => cb(null, user));
passport.deserializeUser((obj, cb) => cb(null, obj));
passport.use(new Strategy({
clientID: FACEBOOK_APP_ID,
clientSecret: FACEBOOK_APP_SECRET,
callbackURL: `${API_URL}facebook/callback`,
profileFields: ['id', 'displayName', 'email', 'name', 'photos'],
passReqToCallback: true,
enableProof: true,
}, async (req, accessToken, refreshToken, profile, cb) => {
try {
const email = (profile.emails && profile.emails[0].value) || '';
const { displayName } = profile;
const users = await Users.findOrCreate({
where: { email },
defaults: { name: displayName },
}).catch((err) => {
console.log(err);
});
if (users.length) {
req.session.userId = users[0].get('id');
return cb(null, users[0]);
}
} catch (err) {
console.log('error during fb: ', err);
}
return cb('error in facebook');
}));
app.use('/facebook', passport.authenticate('facebook', { session: false, scope: ['public_profile', 'email'] }));
app.use('/facebook/callback', passport.authenticate('facebook', { session: false, failureRedirect: `${FRONTEND_HOST}` }), (_, res) => {
res.redirect(`${FRONTEND_HOST}`);
});
};
这是我得到的完整错误 -
我不确定您的代码的实际问题,但您需要使用:
app.get('/facebook', ...)
app.get('/facebook/callback', ...)
而不是
app.use(...)