This documentation provides an overview of a Flask web application that uses Google OAuth for user authentication. The application allows users to log in using their Google accounts and performs basic user management tasks, such as user registration, login, logout, profile viewing, and updates.
-
Introduction
- Application Overview
- Prerequisites
-
Installation and Setup
- Installing Dependencies
- Configuration
-
Application Structure
- Project Structure
- Key Components
-
Google OAuth Configuration
- Setting up Google OAuth Credentials
- Configuring OAuth in Flask
-
User Management
- User Registration
- User Login
- User Logout
- Profile Viewing and Updates
-
Running the Application
- Starting the Development Server
- Accessing the Application
This Flask application demonstrates how to integrate Google OAuth into a web application for user authentication. It includes features like user registration, login, and management of user profiles. Users can sign in using their Google accounts, and their Google-provided information is used to create and update user profiles within the application.
Before you begin, make sure you have the following prerequisites installed:
- Python 3.x
- Flask
- SQLAlchemy
- Flask-Login
- Flask-SQLAlchemy
- psycopg2-binary (for PostgreSQL support)
- Flask-Bcrypt
- Flask-OAuthlib
Create a virtual environment and install the required dependencies listed in the requirements.txt file. Use the following command:
pip install -r requirements.txtIn the application code, ensure that you configure your Google OAuth credentials and other settings appropriately.
app.py: The main application file containing Flask routes and OAuth configuration.services/: Directory for services handling CRUD operations on user data.domain/: Contains the user model (e.g.,user_model.py).infrastructure/: Configuration files for the database (e.g.,user_database.py).templates/: HTML templates for the application's web pages.
Flask: The core Flask web framework for routing and handling HTTP requests.Flask-Login: Extension for managing user sessions.Flask-OAuthlib: Extension for OAuth2 integration, specifically for Google OAuth.SQLAlchemy: ORM (Object-Relational Mapping) library for database interactions.Flask-Bcrypt: Used for hashing user passwords securely.
To use Google OAuth, you need to create a project on the Google Developers Console and configure OAuth credentials. Ensure that you set the correct redirect URIs for your Flask application. The credentials typically include a Client ID and a Client Secret.
In the app.py file, you'll find the OAuth configuration for Google. Replace the placeholders for consumer_key and consumer_secret with your actual Google OAuth credentials.
Users can register on the application by providing a username, password, first name, last name, and email address. Passwords are securely hashed before storage.
Registered users can log in using their usernames and passwords. Passwords are hashed and checked for authentication.
Logged-in users can log out of their accounts, terminating their sessions.
- Users can view their profiles, which include their username, first name, last name, and email.
- Users can update their profiles, including their first name, last name, email, and password.
To start the development server, run the following command:
python app.pyThe application should be accessible at http://localhost:80 in your web browser.
- Access the home page at
http://localhost:80. - Click on the "Google Login" button to initiate Google OAuth login.
- Upon successful login, users will be redirected to the "All Users" page, displaying a list of users.
- Users can navigate to the "Profile" page to view and update their information.
- Users can log out by clicking the "Logout" button.
You've successfully set up a Flask web application with Google OAuth for user authentication and basic user management features. Customize and expand upon this foundation to build more complex applications as needed.