Python Flask Restful JWT Authentication with MongoDB
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
Before you can run this project you need a install python first on your operating system. You can download python here and choose according to your operating system. n addition you also need a MongoDB NoSQL database and you can download here.
First, clone this project from github using git command or git gui application like fork.
$ git clone https://github.com/hendrapaiton/mandalika.git
Making environment for project to isolation python installing libraries for this project only.
$ pip install virtualenv
$ virtualenv venv
$ source venv/bin/activate
Installing all libraries needed by this project using pip.
$ pip install -r requirements.txt
Make a configuration file with name .env with this configuration (change as desired).
MONGODB_SETTINGS = { 'host': 'mongodb://localhost/mandalika' }
JWT_SECRET_KEY = 'senggigibeach'
Setting the environment for this project.
$ export FLASK_APP=app.py
$ export ENV_FILE_LOCATION=./.env
Running the project.
flask run
To test crud api endpoint that has been created you can use curl utility. Before test, you must login first to get jwt token and using it in every request header you sent.
$ curl -X POST localhost:5000/api/v1/login -d '{"username":"admin@nj.net", "password":"enje123"}' -H "Content-Type: application/json"
CREATE API ENDPOINT
$ curl -X POST localhost:5000/api/v1/user -d '{"username":"user1@nj.net", "password":"user1"}' -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}"
READ API ENDPOINT
$ curl localhost:5000/api/v1/ -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}"
UPDATE API ENDPOINT
$ curl -X PUT localhost:5000/api/v1/user/<id> -d '{"username":"user01@nj.net", "password":"user01"}' -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}"
DELETE API ENDPOINT
$ curl -X DELETE localhost:5000/api/v1/user/<id> -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}"
- Change ${TOKEN} with token generated by jwt when we login
- Change with User ID you want to update/delete (you can get id from READ API ENDPOINT)
Hendra Dwi Saputra - Initial work - hendrapaiton
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE file for details
- My inspiration and More Tutorials here. Thanks to Paurakh Sharma Humagain for great explanations.
- Flask JWT Extended Documentation for Role-Based Access Control (RBAC)
- Flask Mongo Engine Documentation, specially for supported fields.