你好,我有節點/護照問題,我已經選中的回答#2,但沒有什麼工作對我來說..護照廣東話設置頭髮送之後
所以我嘗試使用護照,但我有這個錯誤:
Error: Can't set headers after they are sent.
這裏是我的策略:
passport.use('42', new FortyTwoStrategy({
clientID: configAuth.schoolAuth.clientId,
clientSecret: configAuth.schoolAuth.clientSecret,
callbackURL: configAuth.schoolAuth.callbackURL,
}, (accessToken, refreshToken, profile, done) => {
process.nextTick(() => {
console.log(1);
console.log(2);
User.findOne({ $and: [{ username: profile.username }, { provider: '42' }] }, (err, user) => {
if (err) return done(err, { status: false, details: 'Cant connect to db' });
if (!user) {
const newUser = new User({
id: profile.id,
username: profile.username,
mail: profile.emails[0].value,
image: profile.photos[0].value,
provider: '42',
});
newUser.save((erro) => {
if (erro) return done(erro);
return done(null, user, { status: true, details: 'success' });
});
} else {
return done(null, user, { status: true, details: 'success' });
}
});
return (false);
});
}));
我的路線:
app.get('/api/user/auth/42', passport.authenticate('42'));
app.get('/api/user/auth/42/callback', userFonc.schoolLogin);
和我的回調函數:
schoolLogin: (req, res, next) => {
passport.authenticate('42', (err, user) => {
if (err) return res.send(err);
if (!user) {
return res.send({ status: false, details: 'error occured' });
}
const token = jwt.sign({ _id: user._id, username: user.username }, cfg.jwtSecret);
res.set('Access-Control-Expose-Headers', 'x-access-token');
res.set('x-access-token', token);
res.send({ status: 'success', user });
return res.send(user);
})(req, res, next);
},
如果有人能解釋我爲什麼我有這樣的錯誤,以及如何將其刪除..因爲我認爲我的回報應該像那個..
謝謝你
嘗試刪除最後一個return語句。我認爲用戶發送了兩次。 – eirik
它的工作謝謝你! – kikiwie