How to check user authentication in GET method using Node.js ? Last Updated : 22 Apr, 2021 Comments Improve Suggest changes Like Article Like Report There are so many authentication methods like web token authentication, cookies based authentication, and many more. In this article, we will discuss one of the simplest authentication methods using express.js during handling clients get a request in node.js with the help of the HTTP headers. Approach: HTTP protocols used various types of headers for authentication the client we will use the WWW-Authenticate header. HTTP WWW-Authenticate header is a response-type header, and it serves as a support for various authentication mechanisms which are important to control access to pages and other resources as well. Explanation of the authentication: When the request header of the client does not contain WWW-Authenticate header servers response header set the header is res.setHeader("WWW-Authenticate", 'Basic') and set status code 401 and after this, a pop will appear on the client-side for valid authentication. Authentication Form: Module Installation: Install the express module using the following command. npm install expressProject structure: index.js // Importing required modules const { response } = require("express"); const express = require("express"); const app=express() // Handling get request from the client side app.get("/",(req,res,next)=>{ // Checking the header of the authorization var authheader=req.headers.authorization; console.log(authheader) if(!authheader){ var err=new Error("You are not authenticated") // Set the header for the response res.setHeader("WWW-Authenticate",'Basic') err.status=401 return next(err) } console.log(authheader) // Decrypt the user name and the password var auth = new Buffer.from(authheader.split(' ')[1], 'base64').toString().split(':'); var user = auth[0]; var pass = auth[1]; // Checking the details if (user == 'admin' && pass == 'password') { res.send("Welcome you are authorized") } else { var err = new Error('You are not authenticated!'); res.setHeader('WWW-Authenticate', 'Basic'); err.status = 401; return next(err); } }) app.listen(3000,()=>{ console.log("Server is starting") }) Run index.js using the following command: node index.js Output: Open any browser with http://localhost:3000 location in a private window(in order to avoid a saved password and username). A pop will occur near the address bar. Fill in the username and password that are mention in the code. If the entered username and password match the mention, then location index.html will render on the browser. Explanation: The first middleware is used for checking the authentication of the client when the server start and the client enter the localhost address. Initially req.headers.authorization is undefined and next() callback function return 401 status code unauthorized access to the browser. The client fills the credentials and the credentials encrypted in base64 format. After that, it decrypts the base64 format data that contains username and password, then after checking the username and password is correct, the next() method calls the next middleware that is mention below the authentication middleware, otherwise the authentication form pop again and again. Request Header Details: Comment More infoAdvertise with us Next Article How to check user authentication in GET method using Node.js ? zack_aayush Follow Improve Article Tags : Web Technologies Node.js NodeJS-Questions Similar Reads How to add authentication in file uploads using Node.js ? There are multiple ways to upload files and apply authentications to them. The easiest way to do so is to use a node module called multer. We can add authentication by restricting users on file uploads such as they can upload only pdf and the file size should be less than 1 Mb. There are many module 3 min read Basic Authentication in Node.js using HTTP Header Basic Authentication is a simple authentication method where the client sends a username and password encoded in base64 format in the HTTP request header.The basic authentication in the Node.js application can be done with the help express.js framework. Express.js framework is mainly used in Node.js 3 min read How to Connect Mongodb Authentication by Node.js? MongoDB is a popular NoSQL database that provides high performance, high availability, and easy scalability. In many applications, you need to connect to a MongoDB database with authentication to ensure data security. This article will guide you through the process of connecting to a MongoDB databas 3 min read Google Authentication using Passport in Node.js The following approach covers how to authenticate with google using passport in nodeJs. Authentication is basically the verification of users before granting them access to the website or services. Authentication which is done using a Google account is called Google Authentication. We can do Google 2 min read Adding User Authentication in Next.js using Auth0 Auth0 is a flexible, drop-in solution to add authentication and authorization services to your applications. Integrating Auth0 with Next.js allows you to manage user authentication seamlessly. In this article, we will learn How we can add user authentication in our NextJS project using Auth0.Approac 3 min read How to Make GET call to an API using Axios in JavaScript? Axios is a promise-based HTTP client designed for Node.js and browsers. With Axios, we can easily send asynchronous HTTP requests to REST APIs and perform create, read, update and delete operations. It is an open-source collaboration project hosted on GitHub. It can be imported in plain JavaScript o 3 min read Mastering JWT authentication in Express While creating any application it is very important to add authentication to ensure that only authorized users can access the protected resources. A JSON Web Token (JWT) is a JSON object utilized to securely transmit information between two parties over the web. Primarily employed in authentication 4 min read JWT Authentication In Node.js In modern web development, ensuring secure and efficient user authentication is paramount. JSON Web Tokens (JWT) offer a robust solution for token-based authentication, enabling secure transmission of user information between parties. This article provides a step-by-step approach to implementing JWT 3 min read Multi Factor authentication using MERN This article will guide you through creating a Multi-Factor Authentication (MFA) project using the MERN. This project aims to enhance security by implementing a multi-step authentication process. Users will be required to provide multiple forms of identification for access, adding an extra layer of 5 min read Beginner's Guide to User Authentication in MERN Stack User authentication is an essential aspect of web development, allowing users to securely register, log in, and access protected resources within an application. This article aims to provide a beginner-friendly guide to implementing user authentication in a MERN (MongoDB, Express.js, React.js, Node. 6 min read Like