<?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: Kashish2402</title>
    <description>The latest articles on DEV Community by Kashish2402 (@kashishgupta).</description>
    <link>https://dev.to/kashishgupta</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%2F2068386%2Fb91f59e8-1d07-41e4-95e0-c744a26946c5.png</url>
      <title>DEV Community: Kashish2402</title>
      <link>https://dev.to/kashishgupta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kashishgupta"/>
    <language>en</language>
    <item>
      <title>Authentication System Using NodeJS</title>
      <dc:creator>Kashish2402</dc:creator>
      <pubDate>Thu, 09 Jan 2025 14:42:53 +0000</pubDate>
      <link>https://dev.to/kashishgupta/authentication-system-using-nodejs-23e</link>
      <guid>https://dev.to/kashishgupta/authentication-system-using-nodejs-23e</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Authentication is a process to identify user's identity and grant them access to the resources provided by application. In backend development, &lt;br&gt;
&lt;em&gt;authentication plays a major role to grant or restrict users to access specific resources.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Authentication can be done in two ways: -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Token-Based Authentication.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Session-Based Authentication.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here we'll talk about Token Based Authentication.&lt;/p&gt;
&lt;h2&gt;
  
  
  Token-Based Authentication
&lt;/h2&gt;

&lt;p&gt;Token-based authentication is a widely used security mechanism to offer robust experience to users without compromising with the security. &lt;/p&gt;

&lt;p&gt;In the token-based authentication, when user logged in server generates unique token for users. After server generates the token, it will be sent to client and stored on client's system locally. Whenever client makes a request, this token go with each request to verify the identity of user.&lt;/p&gt;
&lt;h2&gt;
  
  
  Authentication Process
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;REQUEST&lt;/strong&gt;- When user login to the application, browser made a request to server with user credentials.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;VERIFICATION&lt;/strong&gt; - When request comes to the server it validates the user and generates a secret key known as token and send it to user via HTTP.&lt;br&gt;
Generally, the token is sent in a JWT [jsonWebTokens] open standard that consist of a header, payload, signature.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;VALIDATE TOKEN&lt;/strong&gt; - When user receives the token secret code it saves on client's browser as it helps to verify identity whenever user makes a request. this token is short lived have a life span of 15-60min and this token is also known as access token code. If user unable to use access token code, then it will request for refresh token code which stays in system for 3-4 days.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;RESPONSE&lt;/strong&gt; - When the validation is done then token grants or restrict user to access specific content.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
Let's take an example, we'll implement token-based authentication using register and login methodology.&lt;/p&gt;

&lt;p&gt;First, we will create functionality for Register module for a user:-&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEP1: -&lt;/strong&gt; &lt;em&gt;Register User&lt;/em&gt;&lt;br&gt;
Register uses collects data from user to create account&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// FIRSTLY, WE'LL SELECT THE REGISTER FORM
const form = document.querySelector(".register");

// ADDING EVENT LISTENER TO FORM TO COLLECT THE VALUES FROM USER
form.addEventListener("submit", (e) =&amp;gt; {

  e.preventDefault();
  const username = e.target.username.value;
  const email = e.target.email.value;
  const password = e.target.password.value;

});

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;STEP2: -&lt;/strong&gt; &lt;em&gt;Encrypting the password&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Before saving user's data to database, we first need to encrypt the password for security purpose then save it to database.&lt;/p&gt;

&lt;p&gt;For encrypting the password, we use &lt;strong&gt;npm package bcrypt&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Firstly, we need to install the package by writing in terminal: -&lt;br&gt;
&lt;em&gt;&lt;code&gt;npm install bcrypt&lt;/code&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now, we'll hash the password 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;//  IMPORTING BCRYPT MODULE
const bcrypt=require('bcrypt');


// ASSIGNING USERS PASSWORD 
const plainPassword=userPassword

// ENCRYPTING PASSWORD
// BASIC SYNTAX
// bcrypt.hash(plainTextPassword,salt_rounds)

const hashedPassword=bcrypt.hash(plainPassword,10);

if(!hashedPassword){
    throw new Error('Enable to generate password')
}

// NOW WE WILL SAVE USER DETAILS AND PASSWORD TO DATABASE
console.log('Hashed Password is : ',hashedPassword)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;bcrypt.hash is a function which hashes the password with salt rounds. It generates different hashed key every time weather any of two users have same password.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Login functionality&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When any user try to login, it retrieves hashed password from database and compare it with password given by user during login&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const form = document.querySelector(".login");

form.addEventListener("submit", (e) =&amp;gt; {

  e.preventDefault();
  const username = e.target.username.value;
  const password = e.target.password.value;

});

const hashedPassword= 'retrievePasswordFromDatabase'

let result=bcrypt.compare(hashedPassword,userPassword)

if(result){
    console.log('Login Successfully !!')
}

else{
    console.log('Wrong password or username')
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>authjs</category>
      <category>backend</category>
    </item>
    <item>
      <title>useContext</title>
      <dc:creator>Kashish2402</dc:creator>
      <pubDate>Thu, 09 Jan 2025 14:33:50 +0000</pubDate>
      <link>https://dev.to/kashishgupta/usecontext-1l4o</link>
      <guid>https://dev.to/kashishgupta/usecontext-1l4o</guid>
      <description></description>
    </item>
  </channel>
</rss>
