Skip to content

Instantly share code, notes, and snippets.

@rogerwschmidt
Last active November 27, 2018 22:50
Show Gist options
  • Select an option

  • Save rogerwschmidt/1a003e5168aa6a5f14e9a1f74e30c1b0 to your computer and use it in GitHub Desktop.

Select an option

Save rogerwschmidt/1a003e5168aa6a5f14e9a1f74e30c1b0 to your computer and use it in GitHub Desktop.

Authentication and Authorization Part 1 Instructor Notes

Objectives

  • Walk through the process of creating a user
  • Create a user
  • Explain what Authentication is
  • Walk through the process of authenticating (logging in) a user
  • Explain how to use .env
  • Login a user

Resources

Helpful commands

psql auth_example_dev -c 'SELECT * FROM users;'

Walk through the process of creating a user

  • What path does the code take when running the following command
http POST :3000/users username="student" password="student"
  • What is in the body of the response?
  • What gets stored in the database while creating a user?
  • What happens if you try to create a user with a duplicate user name?
  • What new libraries are used that you have not seen before?
    • What are those libraries doing?

Create a user

  • Switch branches git checkout user_model_create_removed
  • Complete the model so that a user can be created.
  • The following command
http POST :3000/users username="student" password="student"

Should:

  • Make sure that the user does not exists in the database?
  • Hash the password
  • Return the new user without the hashed password

Make sure to commit your changes

Explain what Authentication is

  • With your table, create a definition of what authentication is. Be prepared to share your definition.
  • Why would Authentication be useful in a web application?

Walk through the process of authenticating (logging in) a user

  • On your master branch
  • What path does the code take when running the following command
http POST :3000/auth/toke username="student" password="student"
  • What is in the body of the response?
  • What happens if the user does not exists?
  • What happens when when the password does match?
  • What happens when when the password does not match?
  • What is process.env.SECRET used for?
  • What new libraries are used that you have not seen before?
    • What are those libraries doing?

Explain how to use .env

  • What is .env is used for?
  • What package loads the contents of the .env into process.env?

Login a user

  • Switch branches git checkout auth_model_login_removed
  • Complete the controller and model for auth
  • The following command
http POST :3000/auth/token username="student" password="student"

Should

  • Return an appropriate error
  • Return a token when authentication is successful

Commit all your changes

Notes - Install modules

bcrypt-as-promised

Hashing algorithm

npm install --python=python2.7 bcrypt-as-promised

jsonwebtoken

Token creation and validation library

npm instal jsonwebtoken

dotenv

Environement management tool

npm install --save-dev dotenv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment