flask: how to bridge front-end with back-end service to render api authentication?

As the comments suggest, there's no simple snippet of code anyone can share to answer this question. You're basically asking for a five-part blog on how to attach a database to a Flask app in order to authenticate API credentials. I know it doesn't seem this way, but your questions really cascade from one topic Yinto the next. I think your best bet is to look at the Flask Mega Tutorial Part IV Databases and Part V User Logins. These tutorials cover the foundational concepts your code seems to be missing, as follows:

  1. Using SQLalchemy to define your database models
  2. Defining a basic authorization table in your DB
  3. Using encryption so that your authorization tokens can't be lifted from the database
  4. Flushing expired tokens from the auth table
  5. Using pre-built methods to validate authorization such as Flask-Github's github-callback example or Flask-Login's login_required decorator
  6. Using flask-SQLalchemy's create_db yo build the database from your model
  7. Using flask-SQLalchemy's db.session to set/get data from the db

For what it's worth, I really think The Flask Mega-Tutorial would be helpful.

UPDATE: Here is a minimal example using a dictionary as a toy database. A few things about this example ...

  1. If you run main.py and go to http://127.0.0.1:5000/token?username=admin&password=somepassword you will see the working get example

  2. If you go to http://127.0.0.1:5000, click on "hello_world", click "post", and then click "try it out," you can enter a username and a password, and those will be added to the mock database.

  3. After adding a username and password, you can go to http://127.0.0.1:5000/token?username=[]&password=[] except replace brackets with that new username and password. If you shutdown the server, the usernames and passwords won't be saved since it's just updating a dictionary.

Hopefully, all this helps ... once you've edited the app like this, it should be easier to debug issues that aren't related to username and password authentication.

Tags:

Python

Api

Flask