Can I pass a custom redirect link to my static login page? #11740
-
I'm new to GitHub and web development in general, so please bear with me if I make trivial mistakes. I currently have an express backend that uses an auth middleware on protected routes to redirect users to my login page if no JWT cookie is detected. I'd rather not regenerate the HTML on every request to my login page. Is there any way to accomplish this? For example, can I pass the API response as props to my static login component? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Not sure why I was having trouble before, but I've figured it out now. Server Auth Express middleware: // ...
if (!req.cookies.token) res.redirect(`/login?redirect=${req.url}`);
// ... Login: import Router from "next/router";
// ...
function handleSubmit(event) {
event.preventDefault();
axios
.post("/login", { ... })
.then((response) => {
// ...
window.location.href = Router.query.redirect
? Router.query.redirect
: `/users/${response.data.user.username}`;
// ... |
Beta Was this translation helpful? Give feedback.
Not sure why I was having trouble before, but I've figured it out now.
Server Auth Express middleware:
Login: