Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set cookie in backend #87

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
set cookie in backend
  • Loading branch information
bracesproul committed Mar 21, 2024
commit 907d6408677adc0fc6d905bf4bf3314bc37df9a3
20 changes: 19 additions & 1 deletion backend/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import os
from pathlib import Path

from fastapi import FastAPI
from fastapi import FastAPI, Request, Response
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from langserve import add_routes
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
from starlette.responses import Response
import uuid

from server.api import configurables, examples, extract, extractors, shared, suggest
from server.extraction_runnable import (
Expand All @@ -29,6 +32,21 @@
],
)

class EnsureUserIDMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
user_id = request.cookies.get('user_id')
if not user_id:
user_id = str(uuid.uuid4()) # Generate a new user_id
request.state.user_id = user_id # Set user_id in request state
response = await call_next(request)
response.set_cookie(key="user_id", value=user_id, httponly=True)
else:
request.state.user_id = user_id # Set existing user_id in request state
response = await call_next(request)
return response

app.add_middleware(EnsureUserIDMiddleware)

ROOT = Path(__file__).parent.parent

ORIGINS = os.environ.get("CORS_ORIGINS", "").split(",")
Expand Down
23 changes: 0 additions & 23 deletions frontend/middleware.ts

This file was deleted.

Loading