- Python 3 is required. There are ways to send requests to server.
- Postman, Insomnia, cURL, httpie and curl are simple and useful tools to send requests.
- I mostly prefer httpie and curl. Their usage can be seen below.
Pull project and install requirements to virtual environment (https://pypi.org/project/virtualenv/). Then run.
$ git clone https://github.com/melihcolpan/flask-restful-login
$ cd flask-restful-login
$ virtualenv venv
$ source venv/bin/activate
$ pip install -r requirements.txt
$ python -m main
- For requests using httpie: https://httpie.io/
- For requests using curl: https://curl.haxx.se/download.html
Example user, admin and super admin users are created in database initializer class. You can use these users to login, logout and data handlers. For register handler, use new user information, otherwise returns already exist user.
Test Users | Email Address | Password |
---|---|---|
User | [email protected] | test_password |
Admin | [email protected] | admin_password |
Super Admin | [email protected] | sa_password |
- HTTPIE Request:
http POST :5000/v1/auth/register username=example_username password=example_password [email protected]
- Curl Request:
curl -H "Content-Type: application/json" --data '{"username":"example_name","password":"example_password", "email":"[email protected]"}' http://localhost:5000/v1/auth/register
- HTTPIE Request:
http POST :5000/v1/auth/login [email protected] password=example_password
- Curl Request:
curl -H "Content-Type: application/json" --data '{"email":"[email protected]", "password":"example_password"}' http://localhost:5000/v1/auth/login
Response: Got access token and refresh token!
- HTTPIE Request:
http POST :5000/v1/auth/logout Authorization:"Bearer ACCESS_TOKEN" refresh_token=REFRESH_TOKEN
- Curl Request:
curl -H "Content-Type: application/json" -H "Authorization: Bearer ACCESS_TOKEN" --data '{"refresh_token":"REFRESH_TOKEN"}' http://localhost:5000/v1/auth/logout
- HTTPIE Request:
http POST :5000/v1/auth/password_reset Authorization:"Bearer ACCESS_TOKEN" old_pass=<OLD-PASSWORD> new_pass=<NEW-PASSWORD>
- Curl Request:
curl -H "Content-Type: application/json" -H "Authorization: Bearer ACCESS_TOKEN" --data '{"old_pass":"OLD-PASSWORD", "new_pass":"NEW-PASSWORD"}' http://localhost:5000/v1/auth/password_reset
There are some example routes in UserHandlers file. These handlers mostly return only text. To use them:
Route addresses according to user privileges
User Type | Route Address |
---|---|
User | /data_user |
Admin | /data_admin |
Super Admin | /data_super_admin |
- HTTPIE Request:
http GET :5000/<ROUTE-ADDRESS> Authorization:"Bearer ACCESS_TOKEN"
- Curl Request:
curl -H "Content-Type: application/json" -H "Authorization: Bearer ACCESS_TOKEN" http://localhost:5000/<ROUTE-ADDRESS>
This handler searches username, email or creation dates (range) in users table and returns information these users to super admin.
- HTTPIE Request:
http GET :5000/users Authorization:"Bearer ACCESS_TOKEN" usernames==test_username,admin_username [email protected],[email protected] start_date==01.01.1990 end_date==01.01.2050
- Curl Request:
curl -X GET 'localhost:5000/users?usernames=test_username,admin_username&[email protected],[email protected]&start_date=01.01.1990&end_date=01.01.2050' -H "Content-Type: application/json" -H "Authorization: Bearer ACCESS_TOKEN"
MIT
Free Software, Hell Yeah!