项目作者: hendrapaiton

项目描述 :
Python Flask Restful JWT Authentication with MongoDB
高级语言: Python
项目地址: git://github.com/hendrapaiton/mandalika.git
创建时间: 2020-02-17T16:20:54Z
项目社区:https://github.com/hendrapaiton/mandalika

开源协议:MIT License

下载


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.

  1. $ git clone https://github.com/hendrapaiton/mandalika.git

Making environment for project to isolation python installing libraries for this project only.

  1. $ pip install virtualenv
  2. $ virtualenv venv
  3. $ source venv/bin/activate

Installing all libraries needed by this project using pip.

  1. $ pip install -r requirements.txt

Make a configuration file with name .env with this configuration (change as desired).

  1. MONGODB_SETTINGS = { 'host': 'mongodb://localhost/mandalika' }
  2. JWT_SECRET_KEY = 'senggigibeach'

Setting the environment for this project.

  1. $ export FLASK_APP=app.py
  2. $ export ENV_FILE_LOCATION=./.env

Running the project.

  1. 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.

  1. $ curl -X POST localhost:5000/api/v1/login -d '{"username":"admin@nj.net", "password":"enje123"}' -H "Content-Type: application/json"

CREATE API ENDPOINT

  1. $ 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

  1. $ curl localhost:5000/api/v1/ -H "Content-Type: application/json" -H "Authorization: Bearer ${TOKEN}"

UPDATE API ENDPOINT

  1. $ 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

  1. $ 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.