Skip to content

Flask restful service consists of register, login, logout, reset password and some data routes. It includes a few example routes based on user, admin, or super admin access.

License

Notifications You must be signed in to change notification settings

nerdism/flask-restful-login

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 test_email@example.com test_password
Admin admin_email@example.com admin_password
Super Admin sa_email@example.com sa_password

Register:

  • HTTPIE Request:
http POST :5000/v1/auth/register username=example_username password=example_password email=example@example.com
  • Curl Request:
curl -H "Content-Type: application/json" --data '{"username":"example_name","password":"example_password", "email":"example@example.com"}' http://localhost:5000/v1/auth/register

Login:

  • HTTPIE Request:
http POST :5000/v1/auth/login email=example@example.com password=example_password
  • Curl Request:
curl -H "Content-Type: application/json" --data '{"email":"example@example.com", "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 emails==test_email@example.com,admin_email@example.com start_date==01.01.1990 end_date==01.01.2050
  • Curl Request:
curl -X GET 'localhost:5000/users?usernames=test_username,admin_username&emails=test_email@example.com,admin_email@example.com&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!

About

Flask restful service consists of register, login, logout, reset password and some data routes. It includes a few example routes based on user, admin, or super admin access.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 80.3%
  • HTML 19.7%