Flask google login

    0

    0

    RF-Fahad-Islam

    db.session.
    

    Library: flask

    from authlib.integrations.flask_client import OAuth
    from flask import Flask, render_template
    
    # More Info : https://docs.authlib.org/en/latest/client/flask.html
    app = Flask(__name__)
    CONF_URL = 'https://accounts.google.com/.well-known/openid-configuration'
    #Authlib
    oauth = OAuth(app)
    
    #Register google outh
    google = oauth.register(
      name='google',
      server_metadata_url=CONF_URL,
      # Collect client_id and client secret from google auth api
      client_id= os.environ.get("GOOGLE_CLIENT_ID"),
      client_secret = os.environ.get("GOOGLE_CLIENT_SECRET"),
      client_kwargs={
        'scope': 'openid email profile'
      }
    )
    
    #Routes for login
    @app.route('/google-login')
    def googleLogin():
        redirect_uri = url_for('authorize', _external=True)
        google = oauth.create_client('google')
        return google.authorize_redirect(redirect_uri)
    
    
    @app.route('/authorize')
    def authorize():
        token = oauth.google.authorize_access_token()
        user = token['userinfo']
      # user will return a dict of info like: email = user.get("email")
        #Save the user info to database and login the user
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.