Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 7391

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 28, 20242024-11-28T03:22:09+00:00 2024-11-28T03:22:09+00:00

Subscription App (part 2)

  • 60k

In the last post, I created the project directory and had the server running.

In this post, I'll be working on the auth (sign-up route).

The logic to implement in the sign-up route are:

  • validate email and password
  • check if email is not already used
  • hash the password..(not storing the password in plain text incase the DB gets breached)
  • save the user to the database
  • send back token

Make sure to:

  • install express validator lib package (an express middleware for validator)

“npm install –save express-validator”

  • Download and set up Postman (we'll be using this to test the API). Check out this article to learn what postman is and how to set it up.

Creating the sign-up route

  • Create a routes folder inside of the src directory (this will house our routes)

auth-file01

What I did here:

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

-ln 7 – 30: create a post request for signing up
Breakdown:

  • call the post method on the router, which takes in 2 arguments (path and request handler)
  • in-between the path and the handler, we add what we want to validate

1. Validate username, email and password

  • ln 13: catch the validation error with validationResult which returns an array of errors
  • 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.

auth-file02

  • ln 26 : if there's no error, send a response that the credentials are valid

Let's check if this works…

Configure the authRoutes

configure route

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

“localhost:8080/auth/signup”

Testing the API with Postman

check correct credientils
Correct credentials

Right here:

  • create a new collection
  • create a post request
  • input the url “http://localhost:8000/auth/sign-up“
  • inside the body , check raw and set to JSON
  • then put in the username, email and password
  • click send

What if we put in an invalid email and password? We get this response.

bad reuest
Error response

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).

javascriptprogrammingtutorialwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.