Skip to content

Instantly share code, notes, and snippets.

@erickgnavar
Created August 21, 2015 01:25
Show Gist options
  • Save erickgnavar/e453752902fe1b494c37 to your computer and use it in GitHub Desktop.
Save erickgnavar/e453752902fe1b494c37 to your computer and use it in GitHub Desktop.
# coding: utf-8
import jwt
from datetime import datetime
from flask import Flask, jsonify, request
app = Flask(__name__)
key = 'foo'
@app.before_request
def check_token():
if request.path.startswith('/api/'):
token = request.headers.get('Token', None)
if token is not None:
payload = jwt.decode(token, key)
# token has user_id, check if exists in database
else:
return jsonify(error='fua'), 401
pass
@app.route('/login/')
def login():
# check credentials in database
payload = {
'user_id': 1
}
return jsonify({
'token': jwt.encode(payload, key, algorithm='HS256')
})
@app.route('/api/products/')
def products():
data = []
return jsonify(data=data)
@app.route('/')
def home():
return 'hello'
if __name__ == '__main__':
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment