Purchase access to watch this video.

Unlock course for £159

Already purchased access? Login

Only allowing subscribed customers to watch videos

Connect GitHub

Transcript was automatically generated and may be inaccurate.

  1. –

    We now have videos on our website, we now have customers able to subscribe, but
  2. –

    without
  3. –

    a subscription, customers can still watch videos without paying, so let's remedy
  4. –

    that.
  5. –

    If we head back to the Cashier docs, we can see there's a section called
  6. –

    "Checking Subscription Status".
  7. –

    We can see from the code sample it says that we can use a "subscribed" method on
  8. –

    a User
  9. –

    model instance to determine whether a user subscribed or not.
  10. –

    The first argument is the name of the product.
  11. –

    We left this as "default", so we can even omit that argument and just use $user->subscribed
  12. –

    The Cashier docs suggest using middleware, but we don't want to completely block
  13. –

    users
  14. –

    who aren't subscribed.
  15. –

    We still want guests and non-subscribers to be able to see the videos' details.
  16. –

    We just don't want them to be able to watch the actual video without an active
  17. –

    subscription.
  18. –

    Therefore, we're going to do the subscribed check in the view.
  19. –

    We can make use of a policy here, as we're checking access for the authenticated user
  20. –

    against
  21. –

    a model.
  22. –

    Let's create a VideoPolicy class.
  23. –

    Open up the auth service provider file, and add a mapping for our Video model
  24. –

    and
  25. –

    now a new VideoPolicy class.
  26. –

    Open up the VideoPolicy class, copy the "view" method, paste it, and rename it to
  27. –

    "watch".
  28. –

    Inside this method, we can just check if the user is currently subscribed.
  29. –

    Open up your videos show view, and we can use the @can directive to check if
  30. –

    the user
  31. –

    is authenticated and able to watch the video.
  32. –

    We'll wrap the mux-player tag in the @can directive.
  33. –

    For users who do not have permission to watch the video, let's just show the
  34. –

    thumbnail
  35. –

    with a message saying they need to be subscribed to watch.
  36. –

    We can also add a link to subscribe.
  37. –

    If we open a private browsing window, we should now see the thumbnail with a
  38. –

    message saying
  39. –

    that user needs to be subscribed, and a link to subscribe.
  40. –

    So now we have videos locked behind a paywall where only paying subscribers
  41. –

    can watch.
  42. –

    In the next lesson, we'll look at how we can track activity in our videos using
  43. –

    Mux Data.