Step 12
Let's update the app to use the dismissible messages in other views!
Let's have the logout process to set a message "You have successfully logged out!":
app.post("/logout", (req, res) => {
console.log(colors.cyan("Log out!"));
res.cookie("message", "You have successfully logged out!").redirect("/");
});
Also, let's update the registration process to display appropriate message
app.post("/register", async (req, res) => {
const username = req.body.username;
const password = req.body.password;
console.log(colors.cyan("Register:", { username, password }));
try {
// TODO register the user!
const user = await users.create({ username, password });
console.log(user);
// redirect to the login page
res.cookie("message", "You have successfully registered!").redirect("/");
} catch (err) {
console.log(err);
res.cookie("message", "Invalid username or password!").redirect("/register");
}
});
Also, update the following handler to provide the register
and dashboard
view with the message when it is available:
app.get("/register", (req, res) => {
const message = req.cookies.message;
res.render("register.njk", { message });
});
app.get("/dashboard", (req, res) => {
const username = req.query.username;
const message = req.cookies.message;
res.render("dashboard.njk", { username, message });
});
Save index.js
and visit http://localhost:5001/. Try visiting different routes and test the messaging works as intended.