#authorizePresenceChannel error while returning value from channel

1 messages · Page 1 of 1 (latest)

brittle fog
#

channels.php

Broadcast::channel('lobby.{roomId}', function ($user, $roomId) {
    logger('Authenticated User:', ['user' => $user]);
    return ['id' => (string) $user->id, 'name' => $user->name];
});

js:

            let roomId = "{{ $lobby->lobby_id }}";
            let channel = `lobby.${roomId}`;
            console.log(`lobby.${roomId}`)

            Echo.join(channel)
                .here((users) => {
                    // ...
                })
                .joining((user) => {
                    console.log(user.name);
                })
                .leaving((user) => {
                    console.log(user.name);
                })
                .error((error) => {
                    console.error(error);
                });```
#

stack trace:

[2025-01-11 18:59:03] production.ERROR: Pusher\Pusher::authorizePresenceChannel(): Argument #3 ($user_id) must be of type string, null given, called in /var/www/html/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php on line 119 {"exception":"[object] (TypeError(code: 0): Pusher\\Pusher::authorizePresenceChannel(): Argument #3 ($user_id) must be of type string, null given, called in /var/www/html/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php on line 119 at /var/www/html/vendor/pusher/pusher-php-server/src/Pusher.php:934)
[stacktrace]
#0 /var/www/html/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php(119): Pusher\\Pusher->authorizePresenceChannel()
#1 /var/www/html/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php(124): Illuminate\\Broadcasting\\Broadcasters\\PusherBroadcaster->validAuthenticationResponse()
#2 /var/www/html/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php(85): Illuminate\\Broadcasting\\Broadcasters\\Broadcaster->verifyUserCanAccessChannel()
#3 /var/www/html/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastManager.php(490): Illuminate\\Broadcasting\\Broadcasters\\PusherBroadcaster->auth()```

logger call:

[2025-01-11 18:59:03] production.DEBUG: Authenticated User: {"user":{"App\Models\User":{"id":1...```

brittle fog
#
POST
https://mydomain/broadcasting/auth
[HTTP/2 500  59ms]

Object { type: "AuthError", error: "Unable to retrieve auth string from channel-authorization endpoint - received status: 500 from /broadcasting/auth. Clients must be authorized to join private or presence channels. See: https://pusher.com/docs/channels/server_api/authorizing-users/", status: 500 }```
brittle fog
#

ok well i can't tell if this is a laravel related bug anymore

#
[2025-01-12 09:33:28] production.DEBUG: getAuthIdentifierForBroadcasting:   
[2025-01-12 09:33:28] production.DEBUG: Primary Key Name: id  
[2025-01-12 09:33:28] production.DEBUG: User attempting to join: {"user_id":"1","name":"skul","roomId":"QqGE0"} ```
#

Auth::user()->getAuthIdentifierForBroadcasting() follows this chain

getAuthIdentifierForBroadcasting -> getAuthIdentifier -> getAuthIdentifierName -> getKeyName -> return $this->primaryKey;

#

but logger('Primary Key Name: ' . $user->getKeyName()); is returning id

#
        logger('getAuthIdentifierForBroadcasting: ' . $user->getAuthIdentifierForBroadcasting());
        logger('getAuthIdentifier: ' . $user->getAuthIdentifier());
        logger('getAuthIdentifierName: ' . $user->getAuthIdentifierName());
        logger('getKeyName: ' . $user->getKeyName());```

returns

[2025-01-12 09:35:52] production.DEBUG: getAuthIdentifierForBroadcasting:
[2025-01-12 09:35:52] production.DEBUG: getAuthIdentifier:
[2025-01-12 09:35:52] production.DEBUG: getAuthIdentifierName: handle
[2025-01-12 09:35:52] production.DEBUG: getKeyName: id ```

#

I don't think Laravel likes me using custom auth identifier names

#

ok well adding ```php
public function getAuthIdentifierForBroadcasting()
{
return 'id';
}