<?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: tarekibnkhayer</title>
    <description>The latest articles on DEV Community by tarekibnkhayer (@tarekibnkhayer).</description>
    <link>https://dev.to/tarekibnkhayer</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%2F1118769%2F86dc71f2-84e3-4855-a0eb-b1bbfff1398f.png</url>
      <title>DEV Community: tarekibnkhayer</title>
      <link>https://dev.to/tarekibnkhayer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tarekibnkhayer"/>
    <language>en</language>
    <item>
      <title>Declare vs Assign vs Reassign vs Redeclare in JavaScript</title>
      <dc:creator>tarekibnkhayer</dc:creator>
      <pubDate>Sat, 24 Feb 2024 05:46:52 +0000</pubDate>
      <link>https://dev.to/tarekibnkhayer/declare-vs-assign-vs-reassign-vs-redeclare-in-javascript-4p7p</link>
      <guid>https://dev.to/tarekibnkhayer/declare-vs-assign-vs-reassign-vs-redeclare-in-javascript-4p7p</guid>
      <description>&lt;p&gt;Declare: We can declare a variable using the var, let, and const keywords.&lt;br&gt;
Ex: var x; let y; const z = 10; (Using const keyword variable has to be declared and assigned when it is created);&lt;/p&gt;

&lt;p&gt;Assign: We can assign a value to a variable using the assignment operator(=).&lt;br&gt;
Ex: x = 5; y = 6;&lt;/p&gt;

&lt;p&gt;Reassign: We can reassign a value to a variable except which is declared and assigned with const.&lt;br&gt;
Ex: x = 7; y = 8;&lt;br&gt;
But we declared and assigned const z = 10; before, then we can't reassign it. We can't write the code: z = 4; If we do, JS will throw an error.&lt;/p&gt;

&lt;p&gt;Redeclare: We can redeclare a variable if we use var, otherwise not.&lt;br&gt;
Ex: var a; a = 5; var a = 7; // it won't create any problem.&lt;br&gt;
let b; b = 5; let b = 7; // this is not allowed. Using the let keyword variable can't be redeclared.&lt;br&gt;
const c = 5; const c = 8; // this is not allowed. Using the const keyword variable can't be redeclared.&lt;/p&gt;

&lt;p&gt;One more thing: Using const keyword variable has to be declared and assigned when it is created. You can't assign the value later.&lt;br&gt;
Ex: const d = 5; ( Declaration and Assignment at the same time);&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How JWT works in Authorization</title>
      <dc:creator>tarekibnkhayer</dc:creator>
      <pubDate>Thu, 02 Nov 2023 09:46:21 +0000</pubDate>
      <link>https://dev.to/tarekibnkhayer/how-jwt-works-in-authorization-1ldo</link>
      <guid>https://dev.to/tarekibnkhayer/how-jwt-works-in-authorization-1ldo</guid>
      <description>&lt;h2&gt;
  
  
  What is JWT (JSON Web Token)?
&lt;/h2&gt;

&lt;p&gt;JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties, this is the definition of JWT official website. &lt;/p&gt;

&lt;h2&gt;
  
  
  How JWT works in authorization?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The user logged in to the app and provides their credentials.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The app server verifies the user credentials and generate a JWT token.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const token = jwt.sign(userInfo, 'secret', {expiresIn: '1h'});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;normally, we use axios in login form submit function and when user logged in (with verified info) then it hits '/jwt' URL in backend and we create a token for the user.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
And then we set the token in browser httpOnly cookie in browser.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;res.cookie('token', token {
httpOnly: true,
secure: true, 
sameSite: 'none'
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;On subsequent requests, the browser sends the token in the authorization header of the request.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The server then verifies the JWT token and authorize the users to access the protected resources if the token is valid. The server verifies the token by decoding the token and verifying the signature using the secret key.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Normally the  server verifies the token by a middleware:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const verifyToken = (req, res, next) =&amp;gt; {
const token = req?.cookies?.token;
if(!token) return res.status(401).send({message: 'unauthorized'});
jwt.verify(token, 'secret', (err, decoded)=&amp;gt; {
if(err) return res.status(401).send({message: 'unauthorized'});
req.user = decoded;
next();
}
)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's how you can use JWT to authorize user. And yes, there are so &lt;br&gt;
many ways to do it, but I personally prefer this one.&lt;br&gt;
Thanks for reading. If you have any question related to this post, you can do it in the comment section, I will glad if I can help you with this topic.If you find any mistake please also comment it, I l &lt;br&gt;
will also be glad to correct the mistake.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>security</category>
      <category>node</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
