Micro Services Classroom Series – 28/Nov/2021

Authentication for API

  • To implement the authentication we need to have users in our database
  • So we need to create the user model
  • Lets define user model
    • id: identity of the user
    • username: The username of the user
      • max_length => 80 characters
      • nullable => false
      • unique
    • email: The user’s email
      • max_length => 256 characters
      • nullable => false
      • unique
    • password: The user’s password
      • max_length => 256 characters
    • is_deleted: This is to indicate if the user is active or deleted
      • type: boolean
      • default: False
    • recipes: This doesnot create a field in database. This is a relationship with the recipe model
    • create_at: creation time of the user
    • update_at: The last update time of the user
  • There will be relation between user and recipe model Preview
    • user_id is foreign key for recipe
    • User table will have multiple recipes
  • In the recipe model we need to add the user_id
  • To represent these relationships, Refer Here
  • Refer Here to view the user model added
  • Lets implement reusable methods Refer Here

Password Hashing

  • Hashing is one-way mathematical function, It requires little computing power to convert a plain text to its hash value. Preview
  • With this properyt it is perfect for us to store the hash of the password in the db
  • Next time when user tries to login we hash the entered password and compare it with the database record
  • For password hashing we will be using passlib module of python
  • Refer Here for the changes done

Implement the User Registration Feature

  • For User Registration we need to create a Resource For users
  • Define API Methods
URI Method Description
/users POST Creates a new user
  • Lets create a User Schema

  • Now lets create a UserList Resource

  • Refer Here for the changes

  • Note: We have two problems to be fixed

    • Users and Recipes in models if they should be in different files but that is not getting detected.
    • The following issue occured when create the user in database.
    sqlalchemy.exc.ArgumentError
    

sqlalchemy.exc.ArgumentError: Error creating backref ‘user’ on relationship ‘User.recipes’: property of that name exists on mapper ‘mapped class Recipe->recipe’

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin