Only allowing subscribed customers to watch videos
Transcript was automatically generated and may be inaccurate.
-
–
We now have videos on our website, we now have customers able to subscribe, but -
–
without -
–
a subscription, customers can still watch videos without paying, so let's remedy -
–
that. -
–
If we head back to the Cashier docs, we can see there's a section called -
–
"Checking Subscription Status". -
–
We can see from the code sample it says that we can use a "subscribed" method on -
–
a User -
–
model instance to determine whether a user subscribed or not. -
–
The first argument is the name of the product. -
–
We left this as "default", so we can even omit that argument and just use $user->subscribed -
–
The Cashier docs suggest using middleware, but we don't want to completely block -
–
users -
–
who aren't subscribed. -
–
We still want guests and non-subscribers to be able to see the videos' details. -
–
We just don't want them to be able to watch the actual video without an active -
–
subscription. -
–
Therefore, we're going to do the subscribed check in the view. -
–
We can make use of a policy here, as we're checking access for the authenticated user -
–
against -
–
a model. -
–
Let's create a VideoPolicy class. -
–
Open up the auth service provider file, and add a mapping for our Video model -
–
and -
–
now a new VideoPolicy class. -
–
Open up the VideoPolicy class, copy the "view" method, paste it, and rename it to -
–
"watch". -
–
Inside this method, we can just check if the user is currently subscribed. -
–
Open up your videos show view, and we can use the @can directive to check if -
–
the user -
–
is authenticated and able to watch the video. -
–
We'll wrap the mux-player tag in the @can directive. -
–
For users who do not have permission to watch the video, let's just show the -
–
thumbnail -
–
with a message saying they need to be subscribed to watch. -
–
We can also add a link to subscribe. -
–
If we open a private browsing window, we should now see the thumbnail with a -
–
message saying -
–
that user needs to be subscribed, and a link to subscribe. -
–
So now we have videos locked behind a paywall where only paying subscribers -
–
can watch. -
–
In the next lesson, we'll look at how we can track activity in our videos using -
–
Mux Data.