Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/hendrapaiton/mandalika

Python Flask Restful JWT Authentication with MongoDB
https://github.com/hendrapaiton/mandalika

flask flask-jwt-extended flask-restful jwt mongodb python unittest

Last synced: 6 days ago
JSON representation

Python Flask Restful JWT Authentication with MongoDB

Awesome Lists containing this project

README

        

# 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](https://www.python.org/downloads/) and choose according to your operating system.
n addition you also need a MongoDB NoSQL database and you can download
[here](https://www.mongodb.com/download-center/community).

### Installing

First, clone this project from github using git command or git gui application like [fork](https://git-fork.com/).
```
$ 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](https://pypi.org/project/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/ -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/ -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](https://github.com/hendrapaiton)

See also the list of [contributors](https://github.com/your/project/contributors) who participated in this project.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details

## Acknowledgments

1. My inspiration and More Tutorials [here](https://dev.to/paurakhsharma/flask-rest-api-part-0-setup-basic-crud-api-4650).
Thanks to [Paurakh Sharma Humagain](https://dev.to/paurakhsharma) for great explanations.
2. Flask JWT Extended [Documentation](https://flask-jwt-extended.readthedocs.io/en/stable/tokens_from_complex_object/)
for _Role-Based Access Control_ (RBAC)
3. Flask Mongo Engine [Documentation](http://docs.mongoengine.org/projects/flask-mongoengine/en/latest/),
specially for _supported fields_.