Vinayak221 commited on
Commit
3fdffaf
·
verified ·
1 Parent(s): a46cc14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -60
app.py CHANGED
@@ -1,64 +1,37 @@
1
- from shiny import App, render, ui
2
- from flask import Flask, redirect, url_for, session
3
- from authlib.integrations.flask_client import OAuth
4
-
5
- app = Flask(__name__)
6
- app.secret_key = 'your_secret_key'
7
-
8
- oauth = OAuth(app)
9
- google = oauth.register(
10
- name='google',
11
- client_id='your_client_id',
12
- client_secret='your_client_secret',
13
- authorize_url='https://accounts.google.com/o/oauth2/auth',
14
- access_token_url='https://accounts.google.com/o/oauth2/token',
15
- access_token_params=None,
16
- refresh_token_url=None,
17
- client_kwargs={'scope': 'openid email profile'},
 
 
 
 
 
 
18
  )
19
 
20
- @app.route('/login')
21
- def login():
22
- redirect_uri = url_for('authorize', _external=True)
23
- return google.authorize_redirect(redirect_uri)
24
-
25
- @app.route('/authorize')
26
- def authorize():
27
- token = google.authorize_access_token()
28
- user = google.parse_id_token(token)
29
- session['user'] = user
30
- return redirect('/')
31
-
32
- @app.route('/logout')
33
- def logout():
34
- del session['user']
35
- return redirect('/')
36
 
37
- @app.route('/')
38
- def index():
39
- user = session.get('user')
40
- if user:
41
- username = user['name']
42
- else:
43
- username = "Guest"
44
- return f'Hello, {username}!'
45
-
46
- @app.route('/get_username')
47
- def get_username():
48
- user = session.get('user')
49
- if user:
50
- return user['name']
51
- else:
52
- return "Guest"
53
-
54
- # Create shiny-python app
55
- shiny_app = App(
56
- ui.title("Shiny with Flask Example"),
57
- ui.page(
58
- ui.iframe("/flask", height="500px")
59
- ),
60
- render.FlexDashboard("flex_dashboard", title="Google OAuth Example"),
61
- FlaskApp(app)
62
- )
63
 
64
- shiny_app.serve()
 
 
1
+ from shiny import App, ui, reactive, render, Session
2
+ from google_auth_oauthlib.flow import InstalledAppFlow
3
+ import os
4
+
5
+ # Google OAuth Configuration
6
+ CLIENT_SECRETS_FILE = "client_secrets.json"
7
+ SCOPES = ['https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile']
8
+ os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' # Enable non-HTTPS for local testing
9
+
10
+ def authenticate_user():
11
+ """Perform user authentication via Google OAuth and return user info."""
12
+ flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES)
13
+ creds = flow.run_local_server(port=0)
14
+ user_info = creds.id_token
15
+ return user_info
16
+
17
+ app_ui = ui.page_fluid(
18
+ ui.layout_sidebar(
19
+ ui.sidebar(
20
+ ui.input_action_button("login", "Log in with Google"),
21
+ ),
22
+ ui.output_text_verbatim("username", placeholder="Username will appear here after login")
23
+ )
24
  )
25
 
26
+ def server(input: Inputs, output: Outputs, session: Session):
27
+ @output
28
+ @render.text
29
+ def username():
30
+ if input.login() == 1:
31
+ user_info = authenticate_user()
32
+ return f"Username: {user_info['email']}"
 
 
 
 
 
 
 
 
 
33
 
34
+ app = App(app_ui, server)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
+ if __name__ == "__main__":
37
+ app.run()