Creating a RESTFul API in Flask With JSON Web Token Authentication and Flask-SQLAlchemy

Pretty Printed · 219K views · 2017 · 48:37

Open on YouTube

Web developmentPython, Ruby & scripting languagestutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it as a reference implementation when they are setting up JWT login, protected routes and token decorators in Flask apps.

Recommended by 1 people across 1 different months, 2019 to 2022.

What it is

A 48-minute tutorial from the Pretty Printed channel that builds a todo list REST API in Flask, using Flask-SQLAlchemy for storage and JSON Web Tokens for authentication. It walks through the code step by step, including the token-required decorator pattern.

What people said

Recommends helpjpls · 2021-06 · Stack Overflow · on “How to pass jwt token from one route to another?”

could you explain what that entails please? the protected page simply returns a json message for now. (i was trying to set up something similar to this tutorial: [this video] ).

Cites Reinhard Jonathan Silalahi · 2019-04 · Stack Overflow · on “Error 401 in Swagger UI for Flask Restplus API”

'Bearer Auth' must be replaced with just 'apikey' see these links : [LINK] [this video] How to implement login required decorator in Flask [LINK]

Cites Mike D Hovhannisyan · 2022-09 · Stack Overflow · on “JWT generated token is invalid”

Here is the Flask app import jwt from datetime import datetime, timedelta from flask import Flask, request, jsonify, make_response from flask_socketio import SocketIO, send from flask_sqlalchemy import SQLAlchemy from werkzeug.security import generate_password_hash, check_password_hash from functools import wraps app = Flask(__name__) app.config['SECRET_KEY'] = 'myrandomsecretkey' print(app.config['SECRET_KEY']) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db' db = SQLAlchemy(app) def token_required(f): @wraps(f) def decorated(*args, **kwargs): token = None if 'x-access-token' in request.headers: token = request.headers['x-access-token'] if not token: return jsonify({'message': 'T