<?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>DEV Community: Dr Emmanuel Okoh</title>
    <description>The latest articles on DEV Community by Dr Emmanuel Okoh (@dremmaokoh).</description>
    <link>https://dev.to/dremmaokoh</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%2F907724%2F655caf0b-60e8-4e21-af5b-8484d4923374.jpeg</url>
      <title>DEV Community: Dr Emmanuel Okoh</title>
      <link>https://dev.to/dremmaokoh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dremmaokoh"/>
    <language>en</language>
    <item>
      <title>JWT for authentication in Nodejs and how Oauth2 address some of the problems that comes with it</title>
      <dc:creator>Dr Emmanuel Okoh</dc:creator>
      <pubDate>Tue, 30 Aug 2022 08:06:36 +0000</pubDate>
      <link>https://dev.to/dremmaokoh/jwt-for-authentication-in-nodejs-and-how-oauth2-address-some-of-the-problems-that-comes-with-it-4h9f</link>
      <guid>https://dev.to/dremmaokoh/jwt-for-authentication-in-nodejs-and-how-oauth2-address-some-of-the-problems-that-comes-with-it-4h9f</guid>
      <description>&lt;p&gt;In this article we will be learning;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;what JWT stands for&lt;/li&gt;
&lt;li&gt;How to use JWT to secure endpoint&lt;/li&gt;
&lt;li&gt;Definition of Oauth2&lt;/li&gt;
&lt;li&gt;Advantages of Oauth2 over JWT&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  what is JWT ?
&lt;/h2&gt;

&lt;p&gt;JWT stands for 'JSON Web Tokens' and they are an open, industry-standard RFC 7519 method for representing claims securely between two parties.&lt;/p&gt;

&lt;p&gt;These tokens were created by the server and contain essential information about the user in question. Its data collection consists primarily of email addresses, user IDs, passwords, login information, and other information. All of the records made with it are saved in JSON format. Additionally, it effectively employs cryptography.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use JWT to secure an endpoint?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Stack Used
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Vscode: Work environment&lt;/li&gt;
&lt;li&gt;MongoDB: Database&lt;/li&gt;
&lt;li&gt;Nodejs : Open source&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Packages Installed
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Express: Framework&lt;/li&gt;
&lt;li&gt;Mongoose: To connect to the database&lt;/li&gt;
&lt;li&gt;JSON web token: For authentication&lt;/li&gt;
&lt;li&gt;Nodemon: To restart the server&lt;/li&gt;
&lt;li&gt;Postman: Documentation
### Time to proceed  ###&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Create a new directory and give it any name. For this article i named mine jwt_class. &lt;/li&gt;
&lt;li&gt;Open the newly created directory in VS Code and (inside the terminal) type npm init -y to initialize the project. &lt;/li&gt;
&lt;li&gt;Create the main entry file
my main entry file is called main.js in the project directory. we will be focusing only in this file.&lt;/li&gt;
&lt;li&gt;at the end of the first few steps, you should have ;
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BPeOKtZ6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eyg5ioeerqjjfdkil0m1.PNG" alt="Image description" width="383" height="383"&gt;
&lt;/li&gt;
&lt;li&gt;Import the following  packages: mongoose, express, jwt and nodemon. Then add the packages using the terminal inside VSCode.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RNf1UwRh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qcgjhw3bcdh44djbozxm.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RNf1UwRh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qcgjhw3bcdh44djbozxm.PNG" alt="Image description" width="352" height="206"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create the models, middleware, controllers, routes and their files.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nH-drOz7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rvs507fr1iv38drtf13g.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nH-drOz7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rvs507fr1iv38drtf13g.PNG" alt="Image description" width="272" height="524"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create our server and connect  to our database 
in config/db.js
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TkAFqi4h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/khm1gv000nco6omxxrsk.PNG" alt="Image description" width="671" height="431"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;in main.js&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZlEdcbeT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/14l90u4667yorpegwg7o.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZlEdcbeT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/14l90u4667yorpegwg7o.PNG" alt="Image description" width="691" height="635"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;To start our server, edit the scripts object in our package.json to from "test": "echo \"Error: no test specified\" to look like the one shown below.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C9NRUXfJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z1cdcgwojloum71q3365.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C9NRUXfJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z1cdcgwojloum71q3365.PNG" alt="Image description" width="422" height="184"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create user schema in  models/models.js&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9ReXl_aw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gcvznh240n8llgdbj045.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9ReXl_aw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gcvznh240n8llgdbj045.PNG" alt="Image description" width="560" height="409"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create a sign in and login user  in controllers/controllers.js and routes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;sign in&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iN4lutun--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t31uglzthx70fdzgp5tv.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iN4lutun--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t31uglzthx70fdzgp5tv.PNG" alt="Image description" width="724" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;log in&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZZs_572n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2rq2lhogsot8fxnstk7l.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZZs_572n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2rq2lhogsot8fxnstk7l.PNG" alt="Image description" width="701" height="716"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;in routes&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SpjtNOvD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c7g0i09bhc7kyttcfd2w.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SpjtNOvD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c7g0i09bhc7kyttcfd2w.PNG" alt="Image description" width="507" height="265"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using Postman to test, we’ll get the response shown below after a successful sign in and login.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;sign in&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hVB3wr0t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/opoxk9va7t4saqao5jhh.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hVB3wr0t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/opoxk9va7t4saqao5jhh.PNG" alt="Image description" width="800" height="448"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;log in&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tbSCLz_1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qui67grrnhpig0n8ff94.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tbSCLz_1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qui67grrnhpig0n8ff94.PNG" alt="Image description" width="800" height="511"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We have been able to create and log in a use. We will proceed by creating a a route that requires a user token in the header, which is the JWT token we generated earlier.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create middleware for authentication&lt;br&gt;
middleware/isAuth.js&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MP7psKR5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6vgfr0spbmjaihzw46lx.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MP7psKR5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6vgfr0spbmjaihzw46lx.PNG" alt="Image description" width="748" height="490"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;create the /welcome route and update main.js with the following  to test the middleware.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3aDT8PZr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zcvao42u8btmexe96uo7.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3aDT8PZr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zcvao42u8btmexe96uo7.PNG" alt="Image description" width="400" height="258"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Result in postman;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tmL5QEbk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cov2z0e0orzpzi17hs4z.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tmL5QEbk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cov2z0e0orzpzi17hs4z.PNG" alt="Image description" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Definition of Oauth2
&lt;/h2&gt;

&lt;p&gt;OAuth 2.0, which stands for “Open Authorization”, is a standard designed to allow a website or application to access resources hosted by other web apps on behalf of a user&lt;/p&gt;

&lt;h2&gt;
  
  
  Advantages of Oauth2 over JWT
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;OAuth2 has a  greater variety of usage such as web, browser, API, and various apps or resources While  JWT is mainly used for APIs. OAuth2 helps in more softwares thus giving it an advantage over the limited usage of JWT.&lt;/li&gt;
&lt;li&gt;JWT defines a token format while OAuth deals in defining authorization protocols. OAuth is basically for athorization making possible a particular user to have access to service to any length given by the owner.&lt;/li&gt;
&lt;li&gt;OAuth uses both client-side and server-side storage while JWT must use only client-side storage.&lt;/li&gt;
&lt;li&gt;JWT has limited scope and use cases. OAuth is highly flexible and can be easily used in a wide range of situations.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
  </channel>
</rss>
