This guide is written for developers using
@livepeer/react in a React
application.Create Gated Content
For livestreams
Create your gated stream, with the stream key returned once we create it (styling has been removed for simplicity)accessControl.tsx
For assets
Create your gated asset, with the jwt playback policy type.accessControl.ts
Sign a JWT (Node.JS API Route)
Next, we add an API route - since we are using Next.JS, we add a custom Next.js API route. We add a check in the API route for a special “secret” that must be passed in the POST body for the user to gain access to the stream.Make sure to create a signing key - those
values will be used as the environment variables
ACCESS_CONTROL_PRIVATE_KEY
and NEXT_PUBLIC_ACCESS_CONTROL_PUBLIC_KEY./api/sign-jwt.ts
Configure the Player
Lastly, when the content is created, we make a POST request to the/api/create-signed-jwt API route we created in the previous step.
The React Player passes the JWT with a header,
Livepeer-Jwt, to the backend,
for WebRTC and HLS playback. For MP4 playback, it uses a query parameter,
jwt.jwt prop, which will use that JWT to prove
access to the content!
access-control.tsx
Using a custom player
If you are not using the player, you will need to pass a header,Livepeer-Jwt,
when you perform WebRTC SDP negotiation, or when you play back from a m3u8 URL.
For WebRTC SDP negotiation, here is an example of the header being passed:
If you are using HLS.js for your own custom player, you can set the JWT header like this: