<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Prince Singh</title>
    <description>The latest articles on Forem by Prince Singh (@princesingh19).</description>
    <link>https://forem.com/princesingh19</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F991194%2F3573949d-7e5f-4df7-965f-9a1541e03cd1.jpeg</url>
      <title>Forem: Prince Singh</title>
      <link>https://forem.com/princesingh19</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/princesingh19"/>
    <language>en</language>
    <item>
      <title>Token Based Authentication using Credentials in NodeJS</title>
      <dc:creator>Prince Singh</dc:creator>
      <pubDate>Sat, 23 Dec 2023 06:58:22 +0000</pubDate>
      <link>https://forem.com/princesingh19/token-based-authentication-using-credentials-in-nodejs-47gc</link>
      <guid>https://forem.com/princesingh19/token-based-authentication-using-credentials-in-nodejs-47gc</guid>
      <description>&lt;p&gt;Hello coders,&lt;br&gt;
I am going to talk about the modern way of token-based authentication using NodeJS, so if you are looking for a start or want to build your own then you have come to right place.&lt;/p&gt;

&lt;p&gt;In this post I am going to implement the basic token-based authentication system using ExpressJS, PassportJS and other helpful  libraries.&lt;/p&gt;

&lt;p&gt;So let's start&lt;/p&gt;

&lt;p&gt;First run the command&lt;br&gt;
&lt;code&gt;npm init -y&lt;/code&gt;&lt;br&gt;
It will create a package.json file for you with all the necessary defaults&lt;/p&gt;

&lt;p&gt;Now we need to install the following packages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;ExpressJS&lt;/li&gt;
&lt;li&gt;PassportJS&lt;/li&gt;
&lt;li&gt;HelmetJS&lt;/li&gt;
&lt;li&gt;Nodemon&lt;/li&gt;
&lt;li&gt;Passport-JWT&lt;/li&gt;
&lt;li&gt;Passport-Local&lt;/li&gt;
&lt;li&gt;JsonWebToken&lt;/li&gt;
&lt;li&gt;Cookie-Parser&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;to install these packages run the following command&lt;br&gt;
&lt;code&gt;npm i express helmet jsonwebtoken passport passport-jwt passport-local nodemon cookie-parser&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After this we need to make some changes in the package.json file so that we don't have to run the script manually everytime to see the changes. That's exactly where &lt;code&gt;nodemon&lt;/code&gt; comes into the picture. It looks for the changes into the specific file and if there is any change it restarts the server. &lt;/p&gt;

&lt;p&gt;Let's add the following script into the scripts section in the &lt;code&gt;package.json&lt;/code&gt; file&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"start": "nodemon server.js"&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Now let's move ahead. Following is the file &amp;amp; folder structure we are going to follow to organize our code&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqfgubritfmbnkhsamse9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqfgubritfmbnkhsamse9.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So far so good. We have organized our code. Now it's time to get our hands dirty by writing some code.&lt;/p&gt;

&lt;p&gt;We will start with &lt;code&gt;server.js&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require("express");
// create app instance from express
const app = express();

// specify the port number for your app
const PORT = 3000;

// home page route
app.get("/", (req, res) =&amp;gt; res.send("Hi"));

