IqraFatima commited on
Commit
fcd3fd8
Β·
verified Β·
1 Parent(s): 9a8c8e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py CHANGED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # βœ… app.py for Google OAuth Login in Colab
2
+
3
+ from flask import Flask, redirect, url_for, session
4
+ from authlib.integrations.flask_client import OAuth
5
+ from flask_ngrok import run_with_ngrok
6
+ import os
7
+
8
+ # πŸ” Secure secrets from Colab
9
+ from google.colab import userdata
10
+ CLIENT_ID = userdata.get("GOOGLE_CLIENT_ID")
11
+ CLIENT_SECRET = userdata.get("GOOGLE_CLIENT_SECRET")
12
+
13
+ # πŸ”§ Flask App Setup
14
+ app = Flask(__name__)
15
+ app.secret_key = os.urandom(24)
16
+ run_with_ngrok(app) # Auto-start ngrok in Colab
17
+
18
+ # 🌐 OAuth2 Config
19
+ oauth = OAuth(app)
20
+ google = oauth.register(
21
+ name='google',
22
+ client_id=CLIENT_ID,
23
+ client_secret=CLIENT_SECRET,
24
+ access_token_url='https://oauth2.googleapis.com/token',
25
+ authorize_url='https://accounts.google.com/o/oauth2/auth',
26
+ api_base_url='https://www.googleapis.com/oauth2/v1/',
27
+ userinfo_endpoint='https://openidconnect.googleapis.com/v1/userinfo',
28
+ client_kwargs={'scope': 'openid email profile'},
29
+ )
30
+
31
+ # πŸ“ Routes
32
+ @app.route('/')
33
+ def home():
34
+ email = session.get('email', None)
35
+ if email:
36
+ return f"<h2>βœ… Logged in as: {email}</h2><br><a href='/logout'>Logout</a>"
37
+ return "<a href='/login'>πŸ” Login with Google</a>"
38
+
39
+ @app.route('/login')
40
+ def login():
41
+ redirect_uri = url_for('auth_callback', _external=True)
42
+ return google.authorize_redirect(redirect_uri)
43
+
44
+ @app.route('/auth_callback')
45
+ def auth_callback():
46
+ token = google.authorize_access_token()
47
+ user_info = google.get('userinfo').json()
48
+ session['email'] = user_info['email']
49
+ return redirect('/')
50
+
51
+ @app.route('/logout')
52
+ def logout():
53
+ session.clear()
54
+ return redirect('/')
55
+
56
+ # πŸš€ Run App
57
+ app.run()