【问题标题】:How to explicitly require no authentication in a certain route, in node.js?如何在 node.js 中明确要求在某个路由中不进行身份验证?
【发布时间】:2021-10-18 04:14:19
【问题描述】:

我对 node.js 比较陌生,而且我几乎只是一个初学者:/。 我目前正在我的应用程序上使用重置密码功能(目前没有令牌)。 但是,我的重置密码链接总是在链接末尾有用户的电子邮件(例如:“http://localhost:3000/auth/reset/limtrum3@gmail.com”)。

另一方面,我有一个 ensure_auth 函数,它会阻止对所有页面的访问,除非指定了 req.path,如下所示:

async function ensure_auth(req, res, next) {
if (req.isAuthenticated() || req.path === "/index" || req.path === "/" || req.path === "/auth/login" || req.path === "/auth/register" || req.path === "/auth/forget" || req.path === "/auth/reset") {
    return next();
}
else {
    flashMessage(res, 'danger', 'Warning!!! Please Login to access the page', 'fa fa-exclamation-triangle', true);

    return res.redirect("/auth/login");
}

}

因此,我的重置密码页面需要用户登录才能访问该页面。 我尝试设置“/auth/reset”以及许多其他路径,但它不起作用。 是否有可能有一个适用于所有具有“/auth/reset”的页面的 req.path,无论它后面是什么,还是有其他方法可以做到这一点?

对不起,我的措辞不佳,提前感谢您! :D

【问题讨论】:

    标签: node.js authentication path routes reset-password


    【解决方案1】:

    解决了。使用

    req.path.startsWith("path")
    

    https://www.w3schools.com/jsref/jsref_startswith.asp

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-27
      • 1970-01-01
      相关资源
      最近更新 更多