Categories
JavaScript Answers

How to implement login auth in Node.js?

To implement login auth in Node.js, we add a middleware to check for authentication and a login and logout route.

For instance, we write

const checkAuth = (req, res, next) => {
  if (!req.session.userId) {
    res.send("You are not authorized to view this page");
  } else {
    next();
  }
};

app.get("/my_secret_page", checkAuth, (req, res) => {
  res.send("You are logged in");
});

app.post("/login", (req, res) => {
  const post = req.body;
  if (post.user === "john" && post.password === "johnspassword") {
    //...
    req.session.userId = userId;
    res.redirect("/my_secret_page");
  } else {
    res.send("Bad user/pass");
  }
});

app.get("/logout", (req, res) => {
  delete req.session.userId;
  res.redirect("/login");
});

to define the checkAuth middleware function to check for the userId property in the session.

If it’s set, then the user is logged in and we call next.

Otherwise, we call res.send to return an error response.

Then we add the /my_secret_page route with app.get.

We call checkAuth before calling the route handler to check for authentication before sending the response.

Next we add the /login route that checks for the user and password from the req.body request body.

If they match, then we set req.session.userId since the user is authenticated.

And we call redirect to the /my_secret_page route.

Otherwise, we send an error response.

Finally, we add a /logout route that deletes the userId from req.session and redirect to /login

Categories
JavaScript Answers

How to run app inside Docker as non-root user?

To run app inside Docker as non-root user, we add a user that doesn’t require a password to log in.

For instance, we run

adduser --disabled-password --gecos '' r
adduser r sudo
echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
su -m r -c /home/r/script.sh

to add the user with adduser.

We add it to the list of users that can run sudo with adduser r sudo.

Then we run the command we want with su -m r -c /home/r/script.sh.

Categories
JavaScript Answers

How to add multiple optional route parameters in Node Express?

To add multiple optional route parameters in Node Express, we add question marks after the parameter placeholders.

For instance, we write

app.get("/articles/:year?/:month?/:day?", (req, res) => {
  const year = req.params.year;
  const month = req.params.month;
  const day = req.params.day;
  //...
});

to add a get route handler with the year, month and day route parameters made option with ?.

We get the route parameter values from the req.params object.

Categories
JavaScript Answers

How to install Yarn in Ubuntu?

To install Yarn in Ubuntu, we add the package repository with Yarn before we install it.

We run

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

to add the Yarn package repo.

And then we run

sudo apt-get update && sudo apt-get install yarn

to run apt to install yarn.

Categories
JavaScript Answers

How to set Node engine to 8.x or 10.x in package.json?

To set Node engine to 8.x or 10.x in package.json, we set the engines property.

For instance, in package.json, we write

{
  "engines": {
    "node": "^8 || ^10"
  }
}

to add the engines property into the JSON.

We set the Node engine to 8.x or 10.x with ^8 || ^10.