master

laravel/framework

Last updated at: 29/12/2023 09:20

BroadcastController.php

TLDR

This file, BroadcastController.php, is a part of the Illuminate\Broadcasting namespace in the Demo Projects project. It contains a class named BroadcastController which extends the Illuminate\Routing\Controller class. This controller provides two methods, authenticate and authenticateUser, that are used to authenticate requests for accessing channels in a broadcasting system.

Methods

authenticate

This method authenticates the request for channel access by ref lashing the session and returning the authentication result received from the Broadcast::auth method.

authenticateUser

This method authenticates the current user by ref lashing the session and resolving the authenticated user using the Broadcast::resolveAuthenticatedUser method. If the user cannot be resolved, it throws an AccessDeniedHttpException.

<?php

namespace Illuminate\Broadcasting;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Broadcast;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

class BroadcastController extends Controller
{
    /**
     * Authenticate the request for channel access.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function authenticate(Request $request)
    {
        if ($request->hasSession()) {
            $request->session()->reflash();
        }

        return Broadcast::auth($request);
    }

    /**
     * Authenticate the current user.
     *
     * See: https://pusher.com/docs/channels/server_api/authenticating-users/#user-authentication.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function authenticateUser(Request $request)
    {
        if ($request->hasSession()) {
            $request->session()->reflash();
        }

        return Broadcast::resolveAuthenticatedUser($request)
                    ?? throw new AccessDeniedHttpException;
    }
}