<?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: Afolabi Esther</title>
    <description>The latest articles on Forem by Afolabi Esther (@platinum_hestia).</description>
    <link>https://forem.com/platinum_hestia</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%2F1050951%2F64afeaae-cafb-4d2f-a1f1-2bb8ca74ccb6.png</url>
      <title>Forem: Afolabi Esther</title>
      <link>https://forem.com/platinum_hestia</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/platinum_hestia"/>
    <language>en</language>
    <item>
      <title>Subscription app (part 5)</title>
      <dc:creator>Afolabi Esther</dc:creator>
      <pubDate>Fri, 19 Apr 2024 21:38:04 +0000</pubDate>
      <link>https://forem.com/platinum_hestia/subscription-app-part-5-4cgk</link>
      <guid>https://forem.com/platinum_hestia/subscription-app-part-5-4cgk</guid>
      <description>&lt;p&gt;In the previous post, I created a login route.&lt;/p&gt;

&lt;p&gt;In this post, I'll be creating a user route and a middleware to verify a user.&lt;/p&gt;

&lt;p&gt;Before a user can access the profile page after logging in or signing up, a request needs to be sent with the token received to verify the user before getting access to the profile page.&lt;/p&gt;

&lt;p&gt;The user route will be a protected route and it'll send the user details. The user needs to sign up or login before hitting the route.&lt;/p&gt;

&lt;p&gt;The user route will receive the token, extract the payload from the token, verify it and return the data to the client.&lt;/p&gt;

&lt;p&gt;A middleware will be created for this steps.&lt;/p&gt;

&lt;p&gt;This middleware intercepts the request to get access to a protected route.&lt;/p&gt;

&lt;p&gt;It will extract the token from the request header, verify it and check if the user is authenticated before getting access to a protected route. If the the user is not authenticated, then it will throw an error. &lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the middleware
&lt;/h2&gt;

&lt;p&gt;In the src directory, create a new directory named "middleware."&lt;/p&gt;

&lt;p&gt;Then inside it, create a file named "checkAuth.ts"&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%2Fv2mwintkkel17idavets.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%2Fv2mwintkkel17idavets.png" alt="auth 1" width="800" height="438"&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%2Fe3e44y1gdau45f4n2xjr.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%2Fe3e44y1gdau45f4n2xjr.png" alt="auth 2" width="737" height="457"&gt;&lt;/a&gt;&lt;br&gt;
Create and export a function which takes in the request, response and next parameters&lt;/p&gt;

&lt;p&gt;Logic for verifying the token:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;get the token from the request header (ln 9)&lt;/li&gt;
&lt;li&gt;check if there's a token (ln 11–19)&lt;/li&gt;
&lt;li&gt;extract the token (ln 21). &lt;/li&gt;
&lt;li&gt;In a try-and-catch block, verify the token with JWT and set a property on the user to have the verified email and catch any errors.( ln 23–41) &lt;/li&gt;
&lt;/ol&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%2Fnbyykc2n0r4siln91p2r.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%2Fnbyykc2n0r4siln91p2r.png" alt="c-3" width="537" height="272"&gt;&lt;/a&gt;&lt;br&gt;
 Make sure to define the type for Typescript  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To define the property for the user in typescript&lt;/li&gt;
&lt;li&gt;in the project root, create a @types directory, express directory and an index.ts 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%2Fu1ifqjwz17iyprteji1b.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%2Fu1ifqjwz17iyprteji1b.png" alt="c-4" width="800" height="215"&gt;&lt;/a&gt;&lt;br&gt;
And add the types to the &lt;code&gt;tsconfig.json&lt;/code&gt; file.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;execute the next route (ln 31)&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Using the middleware...
&lt;/h4&gt;

&lt;h2&gt;
  
  
  Create the user route
&lt;/h2&gt;

&lt;p&gt;In the &lt;code&gt;auth.ts&lt;/code&gt; file, create a get request for getting the user profile.&lt;/p&gt;

&lt;p&gt;And import the middleware.&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%2Fbh787bre79to7pgq576g.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%2Fbh787bre79to7pgq576g.png" alt="using the middleware" width="800" height="482"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;right here, the user route is created with the middleware placed in-between the path and the call-back function&lt;/li&gt;
&lt;li&gt;find the user in the DB (ln 146)&lt;/li&gt;
&lt;li&gt;send the data to the client (ln. 148–157)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Testing the code...
&lt;/h4&gt;

&lt;p&gt;In postman:&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%2F34qafd60z20fdozhypwx.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%2F34qafd60z20fdozhypwx.png" alt="testing the user route" width="800" height="387"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create a new GET request&lt;/li&gt;
&lt;li&gt;change the route url &lt;/li&gt;
&lt;li&gt;login an existing user to get a token&lt;/li&gt;
&lt;li&gt;copy the token and place inside of the header&lt;/li&gt;
&lt;li&gt;send &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Yay! I got the token verified and the user data.&lt;/p&gt;

&lt;p&gt;In the next post, I will be setting up Stripe and creating the subs/prices route.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Generate token</title>
      <dc:creator>Afolabi Esther</dc:creator>
      <pubDate>Tue, 02 Apr 2024 12:08:34 +0000</pubDate>
      <link>https://forem.com/platinum_hestia/day-9-12-of-subscription-app-1lae</link>
      <guid>https://forem.com/platinum_hestia/day-9-12-of-subscription-app-1lae</guid>
      <description>&lt;p&gt;In the previous &lt;a href="https://dev.to/hestia/day-5-8-of-subscription-app-55i8"&gt;post&lt;/a&gt;, I setup and connected the database with the server, verified the email in the database, hashed the password, and saved the user to DB.&lt;/p&gt;

&lt;p&gt;In this post, I'll be sending back a token once the user has been saved to DB. Also, create a login route.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Send back token
&lt;/h2&gt;

&lt;p&gt;After a user has been signed up successfully, a token has to be sent back to give the user access to the application.&lt;/p&gt;

&lt;p&gt;The token will be a jwt (json web token). This article gives a great explanation.&lt;/p&gt;

&lt;p&gt;The token is a way of identifying who the user is on the frontend.&lt;/p&gt;

&lt;p&gt;The token has to be created and sent to the client, and then the client will send back the token to the server with every single request made.&lt;/p&gt;

&lt;p&gt;The token consists of 3 parts: the header, the payload, and the verify signature.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create the token
&lt;/h3&gt;

&lt;p&gt;I installed the package&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install jsonwebtoken&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Also, install the types&lt;br&gt;
&lt;code&gt;npm install @types/jsonwebtoken --save-dev&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%2Fc77bv0hda7ndf0pqtxcs.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%2Fc77bv0hda7ndf0pqtxcs.png" alt="create token" width="800" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Code explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;64–70: create a const variable named &lt;code&gt;token&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;generates a JWT containing the user's email and sets its expiration to 1 hour.&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;JWT_SECRET&lt;/code&gt; secret key is crucial for signing the JWT and ensuring its integrity.&lt;/li&gt;
&lt;li&gt;ln 73 - 83: send back token to the user with user information.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Testing the coding...
&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%2Ftzq7pmhehl8hncwwhnat.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%2Ftzq7pmhehl8hncwwhnat.png" alt="sent token" width="800" height="372"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Signing up with a new email.&lt;/p&gt;

&lt;p&gt;Yay! I got a token and the user data.&lt;/p&gt;

&lt;h1&gt;
  
  
  Create login route
&lt;/h1&gt;

&lt;p&gt;The login steps will be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;get user data from the client and find the user in the DB&lt;/li&gt;
&lt;li&gt;compare the hashed password&lt;/li&gt;
&lt;li&gt;send back a token&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Get and find user
&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%2Fcekuuo4e3jrw2b6hdy8o.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%2Fcekuuo4e3jrw2b6hdy8o.png" alt="find user in db" width="695" height="343"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;-ln 94: right here, I get user data from the request body&lt;br&gt;
-ln 96: find user in DB&lt;br&gt;
-ln 98 - 104: if there's no user, send this error&lt;/p&gt;

&lt;p&gt;If there's no error from this step, go ahead to compare the passwords&lt;/p&gt;

&lt;h2&gt;
  
  
  Compare passwords
&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%2F2h9cpiqrkff027frjsw5.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%2F2h9cpiqrkff027frjsw5.png" alt="compare passwords" width="737" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ln 108:  using &lt;code&gt;bcryptjs&lt;/code&gt; to compare the password from the request body with the hashed password from the database &lt;/li&gt;
&lt;li&gt;ln 110-119: check if isMatch is false, send this error message&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If there's no error from this step, send back a token &lt;/p&gt;

&lt;h3&gt;
  
  
  Send back a token
&lt;/h3&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%2F52aqlct3z68lwxz8a7h1.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%2F52aqlct3z68lwxz8a7h1.png" alt="sending back token in login route" width="728" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just as it's done in the sign-up route.&lt;/p&gt;

&lt;h4&gt;
  
  
  Testing the API...
&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%2Fmz9cfxla48hprleltc66.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%2Fmz9cfxla48hprleltc66.png" alt="login successful" width="800" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create another post request named "login" (simply duplicate the sign-up request)&lt;/li&gt;
&lt;li&gt;put in the appropriate url&lt;/li&gt;
&lt;li&gt;in the body, put in the email and password of an existing user&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Login was successful, and a token was returned.&lt;/p&gt;

&lt;p&gt;Checking what happens if I use an incorrect password&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%2Fa112h4puu80qtkqinzm6.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%2Fa112h4puu80qtkqinzm6.png" alt="Incorrect password" width="800" height="355"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the next post, I'll create a check authentication middleware that extracts and verifies the token (gotten from the client) to get the user information.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Set up MongoDB</title>
      <dc:creator>Afolabi Esther</dc:creator>
      <pubDate>Fri, 22 Mar 2024 21:59:21 +0000</pubDate>
      <link>https://forem.com/platinum_hestia/day-5-8-of-subscription-app-55i8</link>
      <guid>https://forem.com/platinum_hestia/day-5-8-of-subscription-app-55i8</guid>
      <description>&lt;p&gt;Following the validation of username, email, and password in the previous &lt;a href="https://dev.to/hestia/day-2-4-of-subscription-app-1kle"&gt;post&lt;/a&gt;, we'll now verify if the provided email address already exists within the database.&lt;/p&gt;

&lt;p&gt;The steps for checking email:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;once we pass the validation step, &lt;/li&gt;
&lt;li&gt;get the email and fetch a particular record in the database that has that email&lt;/li&gt;
&lt;li&gt;if we get back a record, then send a response that the "email is already used, you can't create an account with it."&lt;/li&gt;
&lt;li&gt;if it returns null, then this step is passed, and we can go ahead and hash the password.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Firstly, we need to setup and connect the database with the server to execute these steps.&lt;/p&gt;

&lt;p&gt;Secondly, we need to define how the user entity will look inside the database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up the database (MongoDB)
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://www.mongodb.com/docs/guides/atlas/account/" rel="noopener noreferrer"&gt;doc&lt;/a&gt; is well detailed and very easy to follow to get started with MongoDB. &lt;/p&gt;

&lt;p&gt;Just follow the "Chapter 1 Atlas" six guides.&lt;/p&gt;

&lt;p&gt;These guides provide clear and concise instructions to ensure a smooth and successful database launch.&lt;/p&gt;

&lt;p&gt;NB: Copy your credentials from the security quick-start in a safe place.&lt;/p&gt;

