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:
- Using SQLalchemy to define your database models
- Defining a basic authorization table in your DB
- Using encryption so that your authorization tokens can't be lifted from the database
- Flushing expired tokens from the auth table
- Using pre-built methods to validate authorization such as Flask-Github's github-callback example or Flask-Login's login_required decorator
- Using flask-SQLalchemy's create_db yo build the database from your model
- 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 ...
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
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.
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.