app.listen(PORT, (req, res) =&amp;gt; {
    console.log(`Connected on http://localhost:${PORT}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if you run &lt;code&gt;npm run start&lt;/code&gt; in the terminal then your server will start and run on the port 3000.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: We are not using database at this point to save ourselves from additional database setup. We are using a simple array of objects user model.&lt;br&gt;
If you need this with database then comment down. I will create another post with the database setup.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So let's create a user with array of objects in &lt;code&gt;users.js&lt;/code&gt; file in the models folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const users = [
    { username: "Prince", password: "password1" },
    { username: "Kathan", password: "password2" },
    { username: "Srivinay", password: "password3" },
    { username: "Siddharth", password: "password4" },
];
module.exports = users;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add below html page for login route in the &lt;code&gt;login.html&lt;/code&gt; file in public folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
    &amp;lt;head&amp;gt;
        &amp;lt;meta charset="UTF-8" /&amp;gt;
        &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&amp;gt;
        &amp;lt;title&amp;gt;NodeJS Authentication&amp;lt;/title&amp;gt;
    &amp;lt;/head&amp;gt;
    &amp;lt;body&amp;gt;
        &amp;lt;h1&amp;gt;This is the HtML sent by the server&amp;lt;/h1&amp;gt;
        &amp;lt;a href="/secret"&amp;gt;Show Me!&amp;lt;/a&amp;gt;
        &amp;lt;a href="/auth/google"&amp;gt;Google Login&amp;lt;/a&amp;gt;
        &amp;lt;a href="/auth/logout"&amp;gt;Logout&amp;lt;/a&amp;gt;
        &amp;lt;!-- &amp;lt;img src="./logo.png" alt="logo img" /&amp;gt; --&amp;gt;
    &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this we are going to edit our &lt;code&gt;passport.js&lt;/code&gt; file to add the passportjs with  Token based authentication to our app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const passport = require("passport");
const JwtStrategy = require("passport-jwt").Strategy;
const ExtractJwt = require("passport-jwt").ExtractJwt;

const users = require("./model/users");

// function to extract the cookie from the response header
var cookieExtractor = function (req) {
    var token = null;
    if (req &amp;amp;&amp;amp; req.cookies) {
        token = req.cookies["secure_token"];
    }
    return token;
};

// function to use the jwt as strategy to authenticate the users
function passportAuth() {
    try {
        passport.use(
            new JwtStrategy(
                {
                    jwtFromRequest: ExtractJwt.fromExtractors([cookieExtractor]),
                    secretOrKey: "my_secret_key",
                },
                function (jwt_payload, done) {
                    const user = users.find((user) =&amp;gt; jwt_payload.username === user.username);
                    if (!user) {
                        return done(null, false);
                    }

                    return done(null, user);
                }
            )
        );
    } catch (error) {
        throw error;
    }
}

module.exports = {
    passportAuth,
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In above code we are first importing the necessary packages then I have create a &lt;code&gt;cookieExtractor&lt;/code&gt; function which is used to extract the cookies from the headers and pass the value of the cookies to passport. Please note that we have used cookies specially to save the authentication tokens so that we don't have to include them  seperately in each request if which is the case if we save them in the localStorage or sessionStorage. Sure storing the tokens in cookies have some downside and is susceptible to xsrf but it can be  avoided by setting the cookies to httponly, secure and maxAge properties.&lt;/p&gt;

&lt;p&gt;After that we using the &lt;code&gt;passportjs&lt;/code&gt; middleware which runs on every request that is beign made to our server and extract the cookies from header. It then decodes the token extracted from cookie using a secretKey which you have to set in &lt;code&gt;.env&lt;/code&gt; file and extracts the user information from tokens. We are verifying here based on the username only for now to keep things simple. If it founds the user then it returns the user's properties or else returns false.&lt;/p&gt;

&lt;p&gt;Now let's create the login routes&lt;/p&gt;

&lt;p&gt;Add following code to &lt;code&gt;loginRoutes.js&lt;/code&gt; file in the routes folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require("express");
const loginRouter = express.Router();

//importing the getLoginForm and postLoginDetails from controllers/loginController.js file
const { getLoginForm, postLoginDetails } = require("../controllers/loginController");

// router for the GET method of the login form
loginRouter.get("/", getLoginForm);

// route for the POST method of the login form
loginRouter.post("/", postLoginDetails);

module.exports = loginRouter;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this we are going to edit the &lt;code&gt;loginController.js&lt;/code&gt; file in the controllers folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const path = require("path");
const jwt = require("jsonwebtoken");

const users = require("../model/users");

// function to serve the login page to the user
const getLoginForm = (req, res) =&amp;gt; {
    res.sendFile(path.join(__dirname, "..", "public", "login.html"));
};

// function to handle the login request
const postLoginDetails = (req, res) =&amp;gt; {
    // extracting the username and password from the body of the request
    const { username, password } = req.body;
    const user = users.find((user) =&amp;gt; user.username === username);

    // sending 404 error along with the message if user is not found in the database
    if (!user) {
        return res.status(404).json({ message: "INCORRECT_USERNAME" });
    } else {
        // generating the token and with the information and sending it to the user on the client end
        const token = jwt.sign({ username: username }, "my_secret_key");

        // setting the token in the cookie so that we don't need to include it manually in each request
        res.cookie("secure_token", token, {
            httpOnly: true, // Ensures cookie cannot be accessed by JavaScript
            maxAge: 1000 * 60 * 60, // Expire in 1 hour,
        });

        // returning the response containing the token and the message
        return res.json({
            message: "sucessfully authentiacted",
            token,
        });
    }
};

// function to add the authentication on the selective routes using the jwt strategy
function authenticate(req, res, next) {
    // Use Passport to authenticate the request using the "jwt" strategy
    passport.authenticate("jwt", { session: false }, (err, user) =&amp;gt; {
        console.log(user);
        if (err) next(err); // If there's an error, pass it on to the next middleware
        if (!user) {
            // If the user is not authenticated, send a 401 Unauthorized response
            return res.status(401).json({
                message: "Unauthorized access. No token provided.",
            });
        }
        // If the user is authenticated, attach the user object to the request and move on to the next middleware
        req.user = user;
        next();
    })(req, res, next);
}

module.exports = {
    passportAuth,
    authenticate,
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In above code we are importing the necessary packages. After that we are serving the login.html file which is served to the user after he visits the &lt;code&gt;/login&lt;/code&gt; route in the &lt;code&gt;getLoginForm&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;Now moving on we have &lt;code&gt;postLoginDetails&lt;/code&gt; function which first extracts the user's username and password from the request, then looks for the that user in the users model. If it founds the user then it creates a token using the users information and then sets that token in the cookie. We have already talked about why we are using cookies approach. And finally send the successful message to the client side. In case if user is not found then also we are sending &lt;code&gt;"INCORRECT_USERNAME"&lt;/code&gt; message to the client side.&lt;/p&gt;

&lt;p&gt;After that we have added the &lt;code&gt;authenticate&lt;/code&gt; function middleware which you can use to implement the authentication on selective routes. It first extracts the JWT tokens with the help of passportjs and looks for user. If user is found then it returns the user or returns the error message.&lt;/p&gt;

&lt;p&gt;Now going back to &lt;code&gt;server.js&lt;/code&gt; file and adding the passportjs middleware and login routes&lt;/p&gt;

&lt;p&gt;Modifying our existing file like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require("express");
const helmet = require("helmet");
const passport = require("passport");
const cookieParser = require("cookie-parser");

const { passportAuth } = require("./passport");
const loginRouter = require("./routes/loginRouter");

// create app instance from express
const app = express();

// specify the port number for your app
const PORT = 3000;

// using this helmet middleware to set extra attributes on the request headers
app.use(helmet());

// middleware to parse the cookies in the incoming requests
app.use(cookieParser());

// including the passportjs middleware
passportAuth();

app.use(express.urlencoded({ extended: false }));

// initialising the passportjs middleware
app.use(passport.initialize());

// home page route
app.get("/", (req, res) =&amp;gt; res.send("Hi"));

// login route
app.use("/login", loginRouter);


// secret page route which requires the user to be authenticated
app.get("/secret", authenticate, (req, res) =&amp;gt; {
    res.send("secret value is 19");
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have made couple of changes. We have added the helmet middleware so set extra necessary headers, cookieParser middleware to parse the cookies from the requests and added the passportjs middleware. After that we are intializing the passportjs middleware. Lastly we are creating the routes for the /login route.&lt;/p&gt;

&lt;p&gt;At this point we have made all the heavy lifting. &lt;br&gt;
Now if you go the &lt;code&gt;http://localhost:3000/&lt;/code&gt; route you should see "Hi".&lt;/p&gt;

&lt;p&gt;If you go to &lt;code&gt;http://localhost:3000/secret&lt;/code&gt; endpoint then you will get the error message saying "Unauthorized access. No token provided". It means our authentication is working. &lt;/p&gt;

&lt;p&gt;Now go to &lt;code&gt;http://localhost:3000/login&lt;/code&gt; and you will see the two input fields in which first field is for username and last field is for password. &lt;/p&gt;

&lt;p&gt;When you enter any username listed in the users model you will get token and successful message. If you enter wrong username then you will get error. &lt;/p&gt;

&lt;p&gt;After this if you again go to &lt;code&gt;http://localhost:3000/secret&lt;/code&gt; endpoint you will get you secret endpoint's content which in our case is 'Your secret value is 19'.&lt;/p&gt;

&lt;p&gt;Woo hoo! You have successfully added the modern token based authentication in your app.&lt;/p&gt;

&lt;p&gt;I hope this article gave you the basic knowledge about implementing  authentication logic in your app.&lt;/p&gt;

&lt;p&gt;There are still many areas of improvement in the code like verifying the password along with username, adding database support , signup process and many more. &lt;br&gt;
So try them on your own and if stuck, comment below, I will create another article along with github repo for you.&lt;/p&gt;

&lt;p&gt;Happy Coding!! :)&lt;/p&gt;

</description>
      <category>authentication</category>
      <category>node</category>
      <category>passportjs</category>
      <category>jwt</category>
    </item>
  </channel>
</rss>
