This document explains how to set up Firebase Google Sign-In authentication for the Achievement Management System.
The Achievement Management System uses Firebase for Google Sign-In authentication. This allows users to sign in using their Google accounts instead of traditional username/password login.
- NEVER commit the
.envfile to GitHub - it contains sensitive Firebase credentials - The
.envfile is already in.gitignoreand will not be committed - Share
.env.exampleinstead, which other developers can use as a template - Each developer and deployment should have their own Firebase project and credentials
- Go to Firebase Console
- Click "Create a new project"
- Enter your project name (e.g., "Achievement Management System")
- Enable Google Analytics if desired
- Click "Create project"
- In Firebase Console, click the Web icon (</> symbol) to register a web app
- Name your app (e.g., "AMS-Web")
- Check "Also set up Firebase Hosting for this app" (optional)
- Click "Register app"
- Copy the Firebase config object (you'll need this in Step 5)
- In Firebase Console, go to Authentication in the left sidebar
- Click the Sign-in method tab
- Click Google from the list
- Toggle ON the switch
- Select a project support email
- Click "Save"
- Go to Google Cloud Console
- Select your Firebase project
- Go to APIs & Services > OAuth consent screen
- Select "External" user type
- Fill in the app information:
- App name: Achievement Management System
- User support email: Your email
- Scopes: Add
emailandprofilescopes
- Add test users (your email account for testing)
- Click "Save and Continue"
-
Copy
.env.exampleto.env:cp .env.example .env
-
Open
.envand fill in your Firebase configuration:FIREBASE_API_KEY=your_api_key_here FIREBASE_AUTH_DOMAIN=your_project.firebaseapp.com FIREBASE_DATABASE_URL=https://your_project-default-rtdb.firebaseio.com FIREBASE_PROJECT_ID=your_project_id FIREBASE_STORAGE_BUCKET=your_project.firebasestorage.app FIREBASE_MESSAGING_SENDER_ID=your_sender_id FIREBASE_APP_ID=1:your_app_id:web:your_web_id FIREBASE_MEASUREMENT_ID=your_measurement_id
-
IMPORTANT: Never commit this file. It's in
.gitignoreautomatically.
In templates/home.html, update the Google Sign-In element:
<div id="g_id_onload"
data-client_id="YOUR_ACTUAL_GOOGLE_CLIENT_ID"
data-callback="handleCredentialResponse">
</div>Replace YOUR_ACTUAL_GOOGLE_CLIENT_ID with your actual Google OAuth 2.0 Client ID (found in Google Cloud Console > APIs & Services > Credentials).
If you haven't already, install python-dotenv:
pip install python-dotenv
pip install -r requirements.txt-
Activate your virtual environment:
venv1\Scripts\Activate.ps1
-
Run the Flask app:
python app.py
-
Navigate to
http://localhost:5000in your browser -
You should see a "Sign in with Google" button on the home page
-
Test the Google Sign-In by clicking the button
.env # Local config (NOT committed) - ADD YOUR CREDENTIALS HERE
.env.example # Template for developers (committed to GitHub)
firebase_config.py # Server-side Firebase config manager
static/js/firebase-init.js # Frontend Firebase SDK initialization
templates/home.html # Updated with Google Sign-In button
app.py # Updated with Firebase auth routes
Route: POST /auth/google-login
- Purpose: Handle Google Sign-In authentication
- Expected Data: Email, displayName, photoURL, uid, idToken
- Response: JSON with success status and redirect URL
- TODO: Add Firebase Admin SDK token verification (see comments in
app.py)
Route: POST /auth/logout
- Purpose: Clear user session
- Response: JSON with success status
Route: GET /auth/firebase-config
- Purpose: Serve Firebase config to frontend
- Response: JSON Firebase configuration
File: templates/home.html
- Firebase config is injected from server (lines with
{{ firebase_config['...'] }}) - Google Sign-In callback:
handleCredentialResponse() - Google client ID needs to be updated (see Step 6)
File: static/js/firebase-init.js
- Firebase SDK initialization
- Google provider setup
- Sign-in and sign-out functions
- Token verification (currently basic, should be enhanced)
For development, you can test with:
- Any Google account that's a "test user" in the OAuth consent screen
- Make sure you have a matching student record in the database
- Check that the Google client ID in
home.htmlmatches your project - Verify the accounts are set as test users in OAuth consent screen
- Check that
.envfile exists and has all required fields - Ensure
python-dotenvis installed - Check Flask debug logs for errors
- TODO: Implement Firebase Admin SDK integration (see
app.pycomments) - Currently basic validation is in place; add proper verification for production
-
Token Verification: Implement Firebase Admin SDK to verify tokens
from firebase_admin import auth decoded_token = auth.verify_id_token(idToken)
-
Store Firebase UID: Save Firebase UID with student records for better tracking
-
Error Handling: Implement comprehensive error handling and user feedback
-
HTTPS: Use HTTPS in production (Firebase requires it)
-
CORS: Configure CORS properly for your domain
For questions or issues, refer to the Firebase documentation or contact the development team.