Created
April 1, 2022 12:25
-
-
Save anandtripathi5/e2d242ad5177f61bc4a1a15a03c9c28d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, jsonify | |
from flask_jwt_extended import create_access_token | |
from flask_jwt_extended import get_jwt_identity | |
from flask_jwt_extended import jwt_required | |
from flask_jwt_extended import JWTManager | |
app = Flask('__name__') | |
# Setup the Flask-JWT-Extended extension | |
app.config["JWT_SECRET_KEY"] = "super-secret" # Change this! | |
jwt = JWTManager(app) | |
# Create a route to authenticate your users and return JWTs. The | |
# create_access_token() function is used to actually generate the JWT. | |
@app.route("/login", methods=["POST"]) | |
def login(): | |
username = request.json and request.json.get("username", None) | |
password = request.json and request.json.get("password", None) | |
if username != "test" or password != "test": | |
return jsonify({"msg": "Bad username or password"}), 401 | |
access_token = create_access_token(identity=username) | |
return jsonify(access_token=access_token) | |
@app.get('/hello') | |
def hello(): | |
return 'This is the awesome hello api request' | |
@app.get('/world') | |
def world(): | |
return 'This is the awesome world api request' | |
@app.get('/square/<int:number>') | |
@jwt_required() | |
def square(number): | |
# Access the identity of the current user with get_jwt_identity | |
current_user = get_jwt_identity() | |
return f'User: {current_user}, Square of given number is {number * number}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment