Redirect with message

Hi All,

I am working on a login system where users after successfully registration are to be redirected to login page with some message like “successfully registered” to be shown on login.html page. How to achieve this using below code

return redirect("/login")

Thanks !

This is a rather difficult question to answer since it depends entirely upon how you store state in your application. You of course could handle it completely with cookies, or with DB storage. We would need to know more about what you are trying to build and the architecture involved.

Typically such messages are passed in query parameters in the URL you redirect to. If the login page is rendered server side, the login page handler needs to then extract the message from its URL (and be sure to escape it before including it in HTML to prevent XSS exploits).

Because a message in URL is rather ugly, some choose to use cookies for this. The redirect response sets a cookie that the login page reads and immediately deletes. This approach is technically a bit flawed because it could be that another browser tab or other request could accidentally receive the cookie, instead of the tab where the user is registering.

If your login system is rather a SPA based on Javascript, possibly doing login/registration by fetch(), you may not need any redirection, as the page can maintain its state in Javascript variables or a store of the framework you are using.