From d656c5601e9bf01e9d8c8fe43c867ec4044d9e69 Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Sat, 5 Jun 2021 17:34:08 +0530 Subject: added sign in with google --- app/__init__.py | 8 ++++++ app/config_dev.py | 9 ++++++- app/templates/auth/signin.html | 58 +++++++++++++++++++++++++++++++++++++++++- app/templates/auth/signup.html | 57 +++++++++++++++++++++++++++++++++++++++++ app/views/auth.py | 41 +++++++++++++++++++++++++++-- 5 files changed, 169 insertions(+), 4 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 9f025af..b006c8f 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -5,12 +5,20 @@ from flask.cli import AppGroup from flask_sqlalchemy import SQLAlchemy import flask_login +from authlib.integrations.flask_client import OAuth + app = Flask(__name__) app.config.from_object('app.config') bcrypt = Bcrypt(app) db = SQLAlchemy(app) login_manager = flask_login.LoginManager() +oauth = OAuth(app) + +oauth.register( + name="google", + server_metadata_url="https://accounts.google.com/.well-known/openid-configuration", + client_kwargs={"scope": "openid email profile"}) login_manager.init_app(app) diff --git a/app/config_dev.py b/app/config_dev.py index e0b32a0..e44163a 100644 --- a/app/config_dev.py +++ b/app/config_dev.py @@ -1,5 +1,12 @@ +import os + SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/test.db' SQLALCHEMY_TRACK_MODIFICATIONS = False + SECRET_KEY = "tchtchtch" + ADMIN_EMAIL = "admin@example.com" -ADMIN_PASSWORD = "iamgroot" \ No newline at end of file +ADMIN_PASSWORD = "iamgroot" + +GOOGLE_CLIENT_ID=os.environ.get("GOOGLE_CLIENT_ID2") +GOOGLE_CLIENT_SECRET=os.environ.get("GOOGLE_CLIENT_SECRET2") \ No newline at end of file diff --git a/app/templates/auth/signin.html b/app/templates/auth/signin.html index eecff34..f35cbb6 100644 --- a/app/templates/auth/signin.html +++ b/app/templates/auth/signin.html @@ -21,8 +21,64 @@ {{ render_field(form.password) }}
- +
+

-OR-

+ + + {% endblock %} \ No newline at end of file diff --git a/app/templates/auth/signup.html b/app/templates/auth/signup.html index e0cae82..dbe9ca6 100644 --- a/app/templates/auth/signup.html +++ b/app/templates/auth/signup.html @@ -34,5 +34,62 @@ +

-OR-

+ + + + {% endblock %} \ No newline at end of file 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(): -- cgit v1.2.3