-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
77 lines (59 loc) · 2.32 KB
/
app.py
File metadata and controls
77 lines (59 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from flask import Flask, jsonify, g, request
from flask_cors import CORS
import os
from functools import wraps
import jwt
from playhouse.shortcuts import model_to_dict
import models
from resources.rants import rants
from resources.comments import comments
from resources.users import users
#all information on jwt and creating decorators was taken from https://www.youtube.com/watch?v=WxGBoY5iNXY
def login_check(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(data={}, status={"code": 401, "message" : "Login required"})
try:
data = jwt.decode(token, 'THISISASECRETKEY')
current_user = models.Users.get(models.Users.id == data['id'])
except:
return jsonify(data={}, status={"code": 401, "message": "Token is invalid"})
return f(current_user, *args, **kwargs)
return decorated
DEBUG = True
PORT = 8000
app = Flask(__name__)
CORS(rants, origins=['http://localhost:3000', 'https://project-4-client-rantz.herokuapp.com'], supports_credentials=True)
app.register_blueprint(rants, url_prefix='/rantz')
CORS(comments, origins=['http://localhost:3000', 'https://project-4-client-rantz.herokuapp.com'], supports_credentials=True)
app.register_blueprint(comments, url_prefix='/comments')
CORS(users, origins=['http://localhost:3000', 'https://project-4-client-rantz.herokuapp.com'], supports_credentials=True)
app.register_blueprint(users, url_prefix='/users')
CORS(app, origins=['http://localhost:3000', 'https://project-4-client-rantz.herokuapp.com'], supports_credentials=True)
@app.before_request
def before_request():
"""Connect to the database before each request."""
print("you should see this before each request")
g.db = models.DATABASE
g.db.connect()
@app.after_request
def after_request(response):
"""Close db after each request"""
print("you should see this after each request")
g.db.close()
return response
@app.route('/')
@login_check
def index(current_user):
user_dict = model_to_dict(current_user)
return jsonify(data=user_dict, status={"code": 200, "message": "session is valid"})
if 'ON_HEROKU' in os.environ:
print('\non Heorku!')
models.initialize()
if __name__ == '__main__':
models.initialize()
app.run(debug=DEBUG, port=PORT)