In this lab, we'll continue working on the blog site, and add some features that only logged in users have access to.
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 from the root project directory with:
npm start --prefix client
You don't have to make any changes to the React code to get this lab working.
Now that we've got the basic login feature working, we need to reward our logged in users with some bonus content that only users who have logged in will be able to access.
We added a new attribute to our articles, is_member_only
, to reflect whether
the article should only be available to authorized users of the site. We also
created two new views: MemberOnlyIndex
and MemberOnlyArticle
.
Your goal is to add the following functionality to the new views:
- If a user is not signed in, the
get()
methods in each view should return a status code of 401 unauthorized, along with an error message. - If the user is signed in, the
get()
methods in each view should return the JSON data for the members-only articles and the members-only article by ID, respectively.
Use the session
to find if the user is logged in or not.
If a user is not signed in, the get()
methods in each view should return a
status code of 401 unauthorized, along with an error message.
If a user is not signed in, the get()
methods in each view should return a
status code of 401 unauthorized, along with an error message.
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.