-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #211 from nsidc/issue-178-login-redirect
Attempt to fix login redirect behaviors
- Loading branch information
Showing
3 changed files
with
19 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,34 @@ | ||
import os | ||
|
||
from flask import Blueprint, redirect, url_for | ||
from flask_dance.contrib.google import google | ||
from flask_dance.consumer import oauth_authorized | ||
from flask_dance.contrib.google import google, make_google_blueprint | ||
from flask_login import login_user | ||
|
||
from usaon_benefit_tool.util.db.user import ensure_user_exists | ||
|
||
login_bp = Blueprint('login', __name__, url_prefix='/login') | ||
google_bp = make_google_blueprint( | ||
client_id=os.getenv('USAON_BENEFIT_TOOL_GOOGLE_CLIENT_ID'), | ||
client_secret=os.getenv('USAON_BENEFIT_TOOL_GOOGLE_CLIENT_SECRET'), | ||
scope=["profile", "email"], | ||
) | ||
|
||
|
||
@login_bp.route("") | ||
def login(): | ||
if not google.authorized: | ||
return redirect(url_for("google.login")) | ||
resp = google.get("/oauth2/v2/userinfo") | ||
assert resp.ok, resp.text | ||
|
||
user = ensure_user_exists(resp.json()) | ||
login_user(user) | ||
|
||
return redirect(url_for('root.root')) | ||
|
||
|
||
# this may be the login issue | ||
# @app.before_request | ||
# def before_request(): | ||
# """Handle expired google tokens as a pre-request hook.""" | ||
# if token := (s := session).get('google_oauth_token'): | ||
# print("Token expiring in", token['expires_at'] - time.time()) | ||
# if time.time() >= token['expires_at']: | ||
# del s['google_oauth_token'] | ||
@oauth_authorized.connect_via(google_bp) | ||
def google_logged_in(blueprint): | ||
# TODO: Flash a message? | ||
account_data = blueprint.session.get("/oauth2/v2/userinfo") | ||
|
||
user = ensure_user_exists(account_data.json()) | ||
login_user(user) | ||
|
||
return redirect(url_for('root.root')) |