@app.route("/oauth2callback/")
def authenticate_user():

    error = request.args.get("error")
    if error:
        logger.warning("Google sent us an error via OAuth2: %s", error)

        return redirect(url_for("login"))

    # Get OAuth2 authentication code
    code = request.args.get("code")

    # Exchange code for fresh credentials
    credentials = flow.step2_exchange(code)

    # Extract email and email verification
    id_token = credentials.id_token
    email = id_token["email"]
    verified_email = id_token["email_verified"]

    if verified_email is True:
        # Find the user with the given email
        try:
            user = FlaskUser(User.objects.get(email = email))
        except User.DoesNotExist:
            user = None

        if not user:
            flash("A Galah account does not exist for this email.", "error")

            logger.info(
                "User %s has attempted to log in via OAuth2 but an account "
                "does not exist for them.", email
            )
        else:
            login_user(user)

            logger.info(
                "User %s has succesfully logged in via OAuth2.", email
            )

            return redirect(url_for("home"))

    else:
        flash("Sorry, we couldn't verify your email", "error")

        logger.info("User %s failed to authenticate with OAuth2 because "
            "their email has not been verified with google.", email)

    return redirect(url_for("login"))
Return next/previous item in the history, stopping at top/bottom.
dataset/ETHPy150Open ucrcsedept/galah/galah/web/views/_login.py/authenticate_user--crossover with--dataset/ETHPy150Open kuri65536/python-for-android/python-modules/twisted/twisted/manhole/ui/gtk2manhole.py/History.move