简体   繁体   English

如果用户在 express js 中尝试访问登录路由时已经登录,我想重定向到仪表板

[英]I want to redirect to dashboard if user is already logged in when he try to access login route in express js

I want to redirect to dashboard if user is already logged in when he try to access login route in express js如果用户在 express js 中尝试访问登录路由时已经登录,我想重定向到仪表板

Middleware中间件

const isAuthenticate = async (req, res, next) => {
  const token = req.cookies.jwt;
if (token) {
    jwt.verify(token, "thisisjwtsecret", async (err, token_decode) => {
      if (!err) {
        const u_id = token_decode._id;
        const userData = await User.findOne({ _id: u_id });
        req.user = userData;
        req.isAuth = true;
        next();
      } else {
        res.redirect("/user/login");
      }
    });
  } else {
    res.redirect("/user/login");
    }
};

Route.js路由.js

// Auth Controller
const AuthController = require("../../controllers/auth/AuthController");
const { isAuthenticate } = require("../../middlewares/isAutheticated");

router.get("/user/login", isAuthenticate, AuthController.login);
router.post("/user/login", AuthController.checkLogin);
router.get("/user/register", isAuthenticate, AuthController.createUser);
router.post("/user/register", isAuthenticate, AuthController.storeUser);
module.exports = router;

LOgin function登录 function

// Showing Login Page to User
const login = (req, res) => {
  return res.render("auth/login");
};

You can break out the functionality from your existing isAuthenticate() function so it just returns a result and then use that to do something like this:您可以从现有的isAuthenticate() function 中分离出功能,这样它只返回一个结果,然后使用它来执行如下操作:

const { promisify } = require('util');
const verify = promisify(jwt.verify);

// resolves if jwt cookie verifies and user found
// rejects if jwt cookie is missing or doesn't verify or user not found
async function isLoggedIn(req) {
    const token = req.cookies.jwt;
    if (!token) throw new Error("no jwt cookie");
    const token_decode = await verify(token, "thisisjwtsecret");
    let user = await User.findOne({ _id: token_decode._id });
    if (!user) throw new Error("user not found");
    return;
}

// Showing Login Page to User
// Or redirect to /dashboard if already logged in
const login = async (req, res) => {
    try {
        await isLoggedIn(req);
        // already logged in, redirect to dashboard
        // you MUST make sure that /dashboard does not redirect to /user/login itself
        // when isLoggedIn resolves to avoid circular redirects
        res.redirect("/dashboard");
    } catch (e) {
        // not logged in, render the login page
        res.render("auth/login");
    }
};

The isLoggedIn(req) function resolves if the token validates and the user is found in the database. isLoggedIn(req) function 解析令牌是否有效并且在数据库中找到了用户。 Otherwise, it rejects.否则,它拒绝。 You can then use that in other routes to decide whether you want to redirect or not.然后,您可以在其他路由中使用它来决定是否要重定向。

暂无
暂无

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

相关问题 我想在未登录时尝试在登录后访问屏幕时重定向到登录屏幕 - I want to redirect to the login screen when I try to access the screen after login when I am not logged in Angular 2/4:如果用户已经登录,如何限制对登录路由的访问? - Angular 2/4: How to restrict access to Login Route if user is already logged in? 使用php将登录用户从登录页面重定向到用户仪表板 - Redirect a logged in user from login page to user dashboard with php 当我尝试访问快递路线时未找到404错误 - Not Found 404 Error when i try to access an express route 如果用户未登录,则Node.js重定向到登录 - Node.js redirect to login if user is not logged in 如果用户已经登录,则将用户重定向到受保护的路由会导致登录页面在重定向前重新呈现并显示一段时间 - Redirect user to protected route if user already logged in causes login page to re-render and display for sometime before redirection 如何在用户登录-passport.js-phonegap时“记住”用户 - How to 'remember' a user when he logged in -passport.js-phonegap 限制用户登录后重定向到登录页面,即使他按角度按浏览器的后退按钮? - restrict the redirection to login page once the user has already logged in even if he press the back button of browser in angular? 在使用 Express 的 Node.js 中,如何同时拥有用户登录和管理员登录? 我希望具有管理员访问权限的用户可以访问所有路由 - In Node.js using Express, how does one have a user login along with admin login? I want users limited access with admin have access to all routes 如果用户尚未登录Angular JS,则重定向到登录页面 - Redirect to a login page if the user is not yet logged in Angular JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM