[英]Dynamic login/logout navbar
I want login button to convert to log out if the user is logged in. In my index.js file: 如果用户已登录,我希望登录按钮转换为注销。在我的index.js文件中:
app.get('/', (request, response) => {
const authenticated = request.session.authenticated || false;
// { user: { authenticated: authenticated } }
response.render('index',authenticated);
console.log(authenticated);
});
and in my index.pug: 在我的index.pug中:
`ul.nav.navbar-nav
li.nav-item
a.nav-link(href='foods') Foods
if authenticated
li.nav-item
a.nav-link(href='/logout') Logout
else
li.nav-item.dropdown
a#navbarDropdownMenuLink.nav-link.dropdown-toggle(href='/registration' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false')
| Register`
I'm not getting any error but it's not working. 我没有收到任何错误,但是没有用。 I don't know how to solve it. 我不知道该怎么解决。 Also console.log(authenticated);
也是console.log(authenticated);
shows the right boolean value. 显示正确的布尔值。
That's how I solve the issue: 1. 这就是我解决问题的方式:1。
let username ='';
2. app.get('/', (request, response) => { 2. app.get('/',(请求,响应)=> {
const authenticated = request.session.authenticated || false;
// { user: { authenticated: authenticated } }
response.render('index',{ authenticated: authenticated, username: username});
});
in PUG: 在PUG中:
if authenticated p Welcome #{username}! 如果已通过身份验证p欢迎使用{{username}!
4. 4。
if authenticated
li.nav-item
a.nav-link(href='/logout') Logout
if !authenticated
li.nav-item.dropdown
a#navbarDropdownMenuLink.nav-link.dropdown-toggle(href='/registration' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false')
| Register
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.