<?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>Forem: Harold Mudosa</title>
    <description>The latest articles on Forem by Harold Mudosa (@haroldmud).</description>
    <link>https://forem.com/haroldmud</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%2F965252%2F074ec8b6-f9f8-4a8e-a226-aa5795672189.jpg</url>
      <title>Forem: Harold Mudosa</title>
      <link>https://forem.com/haroldmud</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/haroldmud"/>
    <language>en</language>
    <item>
      <title>C++ Arrays and Vectors: How I understood the Differences</title>
      <dc:creator>Harold Mudosa</dc:creator>
      <pubDate>Fri, 14 Nov 2025 12:03:27 +0000</pubDate>
      <link>https://forem.com/haroldmud/c-arrays-and-vectors-how-i-understood-the-differences-4jpb</link>
      <guid>https://forem.com/haroldmud/c-arrays-and-vectors-how-i-understood-the-differences-4jpb</guid>
      <description>&lt;p&gt;Yearning to become an Unreal Engine and VR developer, I’ve been diving into C++ fundamentals. Coming from a JavaScript background, I initially thought that working with arrays would be more than enough. That was until I started tackling coding challenges and realized that arrays in C++ aren’t quite the all-stars they are in JavaScript.&lt;/p&gt;

&lt;p&gt;I was soon forced to learn about &lt;strong&gt;vectors&lt;/strong&gt;. Realizing I had a big knowledge gap, I spent over 10 hours trying to understand why things suddenly felt so complicated, especially when I thought I already had it figured out, being a JavaScript professional.&lt;/p&gt;

&lt;p&gt;Through this process, I identified some key differences between arrays and vectors in C++ that really helped me make sense of things.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. How to read their collection length
&lt;/h2&gt;

&lt;h3&gt;
  
  
  - Arrays
&lt;/h3&gt;

&lt;p&gt;You need to know their size beforehand or calculate them manually based on your full array and its first element's size in bytes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int numbers[5] = {1, 2, 3, 4, 5};
int length = sizeof(numbers) / sizeof(numbers[0]); // calculates number of elements based on their size in bytes(sizeof() is for sizes in bytes)
std::cout &amp;lt;&amp;lt; "Array length: " &amp;lt;&amp;lt; length &amp;lt;&amp;lt; std::endl; //Array length: 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  - Vectors
&lt;/h3&gt;

&lt;p&gt;They have a built-in &lt;code&gt;.size()&lt;/code&gt; method, which makes it much easier(in my opinion😁).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;vector&amp;gt;
#include &amp;lt;iostream&amp;gt;
using namespace std;

vector&amp;lt;int&amp;gt; numbers = {1, 2, 3, 4, 5};
cout &amp;lt;&amp;lt; "Vector length: " &amp;lt;&amp;lt; numbers.size() &amp;lt;&amp;lt; endl; //5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Their Memory Allocation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  - Arrays
&lt;/h3&gt;

&lt;p&gt;At compile time, their size must be known, and once an array is created, you cannot change its size.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int numbers[5]; // memory for 5 ints is allocated at compile time

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  - Vectors
&lt;/h3&gt;

&lt;p&gt;They are dynamic, and since for them the memory is managed at runtime, their size doesn't need to be declared beforehand.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;vector&amp;gt;
#include &amp;lt;iostream&amp;gt;
using namespace std;

vector&amp;lt;int&amp;gt; numbers;      // no initial size needed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Their growth in size
&lt;/h2&gt;

&lt;h3&gt;
  
  
  - Arrays
&lt;/h3&gt;

&lt;p&gt;Their size can grow, but only up to the number of elements you initially allocated. You cannot exceed that size (unless you create a new array😌).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int numbers[5] = {1, 2, 3}; // can only grow up to 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  - Vectors
&lt;/h3&gt;

&lt;p&gt;They can grow beyond their initial capacity. When you add more elements than the current capacity(using the &lt;code&gt;.push_back&lt;/code&gt; method), vectors automatically allocate more memory internally to accommodate the new elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;vector&amp;gt;
#include &amp;lt;iostream&amp;gt;
using namespace std;

vector&amp;lt;int&amp;gt; numbers = {1, 2, 3};
numbers.push_back(4);  // grows automatically
numbers.push_back(5);  
cout &amp;lt;&amp;lt; "Vector size: " &amp;lt;&amp;lt; numbers.size() &amp;lt;&amp;lt; endl; // Vector size: 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Their decrease in size
&lt;/h2&gt;

&lt;h3&gt;
  
  
  - Arrays
&lt;/h3&gt;

&lt;p&gt;Their size is fixed at compile time. You cannot remove elements or shrink the array. You can only overwrite values, but the memory and “length” stay the same. Here is how it's done:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Knowing that &lt;code&gt;int numbers[5] = {1,2,3}&lt;/code&gt; is the same as &lt;code&gt;{1,2,3,0,0}&lt;/code&gt; under the hood, the only way to decrease &lt;code&gt;numbers&lt;/code&gt; in size is by updating, for example, the item &lt;code&gt;3&lt;/code&gt; to &lt;code&gt;0&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int arr[3] = {1, 2, 3};
arr[2] = 0; // you can overwrite but cannot remove arr[2] (now we'll have something similar to {1, 2, 0, 0, 0}) ❌
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  - Vectors
&lt;/h3&gt;

&lt;p&gt;They are dynamic, so you can remove elements at runtime using &lt;code&gt;.pop_back()&lt;/code&gt; to remove the last element or &lt;code&gt;.erase()&lt;/code&gt; to remove specific elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;vector&amp;gt;
#include &amp;lt;iostream&amp;gt;
using namespace std;

vector&amp;lt;int&amp;gt; numbers = {1, 2, 3, 4};
numbers.pop_back();      // removes 4
numbers.erase(numbers.begin() + 1); // removes 2
cout &amp;lt;&amp;lt; "Vector size: " &amp;lt;&amp;lt; numbers.size() &amp;lt;&amp;lt; endl; // Vector size: 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4.Their compatibility with the STL algorithm
&lt;/h2&gt;

&lt;h3&gt;
  
  
  - Arrays
&lt;/h3&gt;

&lt;p&gt;You can use the Standard Template Library(STL) Algorithms. But for some cases, you'll need to provide pointers or iterators, which feels kinda manual.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;algorithm&amp;gt;
#include &amp;lt;iostream&amp;gt;
using namespace std;

// to sort an array, you need to first specify the range
int numbers[] = {5, 2, 4, 1, 3};
int n = sizeof(numbers) / sizeof(numbers[0]);
sort(numbers, numbers + n);

for(int x : numbers)
    cout &amp;lt;&amp;lt; x &amp;lt;&amp;lt; " "; // 1 2 3 4 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Vectors
&lt;/h3&gt;

&lt;p&gt;They work directly with STL algorithms without extra pointers, making code cleaner and safer than it is for arrays (according to me🤫).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;algorithm&amp;gt;
#include &amp;lt;iostream&amp;gt;
using namespace stf;

vector&amp;lt;int&amp;gt; numbers = {5, 2, 4, 1, 3};
sort(numbers.begin(), numbers.end());

