Git Product home page Git Product logo

task-express-auth-signup-sp's Introduction

If you need a starting point fork this: Starting point.

User Model and Routes

  1. Create a model called User that has the following fields:
    1. username which is a String, not optional, and unique.
    2. password which is a String and not optional.
    3. email which is a String and must be an email.
    4. firstName which is a String.
    5. lastName which is a String.
  2. Create a user folder, and inside it a users.controllers and a users.routes files and connect it the routes to app.js.

Signup Route

  1. In users.controllers, create a method called signup.
  2. Install bcrypt and require it in users.controllers.
  3. Hash the password with 10 salt rounds and overwrite req.body.password with the new, hashed password.
  4. Pass the body of the request to User.create.
  5. Change the response status to 201 and end it with a message.
  6. Don't forget to create your route in users.routes.js and give it the path /apis/signup.

Config Folder

  1. Create a new folder called config.
  2. In this folder create a file called keys.js.
  3. Create an object that has two properties:
    1. JWT_SECRET: give it a secret key.
    2. JWT_EXPIRATION_MS: give it the time for your token expiration in milliseconds.
  4. Export this object.

Generating a Token

Generate a token in users.controllers's signup method.

  1. Require JWT_EXPIRATION_MS and JWT_SECRET from config/keys.js.
  2. Install jsonwebtoken.
  3. Require jwt from jsonwebtoken.
  4. In users.controllers, create a function called generateToken that takes user as an argument.
  5. In this function, create an object called payload and pass it the user's username that's coming from user.
  6. Keep in mind that the token must have the user's ID and the expiration date of the token.
  7. Add an exp property to payload and its value is the date right now plus JWT_EXPIRATION_MS.
  8. After creating your payload object, call jwt.sign() and pass it two arguments:
    1. payload, make sure to stringify it.
    2. JWT_SECRET
  9. Save the returned value in a variable called token and return token.
  10. In the signup method, after creating the user call generateToken and pss it the new user.
  11. Save the returned value in a variable called token and send it as a response.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.