![](/img/trans.png)
[英]req.user is undefined after logging in with google-passport-oauth
[英]adding additional data to req.user with passport-google-oauth
我有一條旨在使用google oauth護照策略( /auth/google
)對用戶進行身份驗證的路由,我也想通過該路由( /auth/google?someParam=SOME_PARAM
)中的url作為查詢來傳遞其他數據我想在( /auth/google/callback
)從Google req.user
時將其添加。 問題是我可以通過/auth/google
訪問此查詢,但是Google會將我重定向到/auth/google/callback
,后者不再有權訪問此數據。
注-由於設計限制,我無法使用外部源作為數據庫。
護照-Google 文檔
代碼:
// auth.js
router.get(
"/",
(req, res, next) => {
let siteName = req.query.siteName;
let pageName = req.query.pageName;
console.log("siteName", siteName);
return next();
},
passport.authenticate("google", {
scope: ["https://www.googleapis.com/auth/plus.login"]
})
);
module.exports = router;
// authCb.js
router.get(
"/",
passport.authenticate("google", {
scope: ["https://www.googleapis.com/auth/plus.login"],
failureRedirect: "/"
}),
(req, res) => {
console.log(req.user);
res.send(req.user);
}
);
module.exports = router;
// app.js
app.use("/auth/google", auth);
app.use("/auth/google/callback", authCb);
您必須先在會話中存儲參數,然后才能向Google發送身份驗證請求。 然后,在重定向之后,從會話中恢復參數。
// auth.js
router.get(
"/",
(req, res, next) => {
req.session.lastQuery = req.query;
return next();
},
passport.authenticate("google", {
scope: ["https://www.googleapis.com/auth/plus.login"]
})
);
module.exports = router;
// authCb.js
router.get(
"/",
passport.authenticate("google", {
scope: ["https://www.googleapis.com/auth/plus.login"],
failureRedirect: "/"
}),
(req, res) => {
const { lastQuery } = req.session;
console.log(lastQuery);
}
);
module.exports = router;
您應該參考Google的文檔。 一旦用戶回到您的站點,您可以使用“ state”參數傳遞您想要獲取的任何數據。 這是此參數的主要用途。 您可以在此處查看詳細信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.