&lt;p&gt;The steps are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sign Up for a MongoDB Account&lt;/li&gt;
&lt;li&gt;Create a Cluster&lt;/li&gt;
&lt;li&gt;Add a Database User&lt;/li&gt;
&lt;li&gt;Configure a Network Connection&lt;/li&gt;
&lt;li&gt;Load Sample Data (skip this step)&lt;/li&gt;
&lt;li&gt;Get Connection String&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%2Fwxi1xc5vrzqh0ilc0qsl.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%2Fwxi1xc5vrzqh0ilc0qsl.png" alt="connecting your app" width="800" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;connecting your app&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%2Fmaow2hazqxamea525swa.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%2Fmaow2hazqxamea525swa.png" alt="copy the connection string" width="800" height="419"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;copy the connection string👆&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once the above steps are done, save your connection string to your app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Save the URI
&lt;/h3&gt;

&lt;p&gt;Copy the URI and save it to your app.&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%2Fmy7zo9z29fpafjkk5cmu.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%2Fmy7zo9z29fpafjkk5cmu.png" alt="save uri" width="800" height="396"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To save...&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;inside the server, create a .env file&lt;/li&gt;
&lt;li&gt;then save your URI
" MONGO_URI=[ your URI ]"&lt;/li&gt;
&lt;li&gt;replace the password part with the credential you saved earlier from the security quick-start.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Connecting the database
&lt;/h2&gt;

&lt;p&gt;Mongoose package lib will be utilized to connect to the MongoDB database.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;install mongoose and dotenv packages. The dotenv package helps to readenvironment variables inside the .env file.&lt;br&gt;
"npm install mongoose dotenv"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;inside index.ts, import mongoose and dotenv&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%2Fxnsa8y07wqqilp1xpr5w.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%2Fxnsa8y07wqqilp1xpr5w.png" alt="mongoose connected" width="800" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code above:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ln 9: read the URI inside the .env file.&lt;/li&gt;
&lt;li&gt;ln 16-23: connect to DB&lt;/li&gt;
&lt;li&gt;pass in the URI&lt;/li&gt;
&lt;li&gt;log some message if we're connected or catch any error if something went wrong.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Yay! MongoDB is connected.&lt;/p&gt;

&lt;p&gt;NB: If you get this error when connecting to the database,&lt;br&gt;
"Error: MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist"&lt;/p&gt;

&lt;p&gt;Simply do this:&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%2Fvi3768dny3uhd6wof1tv.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%2Fvi3768dny3uhd6wof1tv.png" alt="Access error fixed" width="718" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Define the user entity
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;create a directory named "models" in the src directory.&lt;/li&gt;
&lt;li&gt;then create a user.ts file in the models directory. This is where we define the user entity.&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%2Fh7yv9pubkrw511mro39g.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%2Fh7yv9pubkrw511mro39g.png" alt="define user entity" width="800" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code above:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;import mongoose&lt;/li&gt;
&lt;li&gt;get schema from mongoose&lt;/li&gt;
&lt;li&gt;create a new schema and define the properties we want a user to have.&lt;/li&gt;
&lt;li&gt;then export the userSchema&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Verify if email already exists
&lt;/h2&gt;

&lt;p&gt;Back to the auth file...&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;import the userSchema as User (with this, we can perform CRUD operations on the DB)&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%2Fh81o765ffstins4s0j9b.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%2Fh81o765ffstins4s0j9b.png" alt="Image description" width="800" height="299"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ln 34: extract these values from the incoming request body object&lt;/li&gt;
&lt;li&gt;ln 36: the code here find a particular user that has the email we get from the req body in the DB&lt;/li&gt;
&lt;li&gt;since there's no user yet in our DB it will return null.&lt;/li&gt;
&lt;li&gt;we'll check this step later once we create a user&lt;/li&gt;
&lt;li&gt;ln 38: here we specify what should happen if there's a user (send a response that email is in use)&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  3. Hash the password
&lt;/h1&gt;

&lt;p&gt;We'll be using the bcryptjs library to hash the passwords. It securely hashes a user's password before storing it in a database, protecting sensitive information from unauthorized access.&lt;/p&gt;

&lt;p&gt;Install the package &lt;br&gt;
&lt;code&gt;npm install bcryptjs&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Also install the types&lt;br&gt;
&lt;code&gt;npm install @types/bcryptjs --save-dev&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%2Fibsfj6f6eeb3mcobv8se.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%2Fibsfj6f6eeb3mcobv8se.png" alt="Image description" width="800" height="254"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code above:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;import bcrypt&lt;/li&gt;
&lt;li&gt;after checking the email, &lt;/li&gt;
&lt;li&gt;ln 50: we declares a constant variable named &lt;code&gt;hashedPassword&lt;/code&gt; to store the hashed password result. Then we call the &lt;code&gt;hash&lt;/code&gt; function from the &lt;code&gt;bcrypt&lt;/code&gt; library (which takes in a password and a salt value) and await its completion. &lt;/li&gt;
&lt;li&gt;incase you're wondering what a salt value is: Bcrypt automatically generates a unique salt for each password, making it extremely difficult to crack even if multiple users have the same password.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that the password is secure, let's save the user.&lt;/p&gt;

&lt;h1&gt;
  
  
  4. Save the user to the DB
&lt;/h1&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%2Fcvh7gxfdy99ueq6q668t.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%2Fcvh7gxfdy99ueq6q668t.png" alt="Image description" width="800" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ln 54: we declare a constant variable named &lt;code&gt;newUser&lt;/code&gt; to store the newly created user object. Then calls the create function on the User model we imported&lt;/li&gt;
&lt;li&gt;pass in  the provided email and hashed password.&lt;/li&gt;
&lt;li&gt;waits for the user creation to complete before proceeding.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This will successfully create a new user record in the database.&lt;/p&gt;

&lt;h4&gt;
  
  
  Testing the code...
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Registration successful 👇&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%2Fab111eb7rkljfso50go6.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%2Fab111eb7rkljfso50go6.png" alt="Image description" width="800" height="298"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;new user saved to the DB with a hashed password.👇&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%2F6jfmnt8ruicj8w9ognqt.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%2F6jfmnt8ruicj8w9ognqt.png" alt="Image description" width="800" height="348"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's see what we get if we use the same email.&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%2Fb6q0380ybhwvbydzccst.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%2Fb6q0380ybhwvbydzccst.png" alt="Image description" width="800" height="340"&gt;&lt;/a&gt;&lt;br&gt;
 Ta da! We got a response that it's in use👍&lt;/p&gt;

&lt;h2&gt;
  
  
  Errors I got
&lt;/h2&gt;

&lt;h4&gt;
  
  
  ERROR 1
&lt;/h4&gt;

&lt;p&gt;I wanted to restart the server, then I got this error&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%2Fyxdgz67g2j93pkkz6j3t.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%2Fyxdgz67g2j93pkkz6j3t.png" alt="error 1" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I realized I hadn't navigated to the server directory using the cd command. What a classic case.&lt;/p&gt;

&lt;p&gt;Just in case you are about to smash your PC, please do this.&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%2Fhr7mo8g1l6qdxobwjt46.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%2Fhr7mo8g1l6qdxobwjt46.png" alt="error 1 fixed" width="800" height="398"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  ERROR 2
&lt;/h3&gt;

&lt;p&gt;Just when I thought I was done, another error popped up!&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%2Flz55rys5zlxgd2r4e0m8.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%2Flz55rys5zlxgd2r4e0m8.png" alt="error2" width="800" height="323"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To resolve a process conflict, I terminated all running Node.js processes using the task manager and then re-executed the script within VSCode.&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%2F5puyhcuemhr9gcrsgws9.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%2F5puyhcuemhr9gcrsgws9.png" alt="error2 fixed" width="761" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The journey continues in the next post, where we'll explore token generation and transmission.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Create sign-up route</title>
      <dc:creator>Afolabi Esther</dc:creator>
      <pubDate>Tue, 19 Mar 2024 00:58:48 +0000</pubDate>
      <link>https://forem.com/platinum_hestia/day-2-4-of-subscription-app-1kle</link>
      <guid>https://forem.com/platinum_hestia/day-2-4-of-subscription-app-1kle</guid>
      <description>&lt;p&gt;In the last &lt;a href="https://dev.to/hestia/day-0-1-of-subscription-app-3oln"&gt;post&lt;/a&gt;, I created the project directory and had the server running.&lt;/p&gt;

&lt;p&gt;In this post, I'll be working on the auth (sign-up route).&lt;/p&gt;

&lt;p&gt;The logic to implement in the sign-up route are: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;validate email and password&lt;/li&gt;
&lt;li&gt;check if email is not already used&lt;/li&gt;
&lt;li&gt;hash the password..(not storing the password in plain text incase the DB gets breached)&lt;/li&gt;
&lt;li&gt;save the user to the database&lt;/li&gt;
&lt;li&gt;send back token&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Make sure to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;install express validator lib package (an express middleware for validator)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;"npm install --save express-validator"&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Download and set up Postman (we'll be using this to test the API). Check out this &lt;a href="https://www.guru99.com/postman-tutorial.html" rel="noopener noreferrer"&gt;article&lt;/a&gt; to learn what postman is and how to set it up.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Creating the sign-up route
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create a routes folder inside of the src directory (this will house our routes)&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%2Fgh5q5top4w5avscsltpc.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%2Fgh5q5top4w5avscsltpc.png" alt="auth-file01" width="800" height="365"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What I did here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create a auth.ts file ( this will house the auth route)&lt;/li&gt;
&lt;li&gt;ln 1 : import express from express&lt;/li&gt;
&lt;li&gt;ln 2: get body &amp;amp; validationResult from express-validator (we'll be using these to validate user's email and password in the post request)&lt;/li&gt;
&lt;li&gt;ln 4 : get router from express and store in a variable&lt;/li&gt;
&lt;li&gt;with this router variable we can create multiple routes for this file and export it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;-ln 7 - 30: create a post request for signing up&lt;br&gt;
Breakdown:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;call the post method on the router, which takes in 2 arguments (path and request handler)&lt;/li&gt;
&lt;li&gt;in-between the path and the handler, we add what we want to validate &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  1. Validate username, email and password
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;ln 13: catch the validation error with validationResult which returns an array of errors&lt;/li&gt;
&lt;li&gt;ln 15: Check if the validation error(array) isn't empty. If this is true, throw/send back some errors as responses in JSON format.&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%2Fsk9z0qm6wfw1gaq4vblw.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%2Fsk9z0qm6wfw1gaq4vblw.png" alt="auth-file02" width="800" height="335"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ln 26 : if there's no error, send a response that the credentials are valid&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's check if this works...&lt;/p&gt;

&lt;h2&gt;
  
  
  Configure the authRoutes
&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%2Frubnsvuhfj3wxsoyrnhp.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%2Frubnsvuhfj3wxsoyrnhp.png" alt="configure route" width="800" height="431"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;in the root app (index.ts)&lt;/li&gt;
&lt;li&gt;ln 2: import authRoutes from the auth file.&lt;/li&gt;
&lt;li&gt;configure the authRoutes in the root application by utilizing app.use()&lt;/li&gt;
&lt;li&gt;specify the root path and specify all of the routes&lt;/li&gt;
&lt;li&gt;so, to get the signup route, all we need to do is to go to &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;"localhost:8080/auth/signup"&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing the API with Postman
&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%2F2tysdqygtbvkp0jgwrtl.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%2F2tysdqygtbvkp0jgwrtl.png" alt="check correct credientils" width="800" height="375"&gt;&lt;/a&gt;&lt;br&gt;
Correct credentials&lt;/p&gt;

&lt;p&gt;Right here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create a new collection&lt;/li&gt;
&lt;li&gt;create a post request&lt;/li&gt;
&lt;li&gt;input the url 
"&lt;a href="http://localhost:8000/auth/sign-up" rel="noopener noreferrer"&gt;http://localhost:8000/auth/sign-up&lt;/a&gt;"&lt;/li&gt;
&lt;li&gt;inside the body , check raw and set to JSON&lt;/li&gt;
&lt;li&gt;then put in the username, email and password&lt;/li&gt;
&lt;li&gt;click send&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What if we put in an invalid email and password? We get this response.&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%2Ffa6p43461tg0fb9qpuv0.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%2Ffa6p43461tg0fb9qpuv0.png" alt="bad reuest" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
Error response&lt;/p&gt;

&lt;p&gt;In the next post, I'll be checking if email is not already used (in this step, I'll be creating the database and connecting it to the server).&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Setting up!</title>
      <dc:creator>Afolabi Esther</dc:creator>
      <pubDate>Fri, 15 Mar 2024 11:39:29 +0000</pubDate>
      <link>https://forem.com/platinum_hestia/day-0-1-of-subscription-app-3oln</link>
      <guid>https://forem.com/platinum_hestia/day-0-1-of-subscription-app-3oln</guid>
      <description>&lt;h1&gt;
  
  
  What exactly am I building?
&lt;/h1&gt;

&lt;p&gt;A full-stack MERN application.&lt;/p&gt;

&lt;p&gt;It's a subscription app for a hair product where a user can subscribe on a monthly basis to get access to specific hair products and resources based on the plan choosen.&lt;/p&gt;

&lt;p&gt;It will consist of: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a landing page (hero page)&lt;/li&gt;
&lt;li&gt;sign-up, login &amp;amp; logout features&lt;/li&gt;
&lt;li&gt;payment session (using Stripe)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So,I'm using &lt;a href="https://www.google.com/url?sa=t&amp;amp;source=web&amp;amp;rct=j&amp;amp;opi=89978449&amp;amp;url=https://m.youtube.com/watch%3Fv%3DWxw1wNwlBbk&amp;amp;ved=2ahUKEwid-qyPnoaFAxVAREEAHeHxBZsQwqsBegQIDRAF&amp;amp;usg=AOvVaw21T1bivwH3EGKJo2pTtfNE" rel="noopener noreferrer"&gt;Laith Harb&lt;/a&gt; video as my base tutorial(Laith is a superb tutor...check his videos out) which helps me understand the core logics/functionalities. Also I'll be going through some backend codes on GitHub to see how I can improve the app later on.&lt;/p&gt;

&lt;p&gt;🔴Alert! It's a Typescript project so brace up for errors😅.&lt;/p&gt;

&lt;p&gt;Let's get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  1.Setting up the project
&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%2Fgxxj491js92wu6pdj1k4.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%2Fgxxj491js92wu6pdj1k4.png" alt="build typescript react app" width="646" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Create a directory for the project named subscription_app.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Open up the CMD prompt and move into the directory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Execute a command to create a react app using Vite. &lt;a href="https://blog.logrocket.com/build-react-typescript-app-vite/" rel="noopener noreferrer"&gt;Clara&lt;/a&gt; gave a thorough explanation here.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once react app is created, right-click on the project directory and open with vscode.&lt;/p&gt;

&lt;p&gt;I'll be working on the API first(the main focus of this documentation). The frontend won't be hard to implement since I already know how to work with React.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Building the API
&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%2Fzik9vqnk5e83qjsrkr92.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%2Fzik9vqnk5e83qjsrkr92.png" alt="create server directory" width="800" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the root of the project directory, create a server directory&lt;/li&gt;
&lt;li&gt;Open the terminal window and move into the server folder, "cd server."&lt;/li&gt;
&lt;li&gt;initialize a node application with " npm init -y"&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3.Install some dependencies.
&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%2F138gpqxov43x9lkcv55j.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%2F138gpqxov43x9lkcv55j.png" alt="Install express" width="618" height="212"&gt;&lt;/a&gt;&lt;br&gt;
"npm install express"&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;express, &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%2F2x3th9xvt81aeopfeezf.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%2F2x3th9xvt81aeopfeezf.png" alt="Install types" width="800" height="491"&gt;&lt;/a&gt;&lt;br&gt;
"npm install @types/express @types/node typescript ts-node-dev --save-dev"&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;types for express &amp;amp; node&lt;/li&gt;
&lt;li&gt;typescript, and &lt;/li&gt;
&lt;li&gt;ts-node-dev (this package allows us to run typescript application inside of development),&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;" npm install nodemon -g"&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;install nodemon globally (it hot reloads our application)&lt;/li&gt;
&lt;li&gt;run this command "tsc -init" to initialize that it's a typescript project&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%2Fjscrnqq59ne74sk8d90b.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%2Fjscrnqq59ne74sk8d90b.png" alt="Initialize typescript" width="800" height="179"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Getting the app running
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create a src directory ( where our code will live)&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then, create an index.ts file ( this will be the root of our express app)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;in the index.ts file&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%2Fwa0racfg9pp7x6l0q3q6.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%2Fwa0racfg9pp7x6l0q3q6.png" alt="create src folder" width="800" height="387"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;import express&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;create an express app&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;create a route&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;listen at a particular port&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Don't forget to save.&lt;/p&gt;

&lt;h3&gt;
  
  
  Next...
&lt;/h3&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%2F11jpdq6wfj0ni02h1r7k.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%2F11jpdq6wfj0ni02h1r7k.png" alt="create new script to run" width="800" height="244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;in the package.json&lt;/li&gt;
&lt;li&gt;create a new script "start:dev": "nodemon --watch './*&lt;em&gt;/&lt;/em&gt;.ts' --exec ts-node src/index.ts"&lt;/li&gt;
&lt;li&gt;this call nodemon to watch for changes in "any typescript file that's in any directory that has the extension .ts". If there's any changes nodemon should execute ts-node and rerun the root application.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  So let's run the script (server)...
&lt;/h3&gt;

&lt;p&gt;"npm run start:dev"&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%2Fpjcbb52wh0d2lfasykxd.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%2Fpjcbb52wh0d2lfasykxd.png" alt="running the script" width="800" height="555"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Also run the url in the browser
&lt;/h3&gt;

&lt;p&gt;"localhost:8080." &lt;/p&gt;

&lt;p&gt;It should display your response.&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%2Fgefwv2zjzdqub7dyy4sj.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%2Fgefwv2zjzdqub7dyy4sj.png" alt="Image description" width="800" height="313"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have the server up and running!&lt;/p&gt;

&lt;p&gt;In the next post, I'll be working on the auth(sign-up route)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Freewriting Tech Blog</title>
      <dc:creator>Afolabi Esther</dc:creator>
      <pubDate>Mon, 11 Mar 2024 17:39:54 +0000</pubDate>
      <link>https://forem.com/platinum_hestia/freewriting-tech-blog-1ia7</link>
      <guid>https://forem.com/platinum_hestia/freewriting-tech-blog-1ia7</guid>
      <description>&lt;p&gt;Hi everyone!&lt;br&gt;
Welcome to my page.&lt;/p&gt;

&lt;p&gt;I'm starting this "freewriting" blog to document my learning, share things I'll be building, and also to be seen by anyone who would find it useful.&lt;/p&gt;

&lt;p&gt;As I've mentioned, it's a "freewriting" blog (with some humor sprinkled), not a perfect technical blog. So I'm open to feedback from more experienced developers and writers.&lt;/p&gt;

&lt;p&gt;I'm inspired by &lt;a href="https://medium.com/@tamrat/blogging-driven-learning-8e848bc6d2fe" rel="noopener noreferrer"&gt;Tamrat&lt;/a&gt; on Medium platform. The approach he took was to learn something then explain it to himself in his own words.&lt;/p&gt;

&lt;p&gt;I'm a frontend developer with 2 years of experience exploring the backend world. I'm so curious to see how things work over "there."&lt;/p&gt;

&lt;p&gt;So, as I'm building, I'll be sharing my thought process, explaining concepts, challenges, issues, and solutions to problems I encounter. &lt;/p&gt;

&lt;p&gt;To get started, I'll be challenging myself to build a full-stack subscription app using React, Typescript, Expressjs, and MongoDB. It's my first time building this, though I'm familiar with the frontend stack (Reactjs). I'm eager to learn new things to sharpen my skills.&lt;/p&gt;

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