I'm developing a learning management system using Django and React where I want to restrict video content to only users enrolled in specific courses. I'm attempting to implement Cloudfront signed cookies, but I'm running into a problem. When I request video content from Cloudfront using React (with Video.js for adaptive bitrate streaming), it seems that the cookies aren't being sent at all. I keep getting this error:
`MissingKey
Missing Key-Pair-Id query parameter or cookie value`.
Here's how I'm setting the cookies on the Django backend:
```
response.set_cookie('CloudFront-Policy', cookie_dict['CloudFront-Policy'], path='/', samesite='None', httponly=True, secure=True)
response.set_cookie('CloudFront-Signature', cookie_dict['CloudFront-Signature'], path='/', samesite='None', httponly=True, secure=True)
response.set_cookie('CloudFront-Key-Pair-Id', cookie_dict['CloudFront-Key-Pair-Id'], path='/', samesite='None', httponly=True, secure=True)
```
And this is the React code that sends the request:
```
useEffect(() => {
if (!playerRef.current) {
playerRef.current = videojs(videoRef.current, {..., html5: {withCredentials: true}});
}
}, [src]);
```
The functionality works fine when there are no content restrictions. Any help would be appreciated!
2 Answers
You’re likely facing a configuration issue. Make sure that your cookies are actually being set. When testing locally, keep an eye on your browser's cookie settings to ensure they're being stored properly. If they're not set on the cloudfront domain, they'll be discarded. Verify your cookie's SameSite attribute as well, since using `None` requires the `Secure` flag to be set, and your app needs to be served over HTTPS.
It sounds like your cookie domain settings might be causing the issue. If your application runs on `localhost`, setting the domain to `cloudfront.net` won't work, as browsers won't accept cookies for a different domain. Try using the domain of your application instead. For local development, you can typically set it to `localhost`, but just make sure it matches the origin of your requests.
Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically