mitlist/be/app/api/auth/magic_link.py
mohamad 229f6b7b1c feat: Introduce activity tracking and management features
This commit adds new functionality for tracking user activities within the application, including:

- Implementation of a new activity service to fetch and manage group activities.
- Creation of a dedicated activity store to handle state management for activities.
- Introduction of new API endpoints for retrieving paginated activity data.
- Enhancements to the UI with new components for displaying activity feeds and items.
- Refactoring of existing components to utilize the new activity features, improving user engagement and interaction.

These changes aim to enhance the application's activity tracking capabilities and provide users with a comprehensive view of their interactions.
2025-06-28 19:14:51 +02:00

60 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from fastapi import APIRouter, HTTPException, status, Depends, Request, Query
from pydantic import BaseModel, EmailStr
from app.auth import get_user_manager, AuthenticationBackendWithRefresh, bearer_transport, get_jwt_strategy, get_refresh_jwt_strategy
from fastapi.responses import JSONResponse
router = APIRouter()
class MagicLinkRequest(BaseModel):
email: EmailStr
# Path: POST /api/v1/auth/magic-link
@router.post('/magic-link', status_code=status.HTTP_200_OK)
async def send_magic_link(payload: MagicLinkRequest, request: Request, user_manager=Depends(get_user_manager)):
"""Generate a one-time magic-link token and *log* it for now.
In production this should email the user. For Phase-4 backend milestone we
simply issue the verification token and return it in the response so the
frontend can test the flow without an email provider.
"""
# Ensure user exists (create guest if not)
user = await user_manager.get_by_email(payload.email)
if user is None:
# Auto-register guest account (inactive until verified)
user_in = {
'email': payload.email,
'password': '', # FastAPI Users requires but we will bypass login
}
# Using UserCreate model generically relies on fastapi-users internals
try:
user = await user_manager.create(user_in, safe=True, request=request)
except Exception:
raise HTTPException(status_code=400, detail='Unable to create account')
verification_token = await user_manager.generate_verification_token(user)
# TODO: send email instead of returning token
return {'detail': 'Magic link generated (token returned for dev)', 'token': verification_token}
@router.get('/magic-link/verify')
async def verify_magic_link(token: str = Query(...), request: Request = None, user_manager=Depends(get_user_manager)):
"""Verify incoming token and issue standard JWT + refresh tokens."""
try:
user = await user_manager.verify(token, request)
except Exception:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail='Invalid or expired token')
# Issue JWT + refresh using existing backend routines
access_strategy = get_jwt_strategy()
refresh_strategy = get_refresh_jwt_strategy()
access_token = await access_strategy.write_token(user)
refresh_token = await refresh_strategy.write_token(user)
return JSONResponse(
{
'access_token': access_token,
'refresh_token': refresh_token,
'token_type': 'bearer',
}
)