Flask login for REST API and Web application both

128 Views Asked by At

I want to do a Flask application with both REST API and Web Application. For example, my application will have protected routes:

  • /notes - for viewing all notes of user
  • /notes/<note_id> - for viewing ceratin note

And same protected routes but in REST API:

  • /api/notes
  • /api/notes/<note_id>

Now I want to implement register and login functions. I know that for Web Application part there is library Flask-Login for that purpose. For REST API part I know I can use Flask-JWT-Extended.

But, what if I want to make AJAX request from /notes/1 to /api/notes? To load all user notes on page after page load. I will need JWT token of user to make such request. Is it always neccessary to make request to /api/login first every time? Or I can use Flask-Login somehow to work using same JWT tokens REST API part uses and store JWT-tokens somewhere after user goes to page /notes/1 in his browser, allowing JS script to use it after page loading to make AJAX requests to protected API routes?

0

There are 0 best solutions below