Skip to content

Enable different CORS for different users #1918

Answered by alex-oleshkevich
gikpro asked this question in Q&A
Discussion options

You must be logged in to vote

It could be something like this. Note, I didn't run this, but you should get the idea:

from starlette.middleware.cors import CORSMiddleware

class MyCORSMiddleware:
	def __init__(app):
		self.app = app

	async def __call__(self, scope, receive, send):
		current_user = scope['user']
		
		if current_user.id = 1:
			cors_mw = CORSMiddleware(self.app, **options_1)

		if current_user.id = 2:
			cors_mw = CORSMiddleware(self.app, **options_2)
	
		await cors_mw(scope, receive, send)
  

middleware = [
 	Middleware(AuthenticationMiddleware), # will add user info into request
	Middleware(MyCORSMiddleware), 
]

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@gikpro
Comment options

@alex-oleshkevich
Comment options

Answer selected by Kludex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants