aboutsummaryrefslogtreecommitdiff
path: root/app/views/auth.py
diff options
context:
space:
mode:
authorNavan Chauhan <navanchauhan@gmail.com>2021-06-05 17:34:08 +0530
committerNavan Chauhan <navanchauhan@gmail.com>2021-06-05 17:34:08 +0530
commitd656c5601e9bf01e9d8c8fe43c867ec4044d9e69 (patch)
tree6ff3a9ec6fd1041f1cb77912a967a7430c2302d8 /app/views/auth.py
parent759a59d3066abcbfe8f2523044b4477a02090c03 (diff)
added sign in with google
Diffstat (limited to 'app/views/auth.py')
-rw-r--r--app/views/auth.py41
1 files changed, 39 insertions, 2 deletions
diff --git a/app/views/auth.py b/app/views/auth.py
index eb9a46a..983380e 100644
--- a/app/views/auth.py
+++ b/app/views/auth.py
@@ -1,11 +1,10 @@
-from app import app, db, models, login_manager
+from app import app, db, models, login_manager, oauth
from app.forms.app_forms import UserSignUp, UserLogIn
from flask import render_template, flash,url_for, redirect
from app.misc_func import flash_errors
import flask_login
from sqlalchemy.exc import IntegrityError
-
@app.route("/signup", methods=['GET', 'POST'])
def register_user():
form = UserSignUp()
@@ -45,6 +44,44 @@ def signin_user():
flash_errors(form)
return render_template("auth/signin.html",form=form)
+@app.route("/login/with/google")
+def login_with_google():
+ redirect_uri = url_for("login_with_google_auth", _external=True)
+ return oauth.google.authorize_redirect(redirect_uri)
+
+
+@app.route("/login/with/google/callback")
+def login_with_google_auth():
+ token = oauth.google.authorize_access_token()
+ g_user = oauth.google.parse_id_token(token)
+ print(g_user)
+ if g_user["email_verified"]:
+ user = models.User(
+ first_name=g_user["given_name"],
+ last_name=g_user["family_name"],
+ email=g_user["email"],
+ confirmation=True,
+ login_type="google")
+ db.session.add(user)
+ try:
+ db.session.commit()
+ flask_login.login_user(user)
+ return redirect(url_for("user_dashboard"))
+ except IntegrityError:
+ db.session.rollback()
+ user = models.User.query.filter_by(email=g_user["email"]).first()
+ if user.login_type == "google":
+ flask_login.login_user(user)
+ return redirect(url_for("user_dashboard"))
+ else:
+ flash("An account already exists for this email. Please use your password to log in.")
+ return redirect(url_for("signin_user"))
+ else:
+ return render_template(
+ "message.html",
+ message="To use sign-in with Google, you need a verified e-mail.",
+ )
+
@app.route("/dashboard")
@flask_login.login_required
def user_dashboard():