简体   繁体   English

如何根据用户是否登录来更改同一路由上显示的页面? 我正在使用 node.js express auth0

[英]How to change the page diaplayed on the same route based on whether the person is logged in or not? I'm using node.js express auth0

If someone visits the site , they are going to be directed towards '/' which will display index page This page has login and signup options.如果有人访问该站点,他们将被定向到“/”,这将显示索引页面 该页面具有登录和注册选项。

server.js -- server.js --


const express = require("express");
const ejs = require('ejs');

const app  = express();

const { requiresAuth } = require('express-openid-connect');
const { auth } = require('express-openid-connect');
app.get('/', (req, res) => {
   res.render('pages/index');
    
});

however , if the person has logged in and wants to view the index page again I want to remove those login and signup links.但是,如果此人已登录并想再次查看索引页面,我想删除那些登录注册链接。 these links are coded in navigation bar in html, ejs.这些链接在 html、ejs 的导航栏中编码。

index.ejs -- index.ejs --


<%- include('../partials/head.ejs') %>

<body>

<%- include('../partials/navbar.ejs') %>

    <form  action="/" method="POST">

        <button class="btn btn-light btn-lg btn-block addPG"> Add New...</button>

    </form>

<%- include('../partials/footer.ejs') %>

    </body>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>

</html>

I wrote a new index page for those who have logged in but I cant figure a way to do so.我为那些已经登录的人写了一个新的索引页面,但我想不出这样做的方法。 Is my approach right or is there a better way to implement the stuff that I desire.我的方法是正确的还是有更好的方法来实现我想要的东西。

How I did it in my navbar was with if statements, Below is a snippet of how I have my ejs file我在导航栏中的做法是使用 if 语句,下面是我如何拥有 ejs 文件的片段

                <%if (user && user.role==2) { %>
                <li class="treeview">
                    <a href="#">
                      <i data-feather="user-check"></i>
                      <span>Admin</span>
                      <span class="pull-right-container">
                        <i class="fa fa-angle-right pull-right"></i>
                      </span>
                    </a>
                    <ul class="treeview-menu">
                      <li><a href="/admin"><i class="icon-Commit"><span class="path1"></span><span class="path2"></span></i>Users</a></li>
                      <li><a href="/recent-devices"><i class="icon-Commit"><span class="path1"></span><span class="path2"></span></i>Recent Devices</a></li>
                    </ul>
                  </li> 
                  <% } %>
                  <% } %>                 
              <%if (!user) { %>
                <li>
                    <a href="/signin">
                      <i data-feather="log-in"></i>
                      <span>Signin</span>
                    </a>
                  </li> 
              <% } %>

That just checks if there is a user, and you can update your HTML to show stuff based on roles and such.这只是检查是否有用户,您可以更新 HTML 以根据角色等显示内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM