-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
184 lines (146 loc) · 4.6 KB
/
app.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import string
import os
import random
#importing pyhton modules
from flask_migrate import Migrate
from flask import Flask, request, session, render_template, jsonify
from flask_cors import CORS, cross_origin
#importing Google OAuth which enables login using "Sign in with Google"
from google.oauth2 import id_token
from google.auth.transport import requests
from flask_sqlalchemy import SQLAlchemy
# serving the Flask Shell App using CORS
app = Flask(__name__, static_folder="build/static", template_folder="build")
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
# In a nutshell, hackers can use a rainbow table against your session to
# find out the secret key and parrot your app.
app.secret_key = '-' \
.join(random.choices(string.ascii_uppercase + \
string.digits, k=10))
# URI = "postgresql://postgres:admin@localhost/postgres" # or other relevant config var
uri = os.getenv("DATABASE_URL")
if uri and uri.startswith("postgres://") :
uri = uri.replace("postgres://", "postgresql://")
# Replace with your client ID later.
CLIENT_ID = os.getenv("REACT_APP_GOOGLE_CLIENT_ID")
# sqlalchemy to enable connection to the database uri
app.config["DATABASE_URL"] = uri
app.config["SQLALCHEMY_DATABASE_URI"] = uri
app.config["SQLALCHEMY_ECHO"] = False
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)
migrate = Migrate(app, db)
# database model for storing user data
class User(db.Model):
'''User Model'''
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String())
email = db.Column(db.String())
def __init__(self, idx, name, email):
self.id = idx
self.name = name
self.email = email
def __repr__(self):
return f"<id {self.id}>"
# self-serialization of the database fields
def serialize(self):
'''serialize'''
return {
'id': self.id,
'name': self.name,
'email': self.email
}
@app.route('/api/authenticated_endpoint', methods=['POST'])
@cross_origin()
def authenticated_endpoint():
'''authenticated_endpoint'''
request_data = request.get_json()
token = request_data['token']
res = {}
if 'user' in session:
user = session['user']
if token == user:
res = {
'email': user
}
else:
res = {
'error': 'token is invalid!'
}
# json_res = json.dumps(res)
json_res = jsonify(res)
return json_res
def set_token(token):
'''set_token'''
res = {}
session_user = ""
try:
# Specify the CLIENT_ID of the app that accesses the backend:
id_info = id_token.verify_oauth2_token(token, requests.Request(), CLIENT_ID)
# Create the session
session_user = id_info['email']
session["user"] = id_info['email']
res = {
"token": session_user
}
except ValueError as ex:
res = {
"error": ex
}
# Invalid token
return res
@app.route("/")
def index():
'''Main Page'''
return render_template('index.html')
@app.route("/about")
def about():
return render_template('about.html')
@app.route("/contact")
def contact():
return render_template('contact.html')
@app.route("/other")
def other():
return render_template('other.html')
@app.route("/info")
def info():
return render_template('info.html')
# Enable CORS so frontend (localhost:3000) can communicate with backend (localhost:5000)
@app.route('/api/auth', methods=['POST'])
@cross_origin()
def google_sign_in():
'''google_sign_in'''
request_data = request.get_json()
idx = random.randint(1,300)
token = request_data['token']
name = request_data['name']
email = request_data['login']
res = {}
user=User.query.filter_by(email=email).first()
# setting the token
if user:
res = set_token(token)
else :
user=User(
idx=idx,
name=name,
email=email
)
db.session.add(user)
db.session.commit()
res = set_token(token)
# convert into JSON:
# json_res = json.dumps(res)
json_res = jsonify(res)
return json_res
@app.route("/api/migrate")
def migrate_db():
"""migrate"""
db.create_all()
db.session.commit()
return "DB migration is done"
# app main
if __name__ == '__main__':
app.run(debug=True)