<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dewmith Akalanka</title>
    <description>The latest articles on DEV Community by Dewmith Akalanka (@dewmyth).</description>
    <link>https://dev.to/dewmyth</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%2F1315158%2Fc043e7b5-7343-420f-8811-28021b6f28ca.png</url>
      <title>DEV Community: Dewmith Akalanka</title>
      <link>https://dev.to/dewmyth</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dewmyth"/>
    <language>en</language>
    <item>
      <title>How to Create a quick Authentication library for NestJS/MongoDB application</title>
      <dc:creator>Dewmith Akalanka</dc:creator>
      <pubDate>Wed, 08 Jan 2025 18:55:55 +0000</pubDate>
      <link>https://dev.to/dewmyth/how-to-create-a-quick-authentication-library-for-nestjsmongodb-application-45bh</link>
      <guid>https://dev.to/dewmyth/how-to-create-a-quick-authentication-library-for-nestjsmongodb-application-45bh</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;startOfSoftwareDevelopingLife&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;endOfSoftwareDevelopingLife&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="nc"&gt;DO &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;AUTHENTICATION!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;NPM Library - &lt;a href="https://www.npmjs.com/package/dewmyth-auth-nestjs-mongo" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/dewmyth-auth-nestjs-mongo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GitHub Repository - &lt;a href="https://github.com/dewMyth/dewmyth-auth-nestjs-mongo" rel="noopener noreferrer"&gt;https://github.com/dewMyth/dewmyth-auth-nestjs-mongo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;AUTHENTICATION, one of most important (yet boring 😒) process when building a software application. However you can't avoid that even though it is the same thing again and again. So my try here is to make your life easier for you when you need an authentication flow. Just use two function calls to register a user and login a user. 😎&lt;/p&gt;

&lt;p&gt;Note: If you are very new to software development and authentication concepts, Please do and practice the authentication, implement those by scratch. Don't use these kinds of libraries.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What does this library do?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;This is a NestJS based library that uses Mongo DB as the database to save the user and authenticate user by generating a JWT token.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you are not familiar with NestJS, think it as the JavaScript framework that similar to what Spring for Java or what .NET for C#. (Pros, don't throw rocks at me 🥸). If you are a NodeJS developer that never excavated NestJS, give it a try. They have one of the best documentations (&lt;a href="https://nestjs.com/" rel="noopener noreferrer"&gt;https://nestjs.com/&lt;/a&gt;) I have ever seen in my tiny developing life 👶. &lt;/p&gt;

&lt;h2&gt;
  
  
  Who need this library?
&lt;/h2&gt;

&lt;p&gt;For those who want to focus more on your business logic of the application rather than allocating time for an authentication process.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Functionality&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Once you have generated a MongoDB database connection URI and provided it to the library, you will have access to the register user and login user functions.&lt;/p&gt;

&lt;h4&gt;
  
  
  Register User
&lt;/h4&gt;

&lt;p&gt;For this &lt;code&gt;createUser&lt;/code&gt; function, you have to do only passing the email, password (username as well, but it is optional). It will generate a user object as below with a hashed password. (with 10 salt rounds)&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%2F6k4cg4zd88ivt6z2jgz6.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%2F6k4cg4zd88ivt6z2jgz6.png" alt="Image description" width="800" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Login User
&lt;/h4&gt;

&lt;p&gt;In &lt;code&gt;loginUser&lt;/code&gt; function, you have to pass the email and password to get an JWT authentication token. Optionally you can pass the JWT Secret key and token TTL (time to live) as well. &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%2F7ypzajikp9lbtfkeq1yh.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%2F7ypzajikp9lbtfkeq1yh.png" alt="Image description" width="800" height="133"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;More on How to use the library - &lt;a href="https://github.com/dewMyth/dewmyth-auth-nestjs-mongo/blob/master/README.md" rel="noopener noreferrer"&gt;https://github.com/dewMyth/dewmyth-auth-nestjs-mongo/blob/master/README.md&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;How I implemented this library&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;OK!!! Now that the shortcut gang, those who just want to use the library without breaking a sweat have left the building, it’s just us 🤣. Let's dive into how to implement this NPM library from scratch. So you can build your own. 😎&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 1 - Create a NestJS Application. - &lt;a href="https://docs.nestjs.com/" rel="noopener noreferrer"&gt;https://docs.nestjs.com/&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;This will create a boilerplate NestJS application.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 2 - Install &lt;code&gt;mongoose&lt;/code&gt; and &lt;code&gt;@nestjs/mongoose&lt;/code&gt; -&lt;a href="https://docs.nestjs.com/techniques/mongodb" rel="noopener noreferrer"&gt;https://docs.nestjs.com/techniques/mongodb&lt;/a&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i @nestjs/mongoose mongoose
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will be used to create the MongoDB schema for the users database/collection.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 3 - Create the &lt;code&gt;auth&lt;/code&gt; module.
&lt;/h4&gt;

&lt;p&gt;Use the following command to create a new module called &lt;code&gt;auth&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nest g res auth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use REST API and then press "n" to avoid creating sample CRUD endpoints.&lt;/p&gt;

&lt;p&gt;Then make sure to import the &lt;code&gt;AuthModule&lt;/code&gt; to the &lt;code&gt;AppModule&lt;/code&gt; imports if it is not added to there already.&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%2Fxxk8n9hkqvuln73oc4x7.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%2Fxxk8n9hkqvuln73oc4x7.png" alt="Image description" width="508" height="296"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 4 - Create the UserSchema with the User data you need. Make sure to have a unique field to identify a user (such as email or username) and password.
&lt;/h4&gt;

&lt;p&gt;File location - &lt;code&gt;src/auth/schemas/user-schema.ts&lt;/code&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%2Fhrin32t3u2lz85j7gadg.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%2Fhrin32t3u2lz85j7gadg.png" alt="Image description" width="674" height="523"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;HAHA! I am not going to make it textual. DO IT FOR YOURSELF. Write the code. Don't Copy N Paste.&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 5 - Create DTO types.
&lt;/h4&gt;

&lt;p&gt;File location - &lt;code&gt;src/auth/dto/create-user.dto.ts&lt;/code&gt;, &lt;code&gt;src/auth/dto/login-user.dto.ts&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We will need TWO DTO types for this. One to &lt;code&gt;createUserDTO&lt;/code&gt; and to &lt;code&gt;LoginUserDTO&lt;/code&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%2Fu8ipi9e7u10w9m3tr2em.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%2Fu8ipi9e7u10w9m3tr2em.png" alt="Image description" width="348" height="228"&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%2Fewo2mm6j7f6dmcpis1e8.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%2Fewo2mm6j7f6dmcpis1e8.png" alt="Image description" width="329" height="187"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Why we need DTO ? Since we are making a library, it is very important to make the life easier for the library user. So here if we use the DTOs for the input, when the exposed functions are used by the user it will show the relevant parameters as suggestions. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Sample Usage when using the library:&lt;/em&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%2Fpp8behnl3t4kz7xx4wjo.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%2Fpp8behnl3t4kz7xx4wjo.png" alt="Image description" width="569" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 6 -  Like wise I have created a response type for the logged user as well.
&lt;/h4&gt;

&lt;p&gt;File location - &lt;code&gt;src/auth/types/login-user.response.ts&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Like the suggestions for input parameters above, we will have the Promise type for the logged in user's data when we use this login user response type.&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%2Fa281euk54zfhm77mmszb.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%2Fa281euk54zfhm77mmszb.png" alt="Image description" width="372" height="206"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Sample Usage when using the library:&lt;/em&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%2F0uib12j8zi0e0gja4ljq.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%2F0uib12j8zi0e0gja4ljq.png" alt="Image description" width="800" height="147"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 7 - Inject the UserSchema / Model to your &lt;code&gt;AuthService&lt;/code&gt;
&lt;/h4&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%2Ffvuyd2kxr8fzxvz0n1ij.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%2Ffvuyd2kxr8fzxvz0n1ij.png" alt="Image description" width="727" height="161"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 8 - Install &lt;code&gt;bcrypt&lt;/code&gt; library and import the library to &lt;code&gt;AuthService&lt;/code&gt; - &lt;a href="https://www.npmjs.com/package/bcrypt" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/bcrypt&lt;/a&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;bcrypt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2F99i6lf7gf518muf3gjse.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%2F99i6lf7gf518muf3gjse.png" alt="Image description" width="381" height="118"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This library will be used to hash the password.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 9 - Implement the &lt;code&gt;createUser&lt;/code&gt; function.
&lt;/h4&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%2Fsei1htt1ugyd8158lclz.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%2Fsei1htt1ugyd8158lclz.png" alt="Image description" width="726" height="690"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pass the user entered data to the createUser function. It should be under the type &lt;code&gt;CreateUserDTO&lt;/code&gt; and should also map with the &lt;code&gt;UserSchema&lt;/code&gt; created above.&lt;/p&gt;

&lt;p&gt;Then extract the email and password from the payload.&lt;/p&gt;

&lt;p&gt;Email for the checking an existing user with the same email : &lt;br&gt;
Do a simple Mongoose &lt;code&gt;findOne()&lt;/code&gt; using the email and if there is a user already. Throw the shown error. This will stop the function's further processing and will throw the error to the user.&lt;/p&gt;

&lt;p&gt;Password for the hashing process : &lt;br&gt;
Use &lt;code&gt;bcrypt&lt;/code&gt; library's &lt;code&gt;.hash()&lt;/code&gt; function to make the password. I have use 10 round of salts for the hashing. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you want to give a customize number to user to control the salt rounds, pass another argument to this &lt;code&gt;createUser&lt;/code&gt; function and hash using that. More power to the user 💪.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;Then attached the hashed password to the final payload and do save in the MongoDB using the Mongoose &lt;code&gt;.save()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;I have also returned the saved user as the final output of the &lt;code&gt;createUser&lt;/code&gt; function's happy path.&lt;/p&gt;
&lt;h4&gt;
  
  
  Step 10 - Install &lt;code&gt;jsonwebtoken&lt;/code&gt; library and import the library to &lt;code&gt;AuthService&lt;/code&gt; - &lt;a href="https://www.npmjs.com/package/jsonwebtoken" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/jsonwebtoken&lt;/a&gt;
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i jsonwebtoken
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fdxhzjne1yed3lxoe6j36.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%2Fdxhzjne1yed3lxoe6j36.png" alt="Image description" width="400" height="111"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Step 11 - Implement the &lt;code&gt;loginUser&lt;/code&gt; functionality
&lt;/h4&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%2Fprp0q9p6063ix53x7q6c.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%2Fprp0q9p6063ix53x7q6c.png" alt="Image description" width="730" height="838"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pass the user entered data, it should be in the type of &lt;code&gt;LoginUserDTO&lt;/code&gt;. Apart from that I have also used 2 optional arguments. &lt;code&gt;secret&lt;/code&gt; - to pass the JWTSecret key and &lt;code&gt;tokenTTL&lt;/code&gt; - to assign a TTL for the JWT token.&lt;/p&gt;

&lt;p&gt;Extract the email and the password. &lt;/p&gt;

&lt;p&gt;Use email and mongoose &lt;code&gt;findOne()&lt;/code&gt; to check there a user with an existing email. If not throw an error to the user.&lt;/p&gt;

&lt;p&gt;If there is user with that email, Then need to check the password hash is match for the saved salt for that email's user. For that we can use &lt;code&gt;bcrypt&lt;/code&gt; library's &lt;code&gt;.compare()&lt;/code&gt; function with the DB fetched user's hash and the password that the user has entered under the &lt;code&gt;loginCredentials&lt;/code&gt;. If it's not a match, throw an error message to the User.&lt;/p&gt;

&lt;p&gt;If it is a match, procced with creating the JWT token for that user by using the &lt;code&gt;jsonwebtoken&lt;/code&gt;'s &lt;code&gt;sign&lt;/code&gt; function. Pass the data that you want to encrypt inside the token. In this case I have attached the user's email. (So once the user is logged in to the service, we can decode and get the user's email). If there are no parameters to the optional arguments. By default secret will be &lt;code&gt;secret&lt;/code&gt; and the tokenTTL will be 1h (one hour).&lt;/p&gt;

&lt;p&gt;Now the the business logics of the NestJS/MongoDB Authentication is done.&lt;/p&gt;
&lt;h4&gt;
  
  
  Step 12 - Make &lt;code&gt;AuthModule&lt;/code&gt; a Dynamic Module. -- IMPORTANT
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;When you need to use a general NestJS module for different use case scenario's you need to make that NestJS module a dynamic module. -&lt;a href="https://docs.nestjs.com/fundamentals/dynamic-modules" rel="noopener noreferrer"&gt;https://docs.nestjs.com/fundamentals/dynamic-modules&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here, our &lt;code&gt;AuthModule&lt;/code&gt; should accept a Mongo Connection, when it is used by different different users that Mongo Connection should be variable. So we have to make this &lt;code&gt;AuthModule&lt;/code&gt; a reusable dynamic module and pass an argument to accept the Mongo Connection URI.&lt;/p&gt;

&lt;p&gt;Now your Dynamic &lt;code&gt;AuthModule&lt;/code&gt; should be like this. Replace the existing &lt;code&gt;AuthModule&lt;/code&gt; with the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;DynamicModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Global&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Module&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AuthService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./auth.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AuthController&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./auth.controller&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;MongooseModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/mongoose&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;UserSchema&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./schemas/user.schema&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AuthModuleOptions&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./auth-module-options&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Global&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AuthModule&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nf"&gt;forRoot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AuthModuleOptions&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;DynamicModule&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;module&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AuthModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nx"&gt;MongooseModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forRoot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mongoUri&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nx"&gt;MongooseModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forFeature&lt;/span&gt;&lt;span class="p"&gt;([{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UserSchema&lt;/span&gt; &lt;span class="p"&gt;}]),&lt;/span&gt;
      &lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="na"&gt;controllers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;AuthController&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;AuthService&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="na"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;AuthService&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the &lt;code&gt;import&lt;/code&gt; array of the dynamic &lt;code&gt;AuthModule&lt;/code&gt;. Mongoose Connection URI should be coming from the options.&lt;/p&gt;

&lt;p&gt;Create another type file that accept a string as the mongo uri. That should be the type that dynamic &lt;code&gt;AuthModule&lt;/code&gt; should accept as parameters.&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%2F9kfaqn2fq0xurr2zm60z.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%2F9kfaqn2fq0xurr2zm60z.png" alt="Image description" width="416" height="159"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Apart from the imports, as usual keep the test controllers and the exposed services as providers and exports.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 13 - Build and Publish the NPM library
&lt;/h4&gt;

&lt;p&gt;Now we need to export out our dynamic &lt;code&gt;AuthModule&lt;/code&gt; and the &lt;code&gt;AuthService&lt;/code&gt; where the business logic contains.&lt;/p&gt;

&lt;p&gt;Create a file called &lt;code&gt;index.ts&lt;/code&gt; in the &lt;code&gt;src&lt;/code&gt; directory. And export all the content of that above mentioned files as below.&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%2Ffnzmzg1gmpydgcb4jki2.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%2Ffnzmzg1gmpydgcb4jki2.png" alt="Image description" width="800" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, change the &lt;code&gt;main:&lt;/code&gt; key in the &lt;code&gt;package.json&lt;/code&gt; as below.&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%2Fiymiq01kt89sy921nt3p.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%2Fiymiq01kt89sy921nt3p.png" alt="Image description" width="800" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;dist&lt;/code&gt; is the directory where the build files of the NestJS application will be kept. (If you didn't changed in the &lt;code&gt;tsconfig.json&lt;/code&gt; 's &lt;code&gt;outDir&lt;/code&gt; key).&lt;/p&gt;

&lt;p&gt;Now do build the created library. 😎&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see something like this in the &lt;code&gt;dist/index.js&lt;/code&gt; file. You are good to go. (Exported files should be there as code.)&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%2Fxirh5vx2wwmbh5hpueow.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%2Fxirh5vx2wwmbh5hpueow.png" alt="Image description" width="800" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, do a&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And login to the &lt;a href="https://www.npmjs.com/" rel="noopener noreferrer"&gt;https://www.npmjs.com/&lt;/a&gt; and create an account.&lt;/p&gt;

&lt;p&gt;Then you will be able to the publish your library. &lt;/p&gt;

&lt;p&gt;The name of your library will be as per the &lt;code&gt;name&lt;/code&gt; key of &lt;code&gt;package.json&lt;/code&gt;. So make sure to have unique package name before publishing.&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%2Fq75riz41pyei935on37v.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%2Fq75riz41pyei935on37v.png" alt="Image description" width="506" height="183"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;DO PUBLISH!!!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm publish
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Boom!!!. You are done. &lt;br&gt;
Congratulations Neo!&lt;/p&gt;

&lt;p&gt;⠀⠀⠀⠀⠀⠀⢀⣤⣶⣶⣶⣶⣦⣤⣀⠀⠀⠀⠀⠀&lt;br&gt;
⠀⠀⢀⣤⣶⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⡀⠀⠀&lt;br&gt;
⠀⢠⣿⣿⣿⣿⣿⠿⣿⣿⣿⣿⠿⠿⢿⣿⣿⣷⡀⠀&lt;br&gt;
⠀⢸⣿⡿⠋⠁⠀⠀⠀⠉⠉⠀⠀⠀⠀⠈⢹⣿⡇⠀&lt;br&gt;
⠀⢸⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⡇⠀&lt;br&gt;
⠀⢸⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⡇⠀&lt;br&gt;
⠀⢸⣿⣠⣤⣶⣶⣶⣦⣀⣀⣴⣶⣶⣶⣤⣄⣿⡇⡀&lt;br&gt;
⣿⣿⣿⠻⣿⣿⣿⣿⣿⠟⠻⣿⣿⣿⣿⣿⠟⣿⣿⣿&lt;br&gt;
⣿⣿⣿⠀⠈⠉⠛⠋⠉⠀⠀⠉⠙⠛⠉⠁⠀⣿⣿⣿&lt;br&gt;
⠙⢿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⡿⠃&lt;br&gt;
⠀⠸⣿⣧⠀⠀⠀⢀⣀⣀⣀⣀⡀⠀⠀⠀⣼⣿⠇⠀&lt;br&gt;
⠀⠀⠙⢿⣷⣄⠀⠈⠉⠉⠉⠉⠁⠀⣠⣾⡿⠃⠀⠀&lt;br&gt;
⠀⠀⠀⢸⣿⣿⣷⣤⣀⣀⣀⣀⣤⣾⣿⣿⡅⠀⠀⠀&lt;br&gt;
⠀⠀⢰⣿⣿⣿⣿⣿⣿⡿⠿⢿⣿⣿⣿⣿⣿⡄⠀⠀&lt;br&gt;
⠀⠀⠻⠿⠿⠿⠿⠿⠿⠷⠴⠿⠿⠿⠿⠿⠿⠇⠀⠀&lt;/p&gt;

&lt;p&gt;You have break the Do Authentication cycle. (in a way)&lt;/p&gt;

&lt;p&gt;Now you can use this library on your next NestJS project. More on how to use this library - &lt;a href="https://github.com/dewMyth/dewmyth-auth-nestjs-mongo/blob/master/README.md" rel="noopener noreferrer"&gt;https://github.com/dewMyth/dewmyth-auth-nestjs-mongo/blob/master/README.md&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Of course, This is not a perfect library. Many more optimization and features can be added. For an example having dynamic fields for User model, enhance the security of the login functionality, Authorization processes and etc. So feel free to fork and make the PRs if you are interested.&lt;/p&gt;

&lt;p&gt;Thanks for reading my long long long article. &lt;/p&gt;

&lt;p&gt;If you have any concerns or any criticism please free to drop a comment or contact me through the LinkedIn (&lt;a href="http://www.linkedin.com/in/dewmith-akalanka" rel="noopener noreferrer"&gt;www.linkedin.com/in/dewmith-akalanka&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Happy Coding!!!.&lt;/p&gt;

</description>
      <category>authentication</category>
      <category>mongodb</category>
      <category>nestjs</category>
      <category>jwt</category>
    </item>
  </channel>
</rss>