for(int x: numbers) {
  cout &amp;lt;&amp;lt; x &amp;lt;&amp;lt; " "; // 1 2 3 4 5
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Arrays and vectors each have their strengths. Arrays are simple, fixed-size, and slightly faster for certain operations, while vectors are dynamic, flexible, and work seamlessly with modern C++ features like STL algorithms. &lt;/p&gt;

&lt;p&gt;I hope the differences we covered here: length, dynamic growth, memory allocation, initialization, shrinking, templates, and STL compatibility, will help you understand when and why to use each.&lt;/p&gt;

&lt;p&gt;For a deeper dive, check out &lt;a href="https://www.geeksforgeeks.org/cpp/advantages-of-vector-over-array-in-c/" rel="noopener noreferrer"&gt;GeeksforGeeks&lt;/a&gt;: Advantages of Vector over Array in C++.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>cpp</category>
    </item>
    <item>
      <title>How does Java work under the hood?</title>
      <dc:creator>Harold Mudosa</dc:creator>
      <pubDate>Fri, 17 Jan 2025 12:56:58 +0000</pubDate>
      <link>https://forem.com/haroldmud/how-does-java-work-under-the-hood-1pam</link>
      <guid>https://forem.com/haroldmud/how-does-java-work-under-the-hood-1pam</guid>
      <description>&lt;p&gt;&lt;strong&gt;First off, what is java ?&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Java is a multiplatform object-oriented programming language.&lt;/p&gt;

&lt;p&gt;What I mean by the word "multiplatform" is that not only can it be used to create web, mobile applications and games, It can also run on any device you can think of.&lt;/p&gt;

&lt;p&gt;What I meant by Object-oriented programming(OOP) is that its main concept turns around objects, which are simply data that have properties and behaviors.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Now, How is it processed in our Computers ?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffipd0nx2qtv3tjfpsk44.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffipd0nx2qtv3tjfpsk44.png" alt="Schema about how java works" width="800" height="526"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Java processing can be compared to a journey, and the best way to describe this journey is by breaking it down step by step:&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Step 1. The code script&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9nth1avlz55n5t83d3w1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9nth1avlz55n5t83d3w1.png" alt="java code" width="800" height="356"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we all know the code needs to be typed in a ".java" file from an editor(Notepad, IDE, etc.).&lt;/p&gt;

&lt;p&gt;But this code is only understandable by humans so we'll need a way to convert our Java code into something that can be read and understood by our computer.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Step 2. Compilation into Bytecode&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9mevk8qjvkbwu0e8ns2r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9mevk8qjvkbwu0e8ns2r.png" alt="compilation" width="800" height="233"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Javac&lt;/strong&gt; or Java compiler, just as its name shows it, is the standard Java compiler.&lt;/p&gt;

&lt;p&gt;So what it does is, take the Java code and compile it into something called &lt;strong&gt;Bytecode&lt;/strong&gt; under the influence of some command.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;Bytecode&lt;/strong&gt; is a set of instructions that can run on any device as long as it contains a &lt;strong&gt;Java Virtual Machine(JVM)&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Step 3. JVM's role play&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl2gejl172crrvdqzmnsr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl2gejl172crrvdqzmnsr.png" alt="JVM role" width="512" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;JVM&lt;/strong&gt;(Java Virtual Machine), is a powerful translator, it takes the  Bytecode and makes it understandable by the computer's processor backed up by the &lt;strong&gt;JRE&lt;/strong&gt;(Java Runtime Environment) that provides different libraries or interfaces that will help the JVM operate seamlessly.&lt;/p&gt;

&lt;p&gt;One more thing though, the JVM doesn't just run our Bytecode directly, this is what it goes through:&lt;br&gt;
&lt;strong&gt;- interpretation:&lt;/strong&gt; Our JVM converts each line of code into an instruction or action the computer should execute, also called the Bytecode.&lt;br&gt;
&lt;strong&gt;- JIT compilation:&lt;/strong&gt; For things to run faster, the JVM takes those bytecodes and converts them into &lt;strong&gt;native machine code&lt;/strong&gt;(the language our computer's processor can understand) by the &lt;strong&gt;JIT&lt;/strong&gt;(Just-In-Time Compiler).&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Step 4. Running the Program&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5le9dqthu1u74fkp2mu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5le9dqthu1u74fkp2mu.png" alt="game" width="800" height="310"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that our JVM has done its work, it finally sends the instruction to the computer to run or print whatever the developer was intending to print(let's say may be a "hello world!!" on the screen, or that cool game above😊).&lt;/p&gt;

</description>
      <category>java</category>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>NextAuth.js authentication with Github and Google in NextJs</title>
      <dc:creator>Harold Mudosa</dc:creator>
      <pubDate>Fri, 29 Nov 2024 09:33:11 +0000</pubDate>
      <link>https://forem.com/haroldmud/nextauthjs-authentication-with-github-and-google-in-nextjs-253d</link>
      <guid>https://forem.com/haroldmud/nextauthjs-authentication-with-github-and-google-in-nextjs-253d</guid>
      <description>&lt;p&gt;NextAuth aims to make authentication simple, yet secure in Next.js apps. it handles automatically practices like passwordless flow, scoping access and more.&lt;/p&gt;

&lt;p&gt;With it you can simply get authenticated through your existing logins from different platforms like Google, Atlassian, github, etc. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F944up04ukmergm3z5u9r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F944up04ukmergm3z5u9r.png" alt="schema" width="573" height="369"&gt;&lt;/a&gt; &lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step by step guide &lt;br&gt;&lt;br&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1. Setting up NextAuth for sign in
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Run &lt;code&gt;npm install next-auth@4.24.7&lt;/code&gt; to install next auth dependecies&lt;/li&gt;
&lt;li&gt;In in your &lt;u&gt;&lt;em&gt;src/app&lt;/em&gt;&lt;/u&gt; directory create a &lt;u&gt;&lt;em&gt;lib&lt;/em&gt;&lt;/u&gt; directory in which you'll add the &lt;u&gt;&lt;em&gt;authOptions.ts&lt;/em&gt;&lt;/u&gt; file with the following changes:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//src/app/lib/authOptions.ts

import { NextAuthOptions } from "next-auth";
import GithubProvider from "next-auth/providers/github";
import GoogleProvider from "next-auth/providers/google";

export const authOptions: NextAuthOptions = {
  providers: [
    GithubProvider({
      clientId: process.env.GITHUB_ID,
      clientSecret: process.env.GITHUB_SECRET
    }),
    GoogleProvider({
      clientId: process.env.GOOGLE_ID,
      clientSecret: process.env.GOOGLE_SECRET
    }),  
  ],
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To get the values of the above &lt;strong&gt;&lt;em&gt;clientId&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;clientSecret&lt;/em&gt;&lt;/strong&gt;, we'll go through the following process in both &lt;strong&gt;github&lt;/strong&gt; and &lt;strong&gt;googleCloud&lt;/strong&gt; respectively: &lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;For Github, let's go to our Github&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;u&gt;Navigate then through&lt;/u&gt;&lt;/strong&gt;: &lt;em&gt;Your profile icon(at the top right of the interface)&lt;/em&gt; &amp;gt; &lt;em&gt;settings&lt;/em&gt; &amp;gt; &lt;em&gt;developer settings&lt;/em&gt; &amp;gt; &lt;em&gt;OAuth&lt;/em&gt; &amp;gt; &lt;em&gt;New Project&lt;/em&gt;. And Then fill in the form this way:&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuc6i84zltlyvazcd308p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuc6i84zltlyvazcd308p.png" alt="register a new OAuth application" width="800" height="389"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; Next, recover the &lt;strong&gt;clientId&lt;/strong&gt; highlighted below and then generate &lt;strong&gt;client secret&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffiez5d6xkkwnqs7rjn5o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffiez5d6xkkwnqs7rjn5o.png" alt="Client ID and Secret recovery" width="800" height="393"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;For Google, let's go to our Google Cloud&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;u&gt;Navigate then through&lt;/u&gt;&lt;/strong&gt;: &lt;em&gt;my Project&lt;/em&gt;(right near the logo) &amp;gt; &lt;em&gt;Menu&lt;/em&gt; &amp;gt; &lt;em&gt;API&lt;/em&gt;  &amp;gt; &lt;em&gt;Identifiers&lt;/em&gt; &amp;gt;  &lt;em&gt;Create identifier&lt;/em&gt; &amp;gt; ID client OAuth. And then do the following:&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;First select the web app as the application type&lt;br&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpiodpdfekgrp5yx6qugx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpiodpdfekgrp5yx6qugx.png" alt="Create an ID client Auth" width="800" height="467"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Then, Fill in the form like this:&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk7kpctn11w5ntr7nayjb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk7kpctn11w5ntr7nayjb.png" alt="Identifier creation" width="800" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Same process, by clicking on Create you'll be able to recover those keys as well.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgkv056k69ex5s4noo080.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgkv056k69ex5s4noo080.png" alt="client ID and Secret recovery" width="800" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;replace "&lt;em&gt;weiterbildung&lt;/em&gt;" by the name of your project&lt;/li&gt;
&lt;li&gt;Go to your existing &lt;em&gt;&lt;u&gt; .env&lt;/u&gt;&lt;/em&gt; file in which you'll save those keys and more sensitive informations, create it if it doesn't exist in your root directory. 
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 2. Create the Authentication API route
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;u&gt;&lt;em&gt;App&lt;/em&gt;&lt;/u&gt; Create the directory &lt;u&gt;&lt;em&gt;api/[...nextauth]/route.ts&lt;/em&gt;&lt;/u&gt;  and implement the authentication API route the following way:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// route.ts

import { authOptions } from "@/lib/authOptions";
import NextAuth from "next-auth";
import { AuthOptions } from "next-auth";

const handler = NextAuth(authOptions as AuthOptions);

export { handler as GET, handler as POST };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create a provider that will wrap your system, to make all its props available like a context, in your &lt;em&gt;&lt;u&gt;lib&lt;/u&gt;&lt;/em&gt; directory, create &lt;u&gt;&lt;em&gt;sessionWrapper.tsx&lt;/em&gt;&lt;/u&gt; where you'll do:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//sessionWrapper.tsx

"use client"

import { SessionProvider } from "next-auth/react"

const SessionWrapper = ({children}: {children: React.ReactNode}) =&amp;gt; {
  return &amp;lt;SessionProvider&amp;gt;{children}&amp;lt;/SessionProvider&amp;gt;
}

export default SessionWrapper;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;implement the &lt;strong&gt;&lt;em&gt;SessionWrapper&lt;/em&gt;&lt;/strong&gt; component in layout.tsx after importing it and wrap the layout elements within it, its return should look like this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//layout.tsx
import SessionWrapper from "../lib/sessionWrapper";

//...some code
return (
    &amp;lt;SessionWrapper&amp;gt; //here you are
      &amp;lt;html lang="en"&amp;gt;
        &amp;lt;body
          className={`${geistSans.variable} ${geistMono.variable} antialiased`}
        &amp;gt;
          &amp;lt;ClientLayout session={session}&amp;gt;{children}&amp;lt;/ClientLayout&amp;gt;
        &amp;lt;/body&amp;gt;
      &amp;lt;/html&amp;gt;
    &amp;lt;/SessionWrapper&amp;gt; //here you are
  );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3. Set up the connexion between your system and the google and github providers
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;In your log-in page or component do the following modification:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//logIn.tsx


"use client"

import { signIn, useSession } from 'next-auth/react';

export default function Login() {
  const { data: session } = useSession();

  if(session) {
    console.log('session', session);
  }


  return (
    &amp;lt;div&amp;gt;
          &amp;lt;button
            type="button"
            onClick={() =&amp;gt; signIn('github')}
            className="w-full py-2 my-2 border border-red-500"
          &amp;gt;Login with Github
          &amp;lt;/button&amp;gt;

          &amp;lt;button
            type="button"
            onClick={() =&amp;gt; {signIn('google')}}
            className="w-full py-2 my-2 border border-red-500"
          &amp;gt;Login with Google
          &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In your log-out component do the following modification:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//signOut.tsx


import { signOut } from 'next-auth/react';

export default function Login() {  
  return (
    &amp;lt;div&amp;gt;
          &amp;lt;button
            type="button"
            onClick={() =&amp;gt; signOut()}
            className="w-full py-2 my-2 border border-red-500"
          &amp;gt;Log out
          &amp;lt;/button&amp;gt;

    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;RESULT WITH GOOGLE:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3rzhohbt1fejg44152fb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3rzhohbt1fejg44152fb.png" alt="Google result" width="800" height="298"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;NOTE:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In the console, you'll be able to see the user credential once the signIn function is triggered by any of the two buttons.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flhpubeqky1h7baxfdl4y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flhpubeqky1h7baxfdl4y.png" alt="user session result" width="800" height="196"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can choose to get the JWT(Json Web Token) instead of a row object if you want to get your backend involved.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Easiest way of Implementing Authentication in NestJS</title>
      <dc:creator>Harold Mudosa</dc:creator>
      <pubDate>Fri, 22 Nov 2024 11:03:00 +0000</pubDate>
      <link>https://forem.com/haroldmud/easiest-way-of-implementing-authentication-in-nestjs-594g</link>
      <guid>https://forem.com/haroldmud/easiest-way-of-implementing-authentication-in-nestjs-594g</guid>
      <description>&lt;p&gt;In this article you'll be walked through the most straightforward guide of implementing authentication in NestJS, the most efficient scalable server-side applications builder among NodeJS frameworks.&lt;/p&gt;

&lt;p&gt;We'll cover best practices, fundamental concepts that I believe you already have a slight prerequisite in by now and clearer examples. The purpose is to help you build the most robust authentication systems for your NestJS applications from simple concepts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54mmtcikcjgeleoymw1j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54mmtcikcjgeleoymw1j.png" alt="Authentication Implementation with NestJS" width="686" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In simple words, Authentication is a process used to show that something or someone is legit, genuine or valid. hence authentication is a way of verifying if the user is really who they pretend to be before giving them access to a system or to some resources of this latter.&lt;/p&gt;

&lt;p&gt;This goes hand in hand with Authorization, which is a process by which the server determines if a user has permission to have access on a specific resource of a system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Developer Viewpoint Scenario
&lt;/h2&gt;

&lt;p&gt;Before talking about Authentication, the client should register himself as a user of our system, and for that he will need to sign up, a process where he will post the credentials that will allow the system to recognize him later on.&lt;/p&gt;

&lt;p&gt;Now talking of Authentication, the user will initiate the process by passing in his username/email and password, which will trigger the server to generate a JWT(Json Web Token) upon a successful authentication, Token that will then be transmitted as a bearer token in the authentication header for verification.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step by Step guide:&lt;br&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step_1. Create a resource for the users credentials
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;I assume that you already know how to create resources in NestJS, if not click &lt;a href="https://www.freecodecamp.org/news/build-a-crud-rest-api-with-nestjs-docker-swagger-prisma/" rel="noopener noreferrer"&gt;here&lt;/a&gt; to learn how to do so.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Well, first thing first create a resource called maybe "user", this will manage the users credential data, remember the user credentials have to be stored somewhere, right ?&lt;br&gt;&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step_2. Implement authentication in your REST API.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Run the following command and when prompted don’t generate the CRUD, this auth is a special type of resource.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm nest generate resource auth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Install and configure passport(a library that manage authentication states).
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npm install --save @nestjs/passport passport @nestjs/jwt passport-jwt&lt;br&gt;
npm install --save-dev @types/passport-jwt&lt;br&gt;
npm install passport-jwt&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;em&gt;auth.modute.ts&lt;/em&gt;, add the following highlighted code-lines[by red dots 🔴].&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4nmfmhwbyxfr8efvk31z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4nmfmhwbyxfr8efvk31z.png" alt="auth.module.ts" width="800" height="509"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implement a POST endpoint at &lt;strong&gt;&lt;em&gt;/auth/login&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Create a new file called &lt;u&gt;&lt;em&gt;login.dto.ts&lt;/em&gt;&lt;/u&gt; inside the &lt;em&gt;&lt;u&gt;src/auth/dto&lt;/u&gt;&lt;/em&gt; folder and then define the &lt;strong&gt;&lt;em&gt;LoginDto&lt;/em&gt;&lt;/strong&gt; class interface based on the email and password properties for the login.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31nzhzch2of5qrjzc8su.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31nzhzch2of5qrjzc8su.png" alt="auth/dto/login.dto.ts" width="800" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new file called &lt;em&gt;&lt;u&gt;auth.entity.ts&lt;/u&gt;&lt;/em&gt; inside the src/auth/entity that will describe the the shape of the &lt;strong&gt;jwt&lt;/strong&gt; payload.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftyab8v9id2n4cgjahthx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftyab8v9id2n4cgjahthx.png" alt="auth/entity/auth.entity.ts" width="800" height="335"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new login method inside the &lt;u&gt;&lt;em&gt;auth.service.ts&lt;/em&gt;&lt;/u&gt; file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkp6znuehf50ctimjkwam.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkp6znuehf50ctimjkwam.png" alt="auth/auth.service.ts" width="800" height="519"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now, In &lt;u&gt;&lt;em&gt;auth.controller.ts&lt;/em&gt;&lt;/u&gt; create a POST endpoint at &lt;strong&gt;&lt;em&gt;/auth/login&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Figkfe6dczuz1p5yp87hv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Figkfe6dczuz1p5yp87hv.png" alt="auth/auth.controller.ts" width="800" height="538"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step_3. Implement JWT authentication strategy
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;First create a new file called &lt;u&gt;&lt;em&gt;jwt.strategy.ts&lt;/em&gt;&lt;/u&gt; inside the &lt;u&gt;&lt;em&gt;src/auth/strategy&lt;/em&gt;&lt;/u&gt; directory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6mfeuvrqv4551mc6tihy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6mfeuvrqv4551mc6tihy.png" alt="auth/strategy/jwt.strategy.ts" width="800" height="530"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Into &lt;u&gt;&lt;em&gt;Auth.module.ts&lt;/em&gt;&lt;/u&gt; import then add &lt;strong&gt;&lt;em&gt;JwtStrategy&lt;/em&gt;&lt;/strong&gt; in the imports and &lt;em&gt;&lt;strong&gt;usersModule&lt;/strong&gt;&lt;/em&gt; in the providers[see red dots 🔴].&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr3368bfdxqlr3jl9yqm1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr3368bfdxqlr3jl9yqm1.png" alt="auth/auth.module.ts" width="800" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To make &lt;em&gt;&lt;strong&gt;UsersService&lt;/strong&gt;&lt;/em&gt; accessible in our class(surely &lt;strong&gt;&lt;em&gt;AuthService&lt;/em&gt;&lt;/strong&gt;), you also need to add it in the exports of the &lt;em&gt;&lt;strong&gt;UsersModule&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs2j6yljjii3irt23ok62.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs2j6yljjii3irt23ok62.png" alt="users/users.module.ts" width="800" height="402"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step_4. Protecting our system resources from unverified users
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;JWT auth guard&lt;/strong&gt; will be used to protect routes that require authentication in order to be accessed, in our case let's protect the &lt;strong&gt;users&lt;/strong&gt; resource API routes. so, in &lt;u&gt;&lt;em&gt;src/auth&lt;/em&gt;&lt;/u&gt;, create a file called &lt;u&gt;&lt;em&gt;jwt-auth.guard.ts&lt;/em&gt;&lt;/u&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fand342x5lo44xlitfkid.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fand342x5lo44xlitfkid.png" alt="auth/jwt-auth.guard.ts" width="800" height="208"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;NOTE:&lt;/strong&gt; The &lt;strong&gt;&lt;em&gt;AuthGuard&lt;/em&gt;&lt;/strong&gt; class expects the name of an authentication strategy. In this case, you are using the &lt;strong&gt;JwtStrategy&lt;/strong&gt; that you implemented in the previous section, which is named “&lt;strong&gt;&lt;em&gt;jwt&lt;/em&gt;&lt;/strong&gt;”.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can now use this guard (&lt;strong&gt;&lt;em&gt;UseGuards(JwtAuthGuard)&lt;/em&gt;&lt;/strong&gt;) as a decorator to protect your endpoints. Add the &lt;em&gt;&lt;strong&gt;JwtAuthGuard&lt;/strong&gt;&lt;/em&gt; to routes in the &lt;em&gt;&lt;strong&gt;UsersController&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv4igjxgig7hfwyhjko29.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv4igjxgig7hfwyhjko29.png" alt="users/user.controller.ts" width="800" height="862"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; If you're using &lt;strong&gt;Swagger&lt;/strong&gt;, integrate an authentication indication that will highlight the routes that are protected using the &lt;strong&gt;&lt;em&gt;@ApiBearerAuth()&lt;/em&gt;&lt;/strong&gt; decorator. first define it in main, then implement it in &lt;u&gt;&lt;em&gt;users.controller.ts&lt;/em&gt;&lt;/u&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmora6rzb8syfztz86v3b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmora6rzb8syfztz86v3b.png" alt="src/main.ts" width="800" height="296"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh3zll2aj8kat2c4fc0jw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh3zll2aj8kat2c4fc0jw.png" alt="users/users.controller.ts" width="800" height="541"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AND NOW YOU'RE ALL SET&lt;/strong&gt;😮‍💨&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Easiest way of Implementing Authentication in NestJS</title>
      <dc:creator>Harold Mudosa</dc:creator>
      <pubDate>Fri, 22 Nov 2024 11:03:00 +0000</pubDate>
      <link>https://forem.com/haroldmud/easiest-way-of-implementing-authentication-in-nestjs-46d4</link>
      <guid>https://forem.com/haroldmud/easiest-way-of-implementing-authentication-in-nestjs-46d4</guid>
      <description>&lt;p&gt;In this article you'll be walked through the most straightforward guide of implementing authentication in NestJS, the most efficient scalable server-side applications builder among NodeJS frameworks.&lt;/p&gt;

&lt;p&gt;We'll cover best practices, fundamental concepts that I believe you already have a slight prerequisite in by now and clearer examples. The purpose is to help you build the most robust authentication systems for your NestJS applications from simple concepts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54mmtcikcjgeleoymw1j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54mmtcikcjgeleoymw1j.png" alt="Authentication Implementation with NestJS" width="686" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In simple words, Authentication is a process used to show that something or someone is legit, genuine or valid. hence authentication is a way of verifying if the user is really who they pretend to be before giving them access to a system or to some resources of this latter.&lt;/p&gt;

&lt;p&gt;This goes hand in hand with Authorization, which is a process by which the server determines if a user has permission to have access on a specific resource of a system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Developer Viewpoint Scenario
&lt;/h2&gt;

&lt;p&gt;Before talking about Authentication, the client should register himself as a user of our system, and for that he will need to sign up, a process where he will post the credentials that will allow the system to recognize him later on.&lt;/p&gt;

&lt;p&gt;Now talking of Authentication, the user will initiate the process by passing in his username/email and password, which will trigger the server to generate a JWT(Json Web Token) upon a successful authentication, Token that will then be transmitted as a bearer token in the authentication header for verification.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step by Step guide:&lt;br&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step_1. Create a resource for the users credentials
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;I assume that you already know how to create resources in NestJS, if not click &lt;a href="https://www.freecodecamp.org/news/build-a-crud-rest-api-with-nestjs-docker-swagger-prisma/" rel="noopener noreferrer"&gt;here&lt;/a&gt; to learn how to do so.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Well, first thing first create a resource called maybe "user", this will manage the users credential data, remember the user credentials have to be stored somewhere, right ?&lt;br&gt;&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step_2. Implement authentication in your REST API.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Run the following command and when prompted don’t generate the CRUD, this auth is a special type of resource.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm nest generate resource auth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Install and configure passport(a library that manage authentication states).
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npm install --save @nestjs/passport passport @nestjs/jwt passport-jwt&lt;br&gt;
npm install --save-dev @types/passport-jwt&lt;br&gt;
npm install passport-jwt&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;em&gt;auth.modute.ts&lt;/em&gt;, add the following highlighted code-lines[by red dots 🔴].&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4nmfmhwbyxfr8efvk31z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4nmfmhwbyxfr8efvk31z.png" alt="auth.module.ts" width="800" height="509"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implement a POST endpoint at &lt;strong&gt;&lt;em&gt;/auth/login&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Create a new file called &lt;u&gt;&lt;em&gt;login.dto.ts&lt;/em&gt;&lt;/u&gt; inside the &lt;em&gt;&lt;u&gt;src/auth/dto&lt;/u&gt;&lt;/em&gt; folder and then define the &lt;strong&gt;&lt;em&gt;LoginDto&lt;/em&gt;&lt;/strong&gt; class interface based on the email and password properties for the login.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31nzhzch2of5qrjzc8su.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31nzhzch2of5qrjzc8su.png" alt="auth/dto/login.dto.ts" width="800" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new file called &lt;em&gt;&lt;u&gt;auth.entity.ts&lt;/u&gt;&lt;/em&gt; inside the src/auth/entity that will describe the the shape of the &lt;strong&gt;jwt&lt;/strong&gt; payload.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftyab8v9id2n4cgjahthx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftyab8v9id2n4cgjahthx.png" alt="auth/entity/auth.entity.ts" width="800" height="335"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new login method inside the &lt;u&gt;&lt;em&gt;auth.service.ts&lt;/em&gt;&lt;/u&gt; file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkp6znuehf50ctimjkwam.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkp6znuehf50ctimjkwam.png" alt="auth/auth.service.ts" width="800" height="519"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now, In &lt;u&gt;&lt;em&gt;auth.controller.ts&lt;/em&gt;&lt;/u&gt; create a POST endpoint at &lt;strong&gt;&lt;em&gt;/auth/login&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Figkfe6dczuz1p5yp87hv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Figkfe6dczuz1p5yp87hv.png" alt="auth/auth.controller.ts" width="800" height="538"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step_3. Implement JWT authentication strategy
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;First create a new file called &lt;u&gt;&lt;em&gt;jwt.strategy.ts&lt;/em&gt;&lt;/u&gt; inside the &lt;u&gt;&lt;em&gt;src/auth/strategy&lt;/em&gt;&lt;/u&gt; directory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6mfeuvrqv4551mc6tihy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6mfeuvrqv4551mc6tihy.png" alt="auth/strategy/jwt.strategy.ts" width="800" height="530"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Into &lt;u&gt;&lt;em&gt;Auth.module.ts&lt;/em&gt;&lt;/u&gt; import then add &lt;strong&gt;&lt;em&gt;JwtStrategy&lt;/em&gt;&lt;/strong&gt; in the imports and &lt;em&gt;&lt;strong&gt;usersModule&lt;/strong&gt;&lt;/em&gt; in the providers[see red dots 🔴].&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr3368bfdxqlr3jl9yqm1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr3368bfdxqlr3jl9yqm1.png" alt="auth/auth.module.ts" width="800" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To make &lt;em&gt;&lt;strong&gt;UsersService&lt;/strong&gt;&lt;/em&gt; accessible in our class(surely &lt;strong&gt;&lt;em&gt;AuthService&lt;/em&gt;&lt;/strong&gt;), you also need to add it in the exports of the &lt;em&gt;&lt;strong&gt;UsersModule&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs2j6yljjii3irt23ok62.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs2j6yljjii3irt23ok62.png" alt="users/users.module.ts" width="800" height="402"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step_4. Protecting our system resources from unverified users
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;JWT auth guard&lt;/strong&gt; will be used to protect routes that require authentication in order to be accessed, in our case let's protect the &lt;strong&gt;users&lt;/strong&gt; resource API routes. so, in &lt;u&gt;&lt;em&gt;src/auth&lt;/em&gt;&lt;/u&gt;, create a file called &lt;u&gt;&lt;em&gt;jwt-auth.guard.ts&lt;/em&gt;&lt;/u&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fand342x5lo44xlitfkid.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fand342x5lo44xlitfkid.png" alt="auth/jwt-auth.guard.ts" width="800" height="208"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;NOTE:&lt;/strong&gt; The &lt;strong&gt;&lt;em&gt;AuthGuard&lt;/em&gt;&lt;/strong&gt; class expects the name of an authentication strategy. In this case, you are using the &lt;strong&gt;JwtStrategy&lt;/strong&gt; that you implemented in the previous section, which is named “&lt;strong&gt;&lt;em&gt;jwt&lt;/em&gt;&lt;/strong&gt;”.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can now use this guard (&lt;strong&gt;&lt;em&gt;UseGuards(JwtAuthGuard)&lt;/em&gt;&lt;/strong&gt;) as a decorator to protect your endpoints. Add the &lt;em&gt;&lt;strong&gt;JwtAuthGuard&lt;/strong&gt;&lt;/em&gt; to routes in the &lt;em&gt;&lt;strong&gt;UsersController&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv4igjxgig7hfwyhjko29.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv4igjxgig7hfwyhjko29.png" alt="users/user.controller.ts" width="800" height="862"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; If you're using &lt;strong&gt;Swagger&lt;/strong&gt;, integrate an authentication indication that will highlight the routes that are protected using the &lt;strong&gt;&lt;em&gt;@ApiBearerAuth()&lt;/em&gt;&lt;/strong&gt; decorator. first define it in main, then implement it in &lt;u&gt;&lt;em&gt;users.controller.ts&lt;/em&gt;&lt;/u&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmora6rzb8syfztz86v3b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmora6rzb8syfztz86v3b.png" alt="src/main.ts" width="800" height="296"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh3zll2aj8kat2c4fc0jw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh3zll2aj8kat2c4fc0jw.png" alt="users/users.controller.ts" width="800" height="541"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AND NOW YOU'RE ALL SET&lt;/strong&gt;😮‍💨&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Easiest way of Implementing Authentication in NestJS</title>
      <dc:creator>Harold Mudosa</dc:creator>
      <pubDate>Fri, 22 Nov 2024 11:03:00 +0000</pubDate>
      <link>https://forem.com/haroldmud/easiest-way-of-implementing-authentication-in-nestjs-4pba</link>
      <guid>https://forem.com/haroldmud/easiest-way-of-implementing-authentication-in-nestjs-4pba</guid>
      <description>&lt;p&gt;In this article you'll be walked through the most straightforward guide of implementing authentication in NestJS, the most efficient scalable server-side applications builder among NodeJS frameworks.&lt;/p&gt;

&lt;p&gt;We'll cover best practices, fundamental concepts that I believe you already have a slight prerequisite in by now and clearer examples. The purpose is to help you build the most robust authentication systems for your NestJS applications from simple concepts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54mmtcikcjgeleoymw1j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54mmtcikcjgeleoymw1j.png" alt="Authentication Implementation with NestJS" width="686" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In simple words, Authentication is a process used to show that something or someone is legit, genuine or valid. hence authentication is a way of verifying if the user is really who they pretend to be before giving them access to a system or to some resources of this latter.&lt;/p&gt;

&lt;p&gt;This goes hand in hand with Authorization, which is a process by which the server determines if a user has permission to have access on a specific resource of a system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Developer Viewpoint Scenario
&lt;/h2&gt;

&lt;p&gt;Before talking about Authentication, the client should register himself as a user of our system, and for that he will need to sign up, a process where he will post the credentials that will allow the system to recognize him later on.&lt;/p&gt;

&lt;p&gt;Now talking of Authentication, the user will initiate the process by passing in his username/email and password, which will trigger the server to generate a JWT(Json Web Token) upon a successful authentication, Token that will then be transmitted as a bearer token in the authentication header for verification.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step by Step guide:&lt;br&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step_1. Create a resource for the users credentials
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;I assume that you already know how to create resources in NestJS, if not click &lt;a href="https://www.freecodecamp.org/news/build-a-crud-rest-api-with-nestjs-docker-swagger-prisma/" rel="noopener noreferrer"&gt;here&lt;/a&gt; to learn how to do so.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Well, first thing first create a resource called maybe "user", this will manage the users credential data, remember the user credentials have to be stored somewhere, right ?&lt;br&gt;&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step_2. Implement authentication in your REST API.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Run the following command and when prompted don’t generate the CRUD, this auth is a special type of resource.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm nest generate resource auth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Install and configure passport(a library that manage authentication states).
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npm install --save @nestjs/passport passport @nestjs/jwt passport-jwt&lt;br&gt;
npm install --save-dev @types/passport-jwt&lt;br&gt;
npm install passport-jwt&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;em&gt;auth.modute.ts&lt;/em&gt;, add the following highlighted code-lines[by red dots 🔴].&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4nmfmhwbyxfr8efvk31z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4nmfmhwbyxfr8efvk31z.png" alt="auth.module.ts" width="800" height="509"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implement a POST endpoint at &lt;strong&gt;&lt;em&gt;/auth/login&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Create a new file called &lt;u&gt;&lt;em&gt;login.dto.ts&lt;/em&gt;&lt;/u&gt; inside the &lt;em&gt;&lt;u&gt;src/auth/dto&lt;/u&gt;&lt;/em&gt; folder and then define the &lt;strong&gt;&lt;em&gt;LoginDto&lt;/em&gt;&lt;/strong&gt; class interface based on the email and password properties for the login.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31nzhzch2of5qrjzc8su.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31nzhzch2of5qrjzc8su.png" alt="auth/dto/login.dto.ts" width="800" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new file called &lt;em&gt;&lt;u&gt;auth.entity.ts&lt;/u&gt;&lt;/em&gt; inside the src/auth/entity that will describe the the shape of the &lt;strong&gt;jwt&lt;/strong&gt; payload.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftyab8v9id2n4cgjahthx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftyab8v9id2n4cgjahthx.png" alt="auth/entity/auth.entity.ts" width="800" height="335"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new login method inside the &lt;u&gt;&lt;em&gt;auth.service.ts&lt;/em&gt;&lt;/u&gt; file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkp6znuehf50ctimjkwam.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkp6znuehf50ctimjkwam.png" alt="auth/auth.service.ts" width="800" height="519"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now, In &lt;u&gt;&lt;em&gt;auth.controller.ts&lt;/em&gt;&lt;/u&gt; create a POST endpoint at &lt;strong&gt;&lt;em&gt;/auth/login&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Figkfe6dczuz1p5yp87hv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Figkfe6dczuz1p5yp87hv.png" alt="auth/auth.controller.ts" width="800" height="538"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step_3. Implement JWT authentication strategy
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;First create a new file called &lt;u&gt;&lt;em&gt;jwt.strategy.ts&lt;/em&gt;&lt;/u&gt; inside the &lt;u&gt;&lt;em&gt;src/auth/strategy&lt;/em&gt;&lt;/u&gt; directory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6mfeuvrqv4551mc6tihy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6mfeuvrqv4551mc6tihy.png" alt="auth/strategy/jwt.strategy.ts" width="800" height="530"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Into &lt;u&gt;&lt;em&gt;Auth.module.ts&lt;/em&gt;&lt;/u&gt; import then add &lt;strong&gt;&lt;em&gt;JwtStrategy&lt;/em&gt;&lt;/strong&gt; in the imports and &lt;em&gt;&lt;strong&gt;usersModule&lt;/strong&gt;&lt;/em&gt; in the providers[see red dots 🔴].&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr3368bfdxqlr3jl9yqm1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr3368bfdxqlr3jl9yqm1.png" alt="auth/auth.module.ts" width="800" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To make &lt;em&gt;&lt;strong&gt;UsersService&lt;/strong&gt;&lt;/em&gt; accessible in our class(surely &lt;strong&gt;&lt;em&gt;AuthService&lt;/em&gt;&lt;/strong&gt;), you also need to add it in the exports of the &lt;em&gt;&lt;strong&gt;UsersModule&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs2j6yljjii3irt23ok62.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs2j6yljjii3irt23ok62.png" alt="users/users.module.ts" width="800" height="402"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step_4. Protecting our system resources from unverified users
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;JWT auth guard&lt;/strong&gt; will be used to protect routes that require authentication in order to be accessed, in our case let's protect the &lt;strong&gt;users&lt;/strong&gt; resource API routes. so, in &lt;u&gt;&lt;em&gt;src/auth&lt;/em&gt;&lt;/u&gt;, create a file called &lt;u&gt;&lt;em&gt;jwt-auth.guard.ts&lt;/em&gt;&lt;/u&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fand342x5lo44xlitfkid.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fand342x5lo44xlitfkid.png" alt="auth/jwt-auth.guard.ts" width="800" height="208"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;NOTE:&lt;/strong&gt; The &lt;strong&gt;&lt;em&gt;AuthGuard&lt;/em&gt;&lt;/strong&gt; class expects the name of an authentication strategy. In this case, you are using the &lt;strong&gt;JwtStrategy&lt;/strong&gt; that you implemented in the previous section, which is named “&lt;strong&gt;&lt;em&gt;jwt&lt;/em&gt;&lt;/strong&gt;”.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can now use this guard (&lt;strong&gt;&lt;em&gt;UseGuards(JwtAuthGuard)&lt;/em&gt;&lt;/strong&gt;) as a decorator to protect your endpoints. Add the &lt;em&gt;&lt;strong&gt;JwtAuthGuard&lt;/strong&gt;&lt;/em&gt; to routes in the &lt;em&gt;&lt;strong&gt;UsersController&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv4igjxgig7hfwyhjko29.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv4igjxgig7hfwyhjko29.png" alt="users/user.controller.ts" width="800" height="862"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; If you're using &lt;strong&gt;Swagger&lt;/strong&gt;, integrate an authentication indication that will highlight the routes that are protected using the &lt;strong&gt;&lt;em&gt;@ApiBearerAuth()&lt;/em&gt;&lt;/strong&gt; decorator. first define it in main, then implement it in &lt;u&gt;&lt;em&gt;users.controller.ts&lt;/em&gt;&lt;/u&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmora6rzb8syfztz86v3b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmora6rzb8syfztz86v3b.png" alt="src/main.ts" width="800" height="296"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh3zll2aj8kat2c4fc0jw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh3zll2aj8kat2c4fc0jw.png" alt="users/users.controller.ts" width="800" height="541"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AND NOW YOU'RE ALL SET&lt;/strong&gt;😮‍💨&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Easiest way of Implementing Authentication in NestJS</title>
      <dc:creator>Harold Mudosa</dc:creator>
      <pubDate>Fri, 22 Nov 2024 11:03:00 +0000</pubDate>
      <link>https://forem.com/haroldmud/easiest-way-of-implementing-authentication-in-nestjs-4jjd</link>
      <guid>https://forem.com/haroldmud/easiest-way-of-implementing-authentication-in-nestjs-4jjd</guid>
      <description>&lt;p&gt;In this article you'll be walked through the most straightforward guide of implementing authentication in NestJS, the most efficient scalable server-side applications builder among NodeJS frameworks.&lt;/p&gt;

&lt;p&gt;We'll cover best practices, fundamental concepts that I believe you already have a slight prerequisite in by now and clearer examples. The purpose is to help you build the most robust authentication systems for your NestJS applications from simple concepts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54mmtcikcjgeleoymw1j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54mmtcikcjgeleoymw1j.png" alt="Authentication Implementation with NestJS" width="686" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In simple words, Authentication is a process used to show that something or someone is legit, genuine or valid. hence authentication is a way of verifying if the user is really who they pretend to be before giving them access to a system or to some resources of this latter.&lt;/p&gt;

&lt;p&gt;This goes hand in hand with Authorization, which is a process by which the server determines if a user has permission to have access on a specific resource of a system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Developer Viewpoint Scenario
&lt;/h2&gt;

&lt;p&gt;Before talking about Authentication, the client should register himself as a user of our system, and for that he will need to sign up, a process where he will post the credentials that will allow the system to recognize him later on.&lt;/p&gt;

&lt;p&gt;Now talking of Authentication, the user will initiate the process by passing in his username/email and password, which will trigger the server to generate a JWT(Json Web Token) upon a successful authentication, Token that will then be transmitted as a bearer token in the authentication header for verification.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step by Step guide:&lt;br&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step_1. Create a resource for the users credentials
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;I assume that you already know how to create resources in NestJS, if not click &lt;a href="https://www.freecodecamp.org/news/build-a-crud-rest-api-with-nestjs-docker-swagger-prisma/" rel="noopener noreferrer"&gt;here&lt;/a&gt; to learn how to do so.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Well, first thing first create a resource called maybe "user", this will manage the users credential data, remember the user credentials have to be stored somewhere, right ?&lt;br&gt;&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step_2. Implement authentication in your REST API.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Run the following command and when prompted don’t generate the CRUD, this auth is a special type of resource.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm nest generate resource auth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Install and configure passport(a library that manage authentication states).
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npm install --save @nestjs/passport passport @nestjs/jwt passport-jwt&lt;br&gt;
npm install --save-dev @types/passport-jwt&lt;br&gt;
npm install passport-jwt&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;em&gt;auth.modute.ts&lt;/em&gt;, add the following highlighted code-lines[by red dots 🔴].&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4nmfmhwbyxfr8efvk31z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4nmfmhwbyxfr8efvk31z.png" alt="auth.module.ts" width="800" height="509"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implement a POST endpoint at &lt;strong&gt;&lt;em&gt;/auth/login&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Create a new file called &lt;u&gt;&lt;em&gt;login.dto.ts&lt;/em&gt;&lt;/u&gt; inside the &lt;em&gt;&lt;u&gt;src/auth/dto&lt;/u&gt;&lt;/em&gt; folder and then define the &lt;strong&gt;&lt;em&gt;LoginDto&lt;/em&gt;&lt;/strong&gt; class interface based on the email and password properties for the login.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31nzhzch2of5qrjzc8su.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31nzhzch2of5qrjzc8su.png" alt="auth/dto/login.dto.ts" width="800" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new file called &lt;em&gt;&lt;u&gt;auth.entity.ts&lt;/u&gt;&lt;/em&gt; inside the src/auth/entity that will describe the the shape of the &lt;strong&gt;jwt&lt;/strong&gt; payload.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftyab8v9id2n4cgjahthx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftyab8v9id2n4cgjahthx.png" alt="auth/entity/auth.entity.ts" width="800" height="335"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new login method inside the &lt;u&gt;&lt;em&gt;auth.service.ts&lt;/em&gt;&lt;/u&gt; file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkp6znuehf50ctimjkwam.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkp6znuehf50ctimjkwam.png" alt="auth/auth.service.ts" width="800" height="519"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now, In &lt;u&gt;&lt;em&gt;auth.controller.ts&lt;/em&gt;&lt;/u&gt; create a POST endpoint at &lt;strong&gt;&lt;em&gt;/auth/login&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Figkfe6dczuz1p5yp87hv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Figkfe6dczuz1p5yp87hv.png" alt="auth/auth.controller.ts" width="800" height="538"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step_3. Implement JWT authentication strategy
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;First create a new file called &lt;u&gt;&lt;em&gt;jwt.strategy.ts&lt;/em&gt;&lt;/u&gt; inside the &lt;u&gt;&lt;em&gt;src/auth/strategy&lt;/em&gt;&lt;/u&gt; directory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6mfeuvrqv4551mc6tihy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6mfeuvrqv4551mc6tihy.png" alt="auth/strategy/jwt.strategy.ts" width="800" height="530"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Into &lt;u&gt;&lt;em&gt;Auth.module.ts&lt;/em&gt;&lt;/u&gt; import then add &lt;strong&gt;&lt;em&gt;JwtStrategy&lt;/em&gt;&lt;/strong&gt; in the imports and &lt;em&gt;&lt;strong&gt;usersModule&lt;/strong&gt;&lt;/em&gt; in the providers[see red dots 🔴].&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr3368bfdxqlr3jl9yqm1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr3368bfdxqlr3jl9yqm1.png" alt="auth/auth.module.ts" width="800" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To make &lt;em&gt;&lt;strong&gt;UsersService&lt;/strong&gt;&lt;/em&gt; accessible in our class(surely &lt;strong&gt;&lt;em&gt;AuthService&lt;/em&gt;&lt;/strong&gt;), you also need to add it in the exports of the &lt;em&gt;&lt;strong&gt;UsersModule&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs2j6yljjii3irt23ok62.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs2j6yljjii3irt23ok62.png" alt="users/users.module.ts" width="800" height="402"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step_4. Protecting our system resources from unverified users
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;JWT auth guard&lt;/strong&gt; will be used to protect routes that require authentication in order to be accessed, in our case let's protect the &lt;strong&gt;users&lt;/strong&gt; resource API routes. so, in &lt;u&gt;&lt;em&gt;src/auth&lt;/em&gt;&lt;/u&gt;, create a file called &lt;u&gt;&lt;em&gt;jwt-auth.guard.ts&lt;/em&gt;&lt;/u&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fand342x5lo44xlitfkid.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fand342x5lo44xlitfkid.png" alt="auth/jwt-auth.guard.ts" width="800" height="208"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;NOTE:&lt;/strong&gt; The &lt;strong&gt;&lt;em&gt;AuthGuard&lt;/em&gt;&lt;/strong&gt; class expects the name of an authentication strategy. In this case, you are using the &lt;strong&gt;JwtStrategy&lt;/strong&gt; that you implemented in the previous section, which is named “&lt;strong&gt;&lt;em&gt;jwt&lt;/em&gt;&lt;/strong&gt;”.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can now use this guard (&lt;strong&gt;&lt;em&gt;UseGuards(JwtAuthGuard)&lt;/em&gt;&lt;/strong&gt;) as a decorator to protect your endpoints. Add the &lt;em&gt;&lt;strong&gt;JwtAuthGuard&lt;/strong&gt;&lt;/em&gt; to routes in the &lt;em&gt;&lt;strong&gt;UsersController&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv4igjxgig7hfwyhjko29.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv4igjxgig7hfwyhjko29.png" alt="users/user.controller.ts" width="800" height="862"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; If you're using &lt;strong&gt;Swagger&lt;/strong&gt;, integrate an authentication indication that will highlight the routes that are protected using the &lt;strong&gt;&lt;em&gt;@ApiBearerAuth()&lt;/em&gt;&lt;/strong&gt; decorator. first define it in main, then implement it in &lt;u&gt;&lt;em&gt;users.controller.ts&lt;/em&gt;&lt;/u&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmora6rzb8syfztz86v3b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmora6rzb8syfztz86v3b.png" alt="src/main.ts" width="800" height="296"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh3zll2aj8kat2c4fc0jw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh3zll2aj8kat2c4fc0jw.png" alt="users/users.controller.ts" width="800" height="541"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AND NOW YOU'RE ALL SET&lt;/strong&gt;😮‍💨&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Easiest way of Implementing Authentication in NestJS</title>
      <dc:creator>Harold Mudosa</dc:creator>
      <pubDate>Fri, 22 Nov 2024 11:03:00 +0000</pubDate>
      <link>https://forem.com/haroldmud/easiest-way-of-implementing-authentication-in-nestjs-11ko</link>
      <guid>https://forem.com/haroldmud/easiest-way-of-implementing-authentication-in-nestjs-11ko</guid>
      <description>&lt;p&gt;In this article you'll be walked through the most straightforward guide of implementing authentication in NestJS, the most efficient scalable server-side applications builder among NodeJS frameworks.&lt;/p&gt;

&lt;p&gt;We'll cover best practices, fundamental concepts that I believe you already have a slight prerequisite in by now and clearer examples. The purpose is to help you build the most robust authentication systems for your NestJS applications from simple concepts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54mmtcikcjgeleoymw1j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F54mmtcikcjgeleoymw1j.png" alt="Authentication Implementation with NestJS" width="686" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In simple words, Authentication is a process used to show that something or someone is legit, genuine or valid. hence authentication is a way of verifying if the user is really who they pretend to be before giving them access to a system or to some resources of this latter.&lt;/p&gt;

&lt;p&gt;This goes hand in hand with Authorization, which is a process by which the server determines if a user has permission to have access on a specific resource of a system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Developer Viewpoint Scenario
&lt;/h2&gt;

&lt;p&gt;Before talking about Authentication, the client should register himself as a user of our system, and for that he will need to sign up, a process where he will post the credentials that will allow the system to recognize him later on.&lt;/p&gt;

&lt;p&gt;Now talking of Authentication, the user will initiate the process by passing in his username/email and password, which will trigger the server to generate a JWT(Json Web Token) upon a successful authentication, Token that will then be transmitted as a bearer token in the authentication header for verification.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step by Step guide:&lt;br&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step_1. Create a resource for the users credentials
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;I assume that you already know how to create resources in NestJS, if not click &lt;a href="https://www.freecodecamp.org/news/build-a-crud-rest-api-with-nestjs-docker-swagger-prisma/" rel="noopener noreferrer"&gt;here&lt;/a&gt; to learn how to do so.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Well, first thing first create a resource called maybe "user", this will manage the users credential data, remember the user credentials have to be stored somewhere, right ?&lt;br&gt;&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step_2. Implement authentication in your REST API.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Run the following command and when prompted don’t generate the CRUD, this auth is a special type of resource.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm nest generate resource auth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Install and configure passport(a library that manage authentication states).
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npm install --save @nestjs/passport passport @nestjs/jwt passport-jwt&lt;br&gt;
npm install --save-dev @types/passport-jwt&lt;br&gt;
npm install passport-jwt&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;em&gt;auth.modute.ts&lt;/em&gt;, add the following highlighted code-lines[by red dots 🔴].&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4nmfmhwbyxfr8efvk31z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4nmfmhwbyxfr8efvk31z.png" alt="auth.module.ts" width="800" height="509"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implement a POST endpoint at &lt;strong&gt;&lt;em&gt;/auth/login&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Create a new file called &lt;u&gt;&lt;em&gt;login.dto.ts&lt;/em&gt;&lt;/u&gt; inside the &lt;em&gt;&lt;u&gt;src/auth/dto&lt;/u&gt;&lt;/em&gt; folder and then define the &lt;strong&gt;&lt;em&gt;LoginDto&lt;/em&gt;&lt;/strong&gt; class interface based on the email and password properties for the login.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31nzhzch2of5qrjzc8su.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F31nzhzch2of5qrjzc8su.png" alt="auth/dto/login.dto.ts" width="800" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new file called &lt;em&gt;&lt;u&gt;auth.entity.ts&lt;/u&gt;&lt;/em&gt; inside the src/auth/entity that will describe the the shape of the &lt;strong&gt;jwt&lt;/strong&gt; payload.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftyab8v9id2n4cgjahthx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftyab8v9id2n4cgjahthx.png" alt="auth/entity/auth.entity.ts" width="800" height="335"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new login method inside the &lt;u&gt;&lt;em&gt;auth.service.ts&lt;/em&gt;&lt;/u&gt; file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkp6znuehf50ctimjkwam.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkp6znuehf50ctimjkwam.png" alt="auth/auth.service.ts" width="800" height="519"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now, In &lt;u&gt;&lt;em&gt;auth.controller.ts&lt;/em&gt;&lt;/u&gt; create a POST endpoint at &lt;strong&gt;&lt;em&gt;/auth/login&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Figkfe6dczuz1p5yp87hv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Figkfe6dczuz1p5yp87hv.png" alt="auth/auth.controller.ts" width="800" height="538"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step_3. Implement JWT authentication strategy
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;First create a new file called &lt;u&gt;&lt;em&gt;jwt.strategy.ts&lt;/em&gt;&lt;/u&gt; inside the &lt;u&gt;&lt;em&gt;src/auth/strategy&lt;/em&gt;&lt;/u&gt; directory.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6mfeuvrqv4551mc6tihy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6mfeuvrqv4551mc6tihy.png" alt="auth/strategy/jwt.strategy.ts" width="800" height="530"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Into &lt;u&gt;&lt;em&gt;Auth.module.ts&lt;/em&gt;&lt;/u&gt; import then add &lt;strong&gt;&lt;em&gt;JwtStrategy&lt;/em&gt;&lt;/strong&gt; in the imports and &lt;em&gt;&lt;strong&gt;usersModule&lt;/strong&gt;&lt;/em&gt; in the providers[see red dots 🔴].&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr3368bfdxqlr3jl9yqm1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr3368bfdxqlr3jl9yqm1.png" alt="auth/auth.module.ts" width="800" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To make &lt;em&gt;&lt;strong&gt;UsersService&lt;/strong&gt;&lt;/em&gt; accessible in our class(surely &lt;strong&gt;&lt;em&gt;AuthService&lt;/em&gt;&lt;/strong&gt;), you also need to add it in the exports of the &lt;em&gt;&lt;strong&gt;UsersModule&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs2j6yljjii3irt23ok62.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs2j6yljjii3irt23ok62.png" alt="users/users.module.ts" width="800" height="402"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step_4. Protecting our system resources from unverified users
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;JWT auth guard&lt;/strong&gt; will be used to protect routes that require authentication in order to be accessed, in our case let's protect the &lt;strong&gt;users&lt;/strong&gt; resource API routes. so, in &lt;u&gt;&lt;em&gt;src/auth&lt;/em&gt;&lt;/u&gt;, create a file called &lt;u&gt;&lt;em&gt;jwt-auth.guard.ts&lt;/em&gt;&lt;/u&gt;. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fand342x5lo44xlitfkid.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fand342x5lo44xlitfkid.png" alt="auth/jwt-auth.guard.ts" width="800" height="208"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;NOTE:&lt;/strong&gt; The &lt;strong&gt;&lt;em&gt;AuthGuard&lt;/em&gt;&lt;/strong&gt; class expects the name of an authentication strategy. In this case, you are using the &lt;strong&gt;JwtStrategy&lt;/strong&gt; that you implemented in the previous section, which is named “&lt;strong&gt;&lt;em&gt;jwt&lt;/em&gt;&lt;/strong&gt;”.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can now use this guard (&lt;strong&gt;&lt;em&gt;UseGuards(JwtAuthGuard)&lt;/em&gt;&lt;/strong&gt;) as a decorator to protect your endpoints. Add the &lt;em&gt;&lt;strong&gt;JwtAuthGuard&lt;/strong&gt;&lt;/em&gt; to routes in the &lt;em&gt;&lt;strong&gt;UsersController&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv4igjxgig7hfwyhjko29.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv4igjxgig7hfwyhjko29.png" alt="users/user.controller.ts" width="800" height="862"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; If you're using &lt;strong&gt;Swagger&lt;/strong&gt;, integrate an authentication indication that will highlight the routes that are protected using the &lt;strong&gt;&lt;em&gt;@ApiBearerAuth()&lt;/em&gt;&lt;/strong&gt; decorator. first define it in main, then implement it in &lt;u&gt;&lt;em&gt;users.controller.ts&lt;/em&gt;&lt;/u&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmora6rzb8syfztz86v3b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmora6rzb8syfztz86v3b.png" alt="src/main.ts" width="800" height="296"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh3zll2aj8kat2c4fc0jw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh3zll2aj8kat2c4fc0jw.png" alt="users/users.controller.ts" width="800" height="541"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AND NOW YOU'RE ALL SET&lt;/strong&gt;😮‍💨&lt;/p&gt;

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