简体   繁体   中英

how to redirect user to login page with

I've a NodeJS backend service where I am using http-proxy-middleware library to configure proxy and redirect users to the related frontend service according to the user type which also runnning in kubernetes.

Lets say https://example.com is my root adress. When user enter this URL he/she redirects the loging page and then routing to the related frontend according to the user type. After login user can access the contents via https://example.com/docs/employees . But if he/she copy and paste https://example.com/docs/employees URL without login then I throwed an error as below "Login Required". But I would like to also redirect user to the login page automatically.

How can I achieve this?

I tried with custom middleware and use this something like router.get("/", isAuth) but did not worked for me.When I tried to return res.redirect("/redirect/oidcLogin") in proxy there was a loop occured.

module.exports = (req, res, next) => {
    if (!req.session.isLoggedIn && !req.session.userinfo) {
        return res.redirect("/redirect/oidcLogin");
    }
    next();
}

router

router.get("/", (req, res) => {
  if (req.session && req.session.userinfo && req.session.isLoggedIn) {
    return res.redirect("/docs");
  } else {
    return res.redirect("/redirect/oidcLogin");
  }
});

router.get("/redirect/oidcLogin", authController.redirectOidc);
router.get("/auth/login/callback", authController.callbackLogin);
router.get("/redirect/oidcLogout", authController.logOut);
module.exports = router;

app.js

const userTypeRouter = function (req) {
      if (
        !req.session ||
        !req.session.userinfo ||
        !req.session.userinfo.communities ||
        !req.session.isLoggedIn
      ) {
        const err = new Error("Please login");
        err.statusCode = 403;
        throw err;
      }
      let isUser1 = true;
      if (req.session.userinfo.communities.includes("EMPLOYEE")) {
        isUser1 = false;
      }
      if (isDealer) {
        return "http://user1.svc.cluster.local";
      } else {
        return "http://user2.svc.cluster.local";
      }
    };
    
    const options = {
      router: userTypeRouter,
      pathRewrite: {
        ["^/docs"]: "/",
      },
      changeOrigin: true,
      ws: true,
    };
    const proxy = createProxyMiddleware(options);
    
    app.use(authRoutes);
    app.use("/docs", proxy);
    app.use(
      "/employees",
      createProxyMiddleware({
        router: userTypeRouter,
        pathRewrite: {
          "^/employees": "/",
        },
        changeOrigin: true,
        ws: true,
      })
    );
    app.use(
      "/dealers",
      createProxyMiddleware({
        router: userTypeRouter,
        pathRewrite: {
          "^/dealers": "/",
        },
        changeOrigin: true,
        ws: true,
      })
    );
    
    app.use((error, req, res, next) => {
      console.log(error);
      const status = error.statusCode;
      if (status === 403) {
        res.status(status).send("Login required");
      } else {
        res.send(error.message);
      }
    });

You should ideally be setting up a session-store for production mode, so that if your server goes offline or you use more than one server your sessions to users are contained and they can remain online based on a keying system.

On the other hand, res.redirect("/redirect/oidcLogin") should bring you to yoursite.com/redirect/oidcLogin

If you're saying you're stuck in a loop it's likely cause on the GET of oidcLogin, there was a check that failed.

Code isn't speaking loud enough to me but suggestion would be to simplify... You only really need to set isvalidated for the session and define it true when user validates to your criteria.

WEBAPP.post('/login', LoginLimiter, (req, res) => {
    if (req.session.loggedin) return res.redirect("/");
// Not logged in so let's run our code/and render the page.
});

This is really all one needs, loggedin gets set true when user matches 100% registering or logging in.

Below worked for me

app.use(
  "/employees",
    isAuth,
  createProxyMiddleware({
    router: userTypeRouter,
    pathRewrite: {
      "^/employees": "/",
    },
    changeOrigin: true,
    ws: true,
  })
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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