In this lab, we'll continue working on the blog site from the last lab and set up a basic login feature.
There is some starter code in place for a Flask API backend and a React frontend. To get set up, run:
pipenv install && pipenv shell
npm install --prefix client
cd server
flask db upgrade
python seed.py
You can work on this lab by running the tests with pytest -x
. It will also be
helpful to see what's happening during the request/response cycle by running the
app in the browser. You can run the Flask server with:
python app.py
And you can run React in another terminal with:
npm start --prefix client
You don't have to make any changes to the React code to get this lab working.
The React frontend has already defined a proxy in package.json
as shown:
"proxy": "http://localhost:5555",
The proxy avoids CORS issues and allows the server to set a session cookie to store the user's login data.
For our basic login feature, we'll need the following functionality:
- A user can log in by providing their username in a form.
- A user can log out.
- A user can remain logged in, even after refreshing the page.
We'll need to create the resources to handle each of these features.
We'll need to build the following resources:
-
Login
is located at/login
.- It has one route,
post()
. post()
gets ausername
fromrequest
's JSON.post()
retrieves the user byusername
(we made these unique for you).post()
sets the session'suser_id
value to the user'sid
.post()
returns the user as JSON with a 200 status code.
- It has one route,
-
Logout
is located at/logout
.- It has one route,
delete()
. delete()
removes theuser_id
value from the session.delete()
returns no data and a 204 (No Content) status code.
- It has one route,
-
CheckSession
is located at/check_session
.- It has one route,
get()
. get()
retrieves theuser_id
value from the session.- If the session has a
user_id
,get()
returns the user as JSON with a 200 status code. - If the session does not have a
user_id
,get()
returns no data and a 401 (Unauthorized) status code.
- It has one route,
- /login, post()
- should handle the request to get username
- find User in database by username. We'll need the id for the next step.
- /delete, delete()
- /check_session, get()
- Session: /login post()
- set session['user_id'] to the found user's id
- Session: /logout delete()
- remove the value for session['user_id']
- Session: /check_session get()
- get current value of session['user_id'] (it may not exist)
- Session: /login post()
- return user as JSON with a 200 status code
- Session: /logout delete()
- returns no data and a 204 (No Content) status code
- Session: /check_session get()
- If the session has a
user_id
,get()
returns the user as JSON with a 200 status code. - If the session does not have a
user_id
,get()
returns no data and a 401 (Unauthorized) status code.
- If the session has a
The tests are not looking for invalid users for /login, but if you have extra time, consider what response you should send if a user is not found with the given username.
Run the test suite:
pytest
If any tests aren't passing, refine your code using each error message.
View the app in browser and test login, logout, and session persistence. Refine code if needed.
Feel free to also take a look at how the frontend logic is set up to use these endpoints.
- Commit and push your code:
git add .
git commit -m "final solution"
git push
- If you created a separate feature branch, remember to open a PR on main and merge.
Best Practice documentation steps:
- Add comments to the code to explain purpose and logic, clarifying intent and functionality of your code to other developers.
- Update README text to reflect the functionality of the application following https://makeareadme.com.
- Add screenshot of completed work included in Markdown in README.
- Delete any stale branches on GitHub
- Remove unnecessary/commented out code
- If needed, update git ignore to remove sensitive data
Before you submit your solution, you need to save your progress with git.
- Add your changes to the staging area by executing
git add .
. - Create a commit by executing
git commit -m "Your commit message"
. - Push your commits to GitHub by executing
git push origin main
.
CodeGrade will grade your lab using the same tests as are provided in the testing/
directory.