Exclusive sign in with google for simple authentication scaffolding across frameworks.
Project description
SwifttAuth-Fire
SwifttAuth-Fire provides Google Sign-In authentication scaffolding for Firebase across frontend and backend applications. It offers a streamlined developer experience by simplifying the configuration of Firebase Auth popups on the frontend and token verification on the backend.
Project Structure
The repository contains two packages:
js/(NPM Package): The frontend library supporting React, Next.js, Vite, and Vanilla JS.python/(PIP Package): The backend validation library supporting Django and FastAPI.
Frontend Setup (JavaScript)
Installation
To install the package:
bun add swifttauth-fire firebase
Or using npm:
npm install swifttauth-fire firebase
Environment Variables
Define the following environment variables in your frontend project's configuration (e.g., .env.local):
NEXT_PUBLIC_FIREBASE_API_KEY=your_api_key
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=your_project.firebaseapp.com
NEXT_PUBLIC_FIREBASE_PROJECT_ID=your_project_id
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=your_project.appspot.com
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=your_sender_id
NEXT_PUBLIC_FIREBASE_APP_ID=your_app_id
Firebase Initialization
Create a utility file (e.g., firebase.js) to initialize the Firebase SDK:
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID
};
const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
Usage Examples
Vanilla JavaScript / Vite
import { auth } from "./firebase.js";
import { signInWithGoogle, getAuthToken } from "swifttauth-fire";
const loginButton = document.getElementById("login");
loginButton.addEventListener("click", async () => {
try {
const credential = await signInWithGoogle(auth);
const token = await getAuthToken(credential.user);
console.log("Bearer Token:", token);
} catch (error) {
console.error("Sign-in failed:", error);
}
});
React / Next.js Client Components
"use client";
import { auth } from "./firebase.js";
import { useSwifttAuth } from "swifttauth-fire/react";
export default function LoginButton() {
const { user, loading, token, login, logout } = useSwifttAuth(auth);
if (loading) return <p>Loading...</p>;
if (user) {
return (
<div>
<p>Welcome, {user.displayName}</p>
<button onClick={logout}>Sign Out</button>
</div>
);
}
return <button onClick={login}>Sign In with Google</button>;
}
Backend Setup (Python)
Installation
Install the package via pip:
pip install swifttauth-fire firebase-admin
Firebase Service Account Credentials
For secure ID token verification, the backend requires a service account key JSON file.
- Generate a private key JSON from the Firebase Console (Project Settings > Service accounts).
- Save the key JSON file securely in your backend project directory. Add the file to your
.gitignoreto prevent committing it. - Define the environment variable pointing to your key file:
FIREBASE_SERVICE_ACCOUNT_PATH=/path/to/your/firebase-adminsdk.json
Alternatively, pass the file path directly to the initializer in your application startup code:
from swifttauth_fire import init_firebase
init_firebase("path/to/firebase-adminsdk.json")
Usage Examples
Django Integration
Protect Django views with the @swiftt_auth_required decorator. This extracts the Bearer token from the Authorization header and assigns the decoded token payload to request.firebase_user.
from django.http import JsonResponse
from swifttauth_fire.django import swiftt_auth_required
@swiftt_auth_required
def protected_dashboard(request):
user = request.firebase_user
return JsonResponse({
"message": f"Welcome back, {user.get('name')}!",
"email": user.get('email'),
"uid": user.get('uid')
})
FastAPI Integration
Protect FastAPI routes using the get_current_user dependency.
from fastapi import FastAPI, Depends
from swifttauth_fire.fastapi import get_current_user
app = FastAPI()
@app.get("/api/protected")
def protected_route(user: dict = Depends(get_current_user)):
return {
"message": "Access granted to protected route",
"user_email": user.get("email"),
"user_id": user.get("uid")
}
Development and Testing
Running JavaScript Tests
Navigate to the js directory, ensure dependencies are installed, and execute tests:
cd js
bun install
bun test
Running Python Tests
Navigate to the python directory, install testing dependencies, and execute tests:
cd python
pip install pytest
pytest
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file swifttauth_fire-1.1.0.tar.gz.
File metadata
- Download URL: swifttauth_fire-1.1.0.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e613c098266facb5f3bcdad822567545269121256830952644f184acf84c7822
|
|
| MD5 |
12d8b3e84d13313b40945ddf86bc94c6
|
|
| BLAKE2b-256 |
285f61937aa895d68f328ed4681a1e9dbddea5d31e3e345c4b433975d8ce5fd7
|
File details
Details for the file swifttauth_fire-1.1.0-py3-none-any.whl.
File metadata
- Download URL: swifttauth_fire-1.1.0-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
daa1868855aa65c2f6c3b4be2f23503254cd75e085f7ac4e671ca7d650759e2d
|
|
| MD5 |
57483af091ef605439eb6fda4c4e9336
|
|
| BLAKE2b-256 |
46963e5c60fba415e84739409297677fbfddb758de2494c57bf472490f0f83a3
|