app.get("/auth/google", passport.authenticate("google", {
session: false,
scope: ["profile", "email"]
}));
passport.use(new GoogleStrategy({
clientID: " x",
clientSecret: " y",
callbackURL: "http://localhost:3000/auth/google/callback"
},
function(accessToken, refreshToken, Gprofile, done) {
process.nextTick(function() {
Gdata = {
'id': Gprofile.id,
'name': Gprofile.displayName,
'profileImage': Gprofile.photos[0].value,
'gender': Gprofile.gender,
'email': Gprofile.emails[0].value
};
console.log('Gdata recieved');
console.log('--------------------------------------------------------------------------');
return done(null, true); //It seems redirects to failureRedirect or successRedirect.
});
}
));
app.get("/auth/google/callback", passport.authenticate('google', {
session: false,
failureRedirect: "/google_callback_fail",
successRedirect: "/reports"
}));
app.get("/reports", function(req, res) {
// ------------DB
var db = require('../model/connection.js');
var stmt;
var name = Gdata.name.split(" ");
stmt = "INSERT INTO User(`first_name`, `last_name`, `email`) VALUES (?,?,?)";
var x = new Promise(function(resolve, reject) {
var sentToCJS;
db.query(stmt, [name[0], name[1], Gdata.email], function(err, rows) {
if (err) reject(err);
PAYLOAD = { userID: rows.insertId };
ENCODED_JWT = _jwt.sign(PAYLOAD, 'secret', { expiresIn: EXP_TIME });
console.log("1 ENCODED_JWT:", ENCODED_JWT);
sentToCJS = {
'jwt': ENCODED_JWT,
'name': Gdata.name,
'profileImage': Gdata.profileImage,
'gender': Gdata.gender,
'email': Gdata.email
};
console.log("sentToCJS:", sentToCJS);
//creates persistant cookie.
res.cookie("jwt", ENCODED_JWT, {
maxAge: 604800, //7days to ms
httpOnly: true,
sameSite: true,
signed: true //detects if user has modified the cookie
});
resolve(sentToCJS);
});
});
x.then(function(sentToCJS) {
res.render('reports', sentToCJS);
});
x.catch(function(error) {
console.log("error", error);
});
원하는 결과 : 서버 응답 쿠키 및 렌더링 ('reports', sentToCJS); rescookie 및 res.render가 작동해야합니다.서버 response.cookie 및 response.render를 함께 보내는 방법은 무엇입니까?
오류 : 보낸 후 헤더를 설정할 수 없습니다. at SendStream.headersAlreadySent
어떻게 res.cookie와 res.render를 함께 보내거나 여러 답장으로 보낼 수 있습니까? 표현식에서는 가능하지 않지만 node.js res.write 또는 writeHead 메소드를 사용하면 가능합니다.
두 문장은 단일 응답의 일부입니다. 두 가지 응답으로 무엇을 의미합니까? – Muthukumar
@Muthukumar 오류 메시지 –