{"id":111428,"date":"2021-09-20T07:00:00","date_gmt":"2021-09-20T04:00:00","guid":{"rendered":"https:\/\/www.javacodegeeks.com\/?p=111428"},"modified":"2021-09-10T15:44:42","modified_gmt":"2021-09-10T12:44:42","slug":"local-authentication-using-passport-in-node-js","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2021\/09\/local-authentication-using-passport-in-node-js.html","title":{"rendered":"Local Authentication Using Passport in Node.js"},"content":{"rendered":"<p>Hello. This tutorial will explain authentication in the Node.js applications through the passport module.<\/p>\n<h2>1. Introduction<\/h2>\n<p><strong>Passport.js<\/strong> is an authentication middleware designed for Nodejs. <em>passport-local<\/em> uses the passport strategy for authenticating with a username and password. The module helps to authenticate using a username and password in the nodejs applications. If you are interested in reading further about this module take a look at this <a href=\"http:\/\/www.passportjs.org\/packages\/passport-local\/\" target=\"_blank\" rel=\"noopener\">link<\/a>.<\/p>\n<h3>1.1 Setting up Node.js<\/h3>\n<p>To set up <strong>Node.js<\/strong> on windows you will need to download the installer from <a href=\"https:\/\/nodejs.org\/en\/download\/\" target=\"_blank\" rel=\"noopener\">this<\/a> link. Click on the installer (also include the NPM package manager) for your platform and run the installer to start with the Node.js setup wizard. Follow the wizard steps and click on Finish when it is done. If everything goes well you can navigate to the command prompt to verify if the installation was successful as shown in Fig. 1.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/09\/node-npm-installation-img1.jpg\"><img decoding=\"async\" width=\"480\" height=\"91\" src=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/09\/node-npm-installation-img1.jpg\" alt=\"Passport in Node.js - npm installation\" class=\"wp-image-111429\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/09\/node-npm-installation-img1.jpg 480w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/09\/node-npm-installation-img1-300x57.jpg 300w\" sizes=\"(max-width: 480px) 100vw, 480px\" \/><\/a><figcaption>Fig. 1: Verifying node and npm installation<\/figcaption><\/figure>\n<\/div>\n<h2>2. Local Authentication Using Passport in Node.js<\/h2>\n<p>To set up the application, we will need to navigate to a path where our project will reside. For programming stuff, I am using <a href=\"https:\/\/code.visualstudio.com\/\" target=\"_blank\" rel=\"noopener\">Visual Studio Code<\/a> as my preferred IDE. You&#8217;re free to choose the IDE of your choice.<\/p>\n<h3>2.1 Setting up the implementation<\/h3>\n<p>Let us write the different files which will be required for practical learning.<\/p>\n<h4>2.1.1 Setting up dependencies<\/h4>\n<p>Navigate to the project directory and run <code>npm init -y<\/code> to create a <code>package.json<\/code> file. This <a href=\"https:\/\/docs.npmjs.com\/creating-a-package-json-file\" target=\"_blank\" rel=\"noopener\">file<\/a> holds the metadata relevant to the project and is used for managing the project dependencies, script, version, etc. Add the following code to the file wherein we will specify the required dependencies.<\/p>\n<p><span style=\"text-decoration: underline\"><em>package.json<\/em><\/span><\/p>\n<pre class=\"brush:json;\">{\n  \"name\": \"passport-app\",\n  \"version\": \"1.0.0\",\n  \"description\": \"\",\n  \"main\": \"server.js\",\n  \"scripts\": {\n    \"start\": \"nodemon server.js\"\n  },\n  \"keywords\": [],\n  \"author\": \"\",\n  \"license\": \"ISC\",\n  \"dependencies\": {\n    \"bcrypt\": \"^5.0.1\",\n    \"ejs\": \"^3.1.6\",\n    \"express\": \"^4.17.1\",\n    \"express-flash\": \"0.0.2\",\n    \"express-session\": \"^1.17.2\",\n    \"method-override\": \"^3.0.0\",\n    \"passport\": \"^0.4.1\",\n    \"passport-local\": \"^1.0.0\"\n  },\n  \"devDependencies\": {\n    \"dotenv\": \"^10.0.0\",\n    \"nodemon\": \"^2.0.12\"\n  }\n}\n<\/pre>\n<p>To download the dependencies navigate to the directory path containing the file and use the <code>npm install<\/code> command. If everything goes well the dependencies will be loaded inside the <code>node_modules<\/code> folder and you are good to go with the further steps.<\/p>\n<h4>2.1.2 View \u2013 Creating welcome screen<\/h4>\n<p>In the root folder create a folder named <code>views<\/code> and add the following content to the <code>index.ejs<\/code>. This screen will be responsible to show the welcome page after successful authentication.<\/p>\n<p><span style=\"text-decoration: underline\"><em>index.ejs<\/em><\/span><div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush:js;\">&lt;h1&gt;Hi &lt;%= name %&gt;&lt;\/h1&gt;\n&lt;form action=\"\/logout?_method=DELETE\" method=\"POST\"&gt;\n  &lt;button type=\"submit\"&gt;Sign Out&lt;\/button&gt;\n&lt;\/form&gt;\n<\/pre>\n<h4>2.1.3 View \u2013 Creating login screen<\/h4>\n<p>In the root folder create a folder named <code>views<\/code> and add the following content to the <code>login.ejs<\/code>. This screen will be responsible for the login.<\/p>\n<p><span style=\"text-decoration: underline\"><em>index.ejs<\/em><\/span><\/p>\n<pre class=\"brush:js;\">&lt;h1&gt;Login&lt;\/h1&gt;\n&lt;% if (messages.error) { %&gt; &lt;%= messages.error %&gt; &lt;% } %&gt;\n&lt;form action=\"\/login\" method=\"POST\"&gt;\n  &lt;div&gt;\n    &lt;label for=\"email\"&gt;Email&lt;\/label&gt;\n    &lt;input type=\"email\" id=\"email\" name=\"email\" required \/&gt;\n  &lt;\/div&gt;\n  &lt;div&gt;\n    &lt;label for=\"password\"&gt;Password&lt;\/label&gt;\n    &lt;input type=\"password\" id=\"password\" name=\"password\" required \/&gt;\n  &lt;\/div&gt;\n  &lt;button type=\"submit\"&gt;Login&lt;\/button&gt;\n&lt;\/form&gt;\n&lt;a href=\"\/register\"&gt;Sign up&lt;\/a&gt;\n<\/pre>\n<h4>2.1.4 View \u2013 Creating registration screen<\/h4>\n<p>In the root folder create a folder named <code>views<\/code> and add the following content to the <code>login.ejs<\/code>. This screen will be responsible for the registration of new users.<\/p>\n<p><span style=\"text-decoration: underline\"><em>index.ejs<\/em><\/span><\/p>\n<pre class=\"brush:js;\">&lt;h1&gt;Register&lt;\/h1&gt;\n&lt;form action=\"\/register\" method=\"POST\"&gt;\n  &lt;div&gt;\n    &lt;label for=\"name\"&gt;Name&lt;\/label&gt;\n    &lt;input type=\"text\" id=\"name\" name=\"name\" required \/&gt;\n  &lt;\/div&gt;\n  &lt;div&gt;\n    &lt;label for=\"email\"&gt;Email&lt;\/label&gt;\n    &lt;input type=\"email\" id=\"email\" name=\"email\" required \/&gt;\n  &lt;\/div&gt;\n  &lt;div&gt;\n    &lt;label for=\"password\"&gt;Password&lt;\/label&gt;\n    &lt;input type=\"password\" id=\"password\" name=\"password\" required \/&gt;\n  &lt;\/div&gt;\n  &lt;button type=\"submit\"&gt;Register&lt;\/button&gt;\n&lt;\/form&gt;\n&lt;a href=\"\/login\"&gt;Login&lt;\/a&gt;\n<\/pre>\n<h4>2.1.5 Creating passport configuration<\/h4>\n<p>In the root folder add the following content to the configuration file. The file will be responsible to configure the strategy using a username and password. The strategy will also require a callback that will accept the credentials and calls a <code>done(\u2026)<\/code> method to provide the user details.<\/p>\n<p><span style=\"text-decoration: underline\"><em>passport-config.js<\/em><\/span><\/p>\n<pre class=\"brush:js;\">\/\/ adding passport related configuration\n\nconst LocalStrategy = require(\"passport-local\").Strategy;\nconst bcrypt = require(\"bcrypt\");\n\nfunction initialize(passport, getUserByEmail, getUserById) {\n  const authenticateUser = async (email, password, done) =&gt; {\n    const user = getUserByEmail(email);\n    if (user == null) {\n      return done(null, false, { message: \"User not found\" });\n    }\n\n    try {\n      if (await bcrypt.compare(password, user.password)) {\n        return done(null, user);\n      } else {\n        return done(null, false, { message: \"Invalid credentials\" });\n      }\n    } catch (e) {\n      return done(e);\n    }\n  };\n\n  passport.use(new LocalStrategy({ usernameField: \"email\" }, authenticateUser));\n  passport.serializeUser((user, done) =&gt; done(null, user.id));\n  passport.deserializeUser((id, done) =&gt; {\n    return done(null, getUserById(id));\n  });\n}\n\nmodule.exports = initialize;\n<\/pre>\n<h4>2.1.6 Creating controller<\/h4>\n<p>In the root folder add the following content to the index file. The file will be responsible to initialize the imports, routes, and specify the passport configuration to authenticate the requests. Remember to create a <code>.env<\/code> file at the same location and specify the sensitive information such as session-secret, application port number, etc.<\/p>\n<p><span style=\"text-decoration: underline\"><em>server.js<\/em><\/span><\/p>\n<pre class=\"brush:js;\">if (process.env.NODE_ENV !== \"production\") {\n  require(\"dotenv\").config();\n}\n\n\/\/ imports\nconst express = require(\"express\");\nconst app = express();\nconst bcrypt = require(\"bcrypt\");\nconst passport = require(\"passport\");\nconst flash = require(\"express-flash\");\nconst session = require(\"express-session\");\nconst methodOverride = require(\"method-override\");\n\n\/\/ todo - add external db support\nconst users = [];\n\n\/\/ configuring and initializing passport\nconst initializePassport = require(\".\/passport-config\");\ninitializePassport(\n  passport,\n  (email) =&gt; users.find((user) =&gt; user.email === email),\n  (id) =&gt; users.find((user) =&gt; user.id === id)\n);\n\napp.set(\"view-engine\", \"ejs\");\napp.use(express.urlencoded({ extended: false }));\napp.use(flash());\napp.use(\n  session({\n    secret: process.env.SESSION_SECRET || \"8unto0n4oc7903zm\",\n    resave: false,\n    saveUninitialized: false,\n  })\n);\napp.use(passport.initialize());\napp.use(passport.session());\napp.use(methodOverride(\"_method\"));\n\n\/\/ routes\n\n\/\/ welcome page\n\/\/ display greetings message for the user and logout button\napp.get(\"\/\", checkAuthenticated, (req, res) =&gt; {\n  res.render(\"index.ejs\", { name: req.user.name });\n});\n\n\/\/ login page\napp.get(\"\/login\", checkNotAuthenticated, (req, res) =&gt; {\n  res.render(\"login.ejs\");\n});\n\napp.post(\n  \"\/login\",\n  checkNotAuthenticated,\n  passport.authenticate(\"local\", {\n    successRedirect: \"\/\",\n    failureRedirect: \"\/login\",\n    failureFlash: true,\n  })\n);\n\n\/\/ new user sign-up page\napp.get(\"\/register\", checkNotAuthenticated, (req, res) =&gt; {\n  res.render(\"register.ejs\");\n});\n\napp.post(\"\/register\", checkNotAuthenticated, async (req, res) =&gt; {\n  try {\n    const hashedPassword = await bcrypt.hash(req.body.password, 10);\n    users.push({\n      id: \"_\" + Math.random().toString(36).slice(2),\n      name: req.body.name,\n      email: req.body.email,\n      password: hashedPassword,\n    });\n\n    res.redirect(\"\/login\");\n  } catch (e) {\n    \/\/ console.log(e);\n    res.redirect(\"\/redirect\");\n  }\n\n  \/\/ check if the user is successfully added to array\n  \/\/ console.log(users);\n});\n\n\/\/ logout of the application\napp.delete(\"\/logout\", (req, res) =&gt; {\n  req.logOut();\n  res.redirect(\"\/login\");\n});\n\n\/\/ util methods\n\n\/\/ only authenticated user should enter index page\nfunction checkAuthenticated(req, res, next) {\n  if (req.isAuthenticated()) {\n    return next();\n  } else {\n    res.redirect(\"\/login\");\n  }\n}\n\n\/\/ unauthenticated user should not enter index page\nfunction checkNotAuthenticated(req, res, next) {\n  if (req.isAuthenticated()) {\n    return res.redirect(\"\/\");\n  }\n  next();\n}\n\n\/\/ start server\nconst port = process.env.APPLICATION_PORT || 6001;\napp.listen(port, () =&gt; {\n  console.log(\"Server listening at http:\/\/localhost:%s\", port);\n});\n<\/pre>\n<h2>3. Run the Application<\/h2>\n<p>To run the application navigate to the project directory and enter the following command as shown in Fig. 2. If everything goes well the application will be started successfully on a port number read from the <code>.env<\/code> file or <code>6001<\/code>.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/09\/passportlocal-run-step.jpg\"><img decoding=\"async\" width=\"607\" height=\"169\" src=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/09\/passportlocal-run-step.jpg\" alt=\"Passport in Node.js - starting the app\" class=\"wp-image-111430\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/09\/passportlocal-run-step.jpg 607w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/09\/passportlocal-run-step-300x84.jpg 300w\" sizes=\"(max-width: 607px) 100vw, 607px\" \/><\/a><figcaption>Fig. 2: Starting the application<\/figcaption><\/figure>\n<\/div>\n<h2>4. Demo<\/h2>\n<p>Once the application is started successfully navigate to the following url to show the login screen. You can click on the signup button for creating a new user and play around with the application after that. In this tutorial, I have also added some of the basic validations like user not found and invalid credentials.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Endpoint<\/em><\/span><\/p>\n<pre class=\"brush:plain;\">http:\/\/localhost:YOUR_PORT_NUMBER<\/pre>\n<p>That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!<\/p>\n<h2>5. Summary<\/h2>\n<p>In this tutorial, we learned about the passport module and how to create a simple login application with the help of passport-local dependency. You can download the source code and the postman collection from the <a href=\"#projectDownload\">Downloads<\/a> section.<\/p>\n<h2><a name=\"projectDownload\"><\/a>6. Download the Project<\/h2>\n<p>This was a tutorial to implement local authentication using a passport module in a nodejs application.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>You can download the full source code of this example here: <a href=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2021\/09\/Local-Authentication-Using-Passport-in-Node.js_.zip\"><strong>Local Authentication Using Passport in Node.js<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hello. This tutorial will explain authentication in the Node.js applications through the passport module. 1. Introduction Passport.js is an authentication middleware designed for Nodejs. passport-local uses the passport strategy for authenticating with a username and password. The module helps to authenticate using a username and password in the nodejs applications. If you are interested in &hellip;<\/p>\n","protected":false},"author":26931,"featured_media":20900,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1879],"tags":[991,741],"class_list":["post-111428","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-express-js","tag-node-js"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Local Authentication Using Passport in Node.js - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Hello. This tutorial will explain authentication in the Node.js applications through the passport module. 1. Introduction Passport.js is an authentication\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2021\/09\/local-authentication-using-passport-in-node-js.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Local Authentication Using Passport in Node.js - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Hello. This tutorial will explain authentication in the Node.js applications through the passport module. 1. Introduction Passport.js is an authentication\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2021\/09\/local-authentication-using-passport-in-node-js.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-20T04:00:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/01\/javascript-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Yatin Batra\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Yatin Batra\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/09\\\/local-authentication-using-passport-in-node-js.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/09\\\/local-authentication-using-passport-in-node-js.html\"},\"author\":{\"name\":\"Yatin Batra\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/cda31a4c1965373fed40c8907dc09b8d\"},\"headline\":\"Local Authentication Using Passport in Node.js\",\"datePublished\":\"2021-09-20T04:00:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/09\\\/local-authentication-using-passport-in-node-js.html\"},\"wordCount\":720,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/09\\\/local-authentication-using-passport-in-node-js.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2014\\\/01\\\/javascript-logo.jpg\",\"keywords\":[\"Express.js\",\"Node.js\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/09\\\/local-authentication-using-passport-in-node-js.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/09\\\/local-authentication-using-passport-in-node-js.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/09\\\/local-authentication-using-passport-in-node-js.html\",\"name\":\"Local Authentication Using Passport in Node.js - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/09\\\/local-authentication-using-passport-in-node-js.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/09\\\/local-authentication-using-passport-in-node-js.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2014\\\/01\\\/javascript-logo.jpg\",\"datePublished\":\"2021-09-20T04:00:00+00:00\",\"description\":\"Hello. This tutorial will explain authentication in the Node.js applications through the passport module. 1. Introduction Passport.js is an authentication\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/09\\\/local-authentication-using-passport-in-node-js.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/09\\\/local-authentication-using-passport-in-node-js.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/09\\\/local-authentication-using-passport-in-node-js.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2014\\\/01\\\/javascript-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2014\\\/01\\\/javascript-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2021\\\/09\\\/local-authentication-using-passport-in-node-js.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Web Development\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/web-development\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"JavaScript\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/web-development\\\/javascript\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Local Authentication Using Passport in Node.js\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/cda31a4c1965373fed40c8907dc09b8d\",\"name\":\"Yatin Batra\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/12\\\/Yatin.batra_.jpg\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/12\\\/Yatin.batra_.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/12\\\/Yatin.batra_.jpg\",\"caption\":\"Yatin Batra\"},\"description\":\"An experience full-stack engineer well versed with Core Java, Spring\\\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).\",\"sameAs\":[\"https:\\\/\\\/www.javacodegeeks.com\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/yatin-batra\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Local Authentication Using Passport in Node.js - Java Code Geeks","description":"Hello. This tutorial will explain authentication in the Node.js applications through the passport module. 1. Introduction Passport.js is an authentication","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2021\/09\/local-authentication-using-passport-in-node-js.html","og_locale":"en_US","og_type":"article","og_title":"Local Authentication Using Passport in Node.js - Java Code Geeks","og_description":"Hello. This tutorial will explain authentication in the Node.js applications through the passport module. 1. Introduction Passport.js is an authentication","og_url":"https:\/\/www.javacodegeeks.com\/2021\/09\/local-authentication-using-passport-in-node-js.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2021-09-20T04:00:00+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/01\/javascript-logo.jpg","type":"image\/jpeg"}],"author":"Yatin Batra","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Yatin Batra","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2021\/09\/local-authentication-using-passport-in-node-js.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2021\/09\/local-authentication-using-passport-in-node-js.html"},"author":{"name":"Yatin Batra","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/cda31a4c1965373fed40c8907dc09b8d"},"headline":"Local Authentication Using Passport in Node.js","datePublished":"2021-09-20T04:00:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2021\/09\/local-authentication-using-passport-in-node-js.html"},"wordCount":720,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2021\/09\/local-authentication-using-passport-in-node-js.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/01\/javascript-logo.jpg","keywords":["Express.js","Node.js"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2021\/09\/local-authentication-using-passport-in-node-js.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2021\/09\/local-authentication-using-passport-in-node-js.html","url":"https:\/\/www.javacodegeeks.com\/2021\/09\/local-authentication-using-passport-in-node-js.html","name":"Local Authentication Using Passport in Node.js - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2021\/09\/local-authentication-using-passport-in-node-js.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2021\/09\/local-authentication-using-passport-in-node-js.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/01\/javascript-logo.jpg","datePublished":"2021-09-20T04:00:00+00:00","description":"Hello. This tutorial will explain authentication in the Node.js applications through the passport module. 1. Introduction Passport.js is an authentication","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2021\/09\/local-authentication-using-passport-in-node-js.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2021\/09\/local-authentication-using-passport-in-node-js.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2021\/09\/local-authentication-using-passport-in-node-js.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/01\/javascript-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/01\/javascript-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2021\/09\/local-authentication-using-passport-in-node-js.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Web Development","item":"https:\/\/www.javacodegeeks.com\/category\/web-development"},{"@type":"ListItem","position":3,"name":"JavaScript","item":"https:\/\/www.javacodegeeks.com\/category\/web-development\/javascript"},{"@type":"ListItem","position":4,"name":"Local Authentication Using Passport in Node.js"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/cda31a4c1965373fed40c8907dc09b8d","name":"Yatin Batra","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/12\/Yatin.batra_.jpg","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/12\/Yatin.batra_.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/12\/Yatin.batra_.jpg","caption":"Yatin Batra"},"description":"An experience full-stack engineer well versed with Core Java, Spring\/Springboot, MVC, Security, AOP, Frontend (Angular &amp; React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).","sameAs":["https:\/\/www.javacodegeeks.com"],"url":"https:\/\/www.javacodegeeks.com\/author\/yatin-batra"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/111428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/26931"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=111428"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/111428\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/20900"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=111428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=111428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=111428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}