我有一個看起來像這樣的路由器:
router.get('/', index_controller.index);
router.get('/login', index_controller.login)
router.get('/profile/:userId', index_controller.profile)
然后 2 個控制器應該將登錄用戶重定向到profile/{userId}
login = (req, res) => {
res.oidc.login(
{ returnTo: '/profile/:userid'});
};
profile = (req, res) => {
const userid = req.oidc.userid;
res.render('profile', { userid: userid,
user: req.oidc.user,
});
console.log(userid);
};
但是當用戶登錄時,他們會被重定向到“/profile/:userId”而不是實際的 userId。
我一直在嘗試遵循示例,例如此處的 Express 檔案,但看不到我缺少的內容,因為當我手動導航時,通過將userId目標格式復制并粘貼到 URL 中,它可以正常作業并加載用戶組態檔正確。我想我錯過了在用戶登錄后實際使 URL“出現”在 URL 中的一些步驟,但功能本身正在作業。
如何按預期使組態檔頁面僅對 profile/{userId} 的當前用戶可用?
uj5u.com熱心網友回復:
您對 string 進行了硬編碼'/profile/:userid',但您需要以某種方式包含當前用戶 ID。
我不知道您如何在您的情況下訪問用戶,但您必須執行以下操作:
login = (req, res) => {
res.oidc.login(
{ returnTo: `/profile/${currentUser.id}`});
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/440861.html
