Sign-In with Google (set up Google authentication)
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'])
Created: | Last Updated:
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:
- Go to the Google Developers Console (https://console.developers.google.com/).
- Create a new project or select an existing one.
- Go to "Credentials" and create a new OAuth 2.0 client ID.
- Set the authorized JavaScript origins and redirect URIs to match the domain where your app will be hosted.
- Copy the client ID provided by Google.
- In the Lazy builder interface, navigate to the Environment Secrets tab.
- 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.