pvanand commited on
Commit
82b0193
·
verified ·
1 Parent(s): 33839fe

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +98 -0
main.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from flask import Flask, render_template, request, jsonify, make_response
3
+ from flask_cors import CORS
4
+ from supabase import create_client
5
+ from jose import jwt
6
+ import os
7
+ from datetime import datetime, timedelta
8
+ import logging
9
+
10
+ app = Flask(__name__)
11
+ CORS(app)
12
+
13
+ # Set up logging
14
+ logging.basicConfig(level=logging.DEBUG)
15
+
16
+ # Configuration variables (use environment variables in production)
17
+ SUPABASE_URL = os.getenv("SUPABASE_URL")
18
+ SUPABASE_KEY = os.getenv("SUPABASE_KEY")
19
+ JWT_SECRET = os.getenv("JWT_SECRET")
20
+
21
+ # URL variables
22
+ APP_URL = 'https://www.app.com'
23
+ WAITLIST_URL = 'https://www.waitlist.com'
24
+ LOGIN_URL = 'https://www.login.com'
25
+
26
+ # Initialize Supabase client
27
+ supabase = create_client(SUPABASE_URL, SUPABASE_KEY)
28
+
29
+ def create_jwt(payload):
30
+ exp = datetime.utcnow() + timedelta(hours=24)
31
+ return jwt.encode(payload, JWT_SECRET, algorithm='HS256', headers={'exp': exp})
32
+
33
+ def verify_jwt(token):
34
+ try:
35
+ return jwt.decode(token, JWT_SECRET, algorithms=['HS256'])
36
+ except:
37
+ return None
38
+
39
+ @app.route('/')
40
+ def index():
41
+ app.logger.debug("Rendering index.html")
42
+ return render_template('index.html')
43
+
44
+ @app.route('/check_status', methods=['POST'])
45
+ def check_status():
46
+ app.logger.debug("Received request to /check_status")
47
+ token = request.json.get('token')
48
+ app.logger.debug(f"Received token: {token}")
49
+
50
+ if not token:
51
+ app.logger.debug("No token provided, redirecting to login")
52
+ return jsonify({'url': LOGIN_URL})
53
+
54
+ jwt_payload = verify_jwt(token)
55
+ if jwt_payload:
56
+ app.logger.debug(f"Valid JWT payload: {jwt_payload}")
57
+ if jwt_payload.get('authenticated') and jwt_payload.get('valid'):
58
+ return jsonify({'url': APP_URL})
59
+ elif jwt_payload.get('authenticated') and not jwt_payload.get('valid'):
60
+ return jsonify({'url': WAITLIST_URL})
61
+ else:
62
+ return jsonify({'url': LOGIN_URL})
63
+
64
+ try:
65
+ user_email = jwt.decode(token, options={"verify_signature": False})['email']
66
+ app.logger.debug(f"Decoded email from token: {user_email}")
67
+ except:
68
+ app.logger.debug("Failed to decode email from token")
69
+ return jsonify({'url': LOGIN_URL})
70
+
71
+ if not user_email:
72
+ app.logger.debug("No email in token")
73
+ return jsonify({'url': LOGIN_URL})
74
+
75
+ app.logger.debug("Checking email in Supabase")
76
+ response = supabase.table('email_allowlist').select('email').eq('email', user_email).execute()
77
+ app.logger.debug(f"Supabase response: {response}")
78
+
79
+ user_authenticated = True
80
+ user_valid = len(response.data) > 0
81
+
82
+ new_token = create_jwt({'authenticated': user_authenticated, 'valid': user_valid})
83
+ app.logger.debug(f"Created new token: {new_token}")
84
+
85
+ if user_authenticated and user_valid:
86
+ url = APP_URL
87
+ elif user_authenticated and not user_valid:
88
+ url = WAITLIST_URL
89
+ else:
90
+ url = LOGIN_URL
91
+
92
+ app.logger.debug(f"Redirecting to: {url}")
93
+ resp = make_response(jsonify({'url': url, 'token': new_token}))
94
+ resp.set_cookie('auth_token', new_token, httponly=True, secure=True, samesite='Strict', max_age=86400)
95
+ return resp
96
+
97
+ if __name__ == '__main__':
98
+ app.run(debug=True, port=5000)