CRUD Features (Login Management Example)

Purpose: To teach about adding CRUD features to webpages generally and test it by adding login management to the code in previous exercises. Specifically, in this exercise, you will practice adding code that allows users to change their own password. You will also add a page that allows a level 3 user to change other users' passwords, change access levels, and delete users.

At this point: You already know enough to add edit and delete features to your website without further teaching. You know how to use GET and POST requests on the front and back end, and that is enough to handle the communication. You know how to use login tokens and access levels. You know how to manipulate back end data as needed, whether in a database, JSON files, or other formats. In this exercise, however, you will practice a classic API convention of using PATCH and DELETE requests for editing and deleting. There is no real reason you could not use GET and POST requests for this, but it's common convention to do so.

Required Reading: API Conventions for GET, POST, PUT, PATCH, DELETE, etc.

PUT, PATCH, and DELETE requests work exactly the same as POST requests in terms of how data is passed. They do not put data in the URL like GET requests and are not subject to URL size limits. They pass data in the HTTP "body" header like POST requests. However, there is a convention for how each request is supposed to be used, and many APIs follow these conventions. Please review the following overview (you are encouraged but not required to follow links as desired):

Reading on Classic HTTP Methods

You may notice that we are not teaching you to use CONNECT, HEAD, OPTIONS, or TRACE requests, at least not using AJAX. The Flask server responds to CONNECT requests automatically, and the response is not customizable (this is also true of WSGI servers generally). The Flask server allows HEAD requests on any route that accepts the GET method. The Flask server responds to an OPTIONS request based on what you specify in "methods" and in your Flask CORS policy. The Flask server responds to a TRACE request if you specify it in "methods", but it just echoes back the request.

PUT and PATCH requests are very similar in purpose. The main difference is that PUT requests are meant to replace a whole "resource" (a database row, a file, etc.) while PATCH requests are meant to update a part of a resource (a field, a setting, etc.). To follow this convention, we will use PATCH requests in this exercise to change passwords and access levels. We will not use PUT requests in this exercise, but we will use them in Project Part 7 to replace images.

You may wonder why we use POST requests for login, when POST requests conventionally add or create a resource, when that doesn't seem like what we are doing when we do login. The thinking goes that a login creates a resource because it creates a session or a token, and that is considered a resource. Having said all that, a much more basic reason POST is desirable is that a GET request would be bad for security. Ultimately, when you design an API professionally, function is more important than form, so it will be up to you whether to use the convention of POST/PUT/PATCH/DELETE requests or deviate from that model.

Overview and Sample Code

Please begin with this version of the app created in Exercise 6.2-6.5. Take a moment to run it and re-familiarize yourself as needed.

In the Module 6 exercises, we added login and JWT persistence. In this exercise, we will add password changes and user management. More specifically:

Add Back End Model Functions

We will continue to use the global variable "users" as a monkey patch. This makes for fairly simple functions to verify the logged in user's password, level, and identity. In your project, you can adapt ideas from the functions below to work with your database, password hashing, and different user structure.

model_login.py

Import all these functions into app.py. The import should now include:

app.py

Add Back End Routes

In app.py, set up back end routes to edit and delete user data, as shown below.

app.py

Take note of the following features, which are intentional:

Front End: Enable PATCH Requests in api.js

In api.js, modify the apiSubmit function to add a "method" parameter, so we can send PATCH and DELETE requests as well as POST requests. Also, make sure it uses addAuthHeader to ensure the login token is included. The new apiSubmit should look as follows:

api.js

Front End: Add Components to Edit and Delete User Info

Create an EditPassword component with an AJAX form to change password. Create an EditUserAdmin component with an AJAX form to delete a user and change access level. Note that in EditPassword component, the requirement to enter the password twice is a client-side-only check, since its purpose is to control for human error, not machine security.

EditPassword.jsx
EditUserAdmin.jsx

Front End: Put Together Pages, Routes, and Nav

The front end now needs to a little bit more user information from local storage in its shared state. In AuthContext.jsx, add "user" to the "value" attributes of the AuthContext provider, as follows:

AuthContext.jsx

Create page components for EditProfile and AdminUsers. For a user of level less than 3, EditProfile lets that user change password only. For a user of level 3 or above, EditProfile lets them to change password and level and delete user, and AdminUsers displays all users in this way.

EditProfile.jsx
AdminUsers.jsx

Finally, make the needed changes in App.jsx and Nav.jsx so these routes are accessible and appear on the Nav for the right access levels:

App.jsx
Nav.jsx