1

I am building a web app, including routes for authentication.

The Problem: When a registration succeeds I want to redirect to /login, but also include render options. But when I render the login.ejs the url stays /register.

The Options: I either have to find a way to use res.render and change the url OR use res.redirect and also pass render variables.

This is minimal code to show my problem:

app.get("/login", (res, req) => {
 res.render("login.ejs", {flag: ""})
}

app.post("/register", (res, req) => {
  // registration logic
  if(success) {
    res.render("login.ejs", {flag: "registration_success"})
  } 
}
1cedsoda
  • 623
  • 4
  • 16
  • Does this answer your question? [How do I redirect in expressjs while passing some context?](https://stackoverflow.com/questions/19035373/how-do-i-redirect-in-expressjs-while-passing-some-context) – lambda Apr 10 '20 at 16:52
  • Similar question/answer here [Redirecting from one route to another with data from first](https://stackoverflow.com/questions/51162321/is-there-any-way-of-redirecting-from-one-route-to-another-with-data-from-first-r/51162748#51162748). – jfriend00 Apr 10 '20 at 17:00

1 Answers1

0

the url shown is what you write in app.post("/someURL" not what you redirect to.

so if u want to redirect to login page after successful registration, simply redirect to "/login". it renders "login.ejs"

about the part that you probably want to show a sign up success message, u can use 'flash' package; it helps you add data to memory and get it in client side and show a success message. I use sweetalert2 in such a way:

in back-end code:

req.flash('a-name-you-want', { flags, you, want });

to get these info in front-end:

<% let yourData = req.flash('the-name')
if(yourData.length) {
  // do sth to the data
}%>

I hope it helped you!

arianpress
  • 456
  • 1
  • 6
  • 16