Skip to content

Python Flask Restful JWT Authentication with MongoDB

License

Notifications You must be signed in to change notification settings

hendrapaiton/mandalika

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MANDALIKA

Python Flask Restful JWT Authentication with MongoDB

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Prerequisites

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.

Installing

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

Testing

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":"[email protected]", "password":"enje123"}' -H "Content-Type: application/json"

CREATE API ENDPOINT

$ curl -X POST localhost:5000/api/v1/user -d '{"username":"[email protected]", "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":"[email protected]", "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}"
  1. Change ${TOKEN} with token generated by jwt when we login
  2. Change with User ID you want to update/delete (you can get id from READ API ENDPOINT)

Authors

Hendra Dwi Saputra - Initial work - hendrapaiton

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details

Acknowledgments

  1. My inspiration and More Tutorials here. Thanks to Paurakh Sharma Humagain for great explanations.
  2. Flask JWT Extended Documentation for Role-Based Access Control (RBAC)
  3. Flask Mongo Engine Documentation, specially for supported fields.

Releases

No releases published

Packages

No packages published

Languages