Skip to content

Commit

Permalink
custom auth backend endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
josancamon19 committed Nov 17, 2024
1 parent 9240584 commit 40d57be
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
3 changes: 2 additions & 1 deletion backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from modal import Image, App, asgi_app, Secret, Cron
from routers import workflow, chat, firmware, plugins, memories, transcribe_v2, notifications, \
speech_profile, agents, facts, users, processing_memories, trends, sdcard, sync, apps
speech_profile, agents, facts, users, processing_memories, trends, sdcard, sync, apps, custom_auth
from utils.other.notifications import start_cron_job

if os.environ.get('SERVICE_ACCOUNT_JSON'):
Expand Down Expand Up @@ -37,6 +37,7 @@
app.include_router(sync.router)

app.include_router(apps.router)
app.include_router(custom_auth.router)

modal_app = App(
name='backend',
Expand Down
57 changes: 57 additions & 0 deletions backend/routers/custom_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from typing import Optional
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from firebase_admin import auth
import os
import requests

router = APIRouter()


class UserCredentials(BaseModel):
email: str
password: str
name: Optional[str] = None


@router.post("/v1/signup")
def sign_up(credentials: UserCredentials):
try:
user = auth.create_user(
email=credentials.email,
password=credentials.password,
display_name=credentials.name,
)
return {"status": "ok", "message": "User created successfully", "uid": user.uid}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))


@router.post("/v1/signin")
def sign_in(credentials: UserCredentials):
try:
api_key = os.getenv("CUSTOM_AUTH_FIREBASE_API_KEY")
url = f"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key={api_key}"
payload = {
"email": credentials.email,
"password": credentials.password,
"returnSecureToken": True,
}
response = requests.post(url, json=payload)
if response.status_code != 200:
print(response.json())
raise HTTPException(status_code=401, detail="Invalid credentials")

firebase_token = response.json().get("idToken")
decoded_token = auth.verify_id_token(firebase_token)
return {
"status": "ok",
"token": firebase_token,
"uid": decoded_token["uid"],
"name": decoded_token["name"],
"auth_time": decoded_token["auth_time"],
"exp": decoded_token["exp"],
}
except Exception as e:
print("error authenticating", e)
raise HTTPException(status_code=400, detail=str(e))

0 comments on commit 40d57be

Please sign in to comment.