Skip to content

Latest commit

 

History

History
113 lines (90 loc) · 4.06 KB

README.md

File metadata and controls

113 lines (90 loc) · 4.06 KB

flask-restful-login-example

Build Status

INSTALLATION

  • 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

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

Register:

  • 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

Login:

  • 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!

Logout:

  • 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

Reset Password:

  • 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:

Example routes that require authentication

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>

Super admin requiring authentication extra example handler, list users

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" 

License

MIT

Free Software, Hell Yeah!