Sign-In with Google (set up Google authentication)

justaweirdguy
 from abilities import url_for_uploaded_file
from abilities import upload_file_to_storage
import os
import requests
from flask import Flask, render_template, request, redirect, url_for, session, flash
from app_init import app

@app.route("/login", methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        if 'credential' in request.form:
            # Handle Google Sign-In
            credential = request.form['credential']
            # Simulate Google Sign-In success
            session['username'] = 'GoogleUser'
            return redirect(url_for('profile'))
        else:
            username = request.form['username']
            # Simulate login success
            session['username'] = username
            return redirect(url_for('profile'))
    return render_template('login.html', google_client_id=os.environ.get('GOOGLE_CLIENT_ID'))

@app.route("/auth/google", methods=['POST'])

About this template

A quick template to help anyone configure their own Google sign-in authentication for your app!

Introduction to the User Authentication System Template

This template provides a starting point for building a user authentication system with a Google Sign-In option. It includes a home page, login page, and profile page, along with the necessary backend logic to handle user sessions and Google authentication. The template uses Flask as the web framework and Tailwind CSS for styling.

Getting Started

To begin using this template, click on "Start with this Template" in the Lazy builder interface. This will pre-populate the code in the Lazy Builder, so you won't need to copy or paste any code manually.

Initial Setup

Before testing the template, you'll need to set up an environment secret for the Google client ID:

  1. Go to the Google Developers Console (https://console.developers.google.com/).
  2. Create a new project or select an existing one.
  3. Go to "Credentials" and create a new OAuth 2.0 client ID.
  4. Set the authorized JavaScript origins and redirect URIs to match the domain where your app will be hosted.
  5. Copy the client ID provided by Google.
  6. In the Lazy builder interface, navigate to the Environment Secrets tab.
  7. Create a new secret with the key `GOOGLE_CLIENT_ID` and paste the client ID you copied as the value.

Test: Pressing the Test Button

Once the environment secret is set up, you can press the "Test" button in the Lazy builder. This will deploy your app and launch the Lazy CLI. You will not be prompted for any user input at this stage since the template does not require it.

Using the App

After deployment, Lazy will provide you with a dedicated server link to access your app. You can use this link to interact with the frontend of your user authentication system:

  • Visit the home page to see the welcome message and features list.
  • Go to the login page to sign in with a username and password or use the Google Sign-In option.
  • Once logged in, you will be redirected to the profile page where you can view and edit your profile details.

Integrating the App

If you wish to integrate this user authentication system into another service or frontend, you may need to:

  • Update the redirect URIs in your Google project to include the domain of the service where you're integrating the authentication system.
  • Ensure that the backend endpoints for login, logout, and profile management are correctly referenced in the frontend of the service you're integrating with.
  • Use the provided server link in any external tool that requires user authentication.

Remember to secure your application by keeping your environment secrets confidential and following best practices for OAuth 2.0 authentication.

Category
Last published
July 26, 2024

More templates like this

Search Email with GMail API

This is a FastAPI-based application that uses Google’s Gmail API to fetch and display emails based on user-defined search queries. It provides a /search_emails endpoint for email searches and a /oauth2callback endpoint for OAuth2 callbacks. The app requires the environment variables CLIENT_ID, CLIENT_SECRET, and REDIRECT_URI for OAuth2 authentication. These credentials pertain to your Google API and must be set correctly for the application to function as expected.

Fast API
Python
Gmail

Get & Read Email with GMail API

This Python script uses Flask to create a web application that connects to the Gmail API and retrieves the user's unread emails. The script defines two routes: "/" displays the user's unread emails, and "/oauth2callback" is the OAuth 2.0 callback URI. Following api keys need to be set as environment secrets to authenticate with google api: 1. CLIENT_ID 2. CLIENT_SECRET 3. REDIRECT_URI

Python
Flask
Gmail

Gmail Organization Invitation API

This app is an API that sends an invitation email from a Gmail account with 2FA enabled. It accepts various inputs to generate a personalized invitation email based on the name of the email of the person being invited, the email who invited the person and the name of the organisation and an invitation link.

Gmail
Python
Home
/
Sign-In with Google (set up Google authentication)