Skip to content

Commit

Permalink
fix: channels connection authorization #29
Browse files Browse the repository at this point in the history
  • Loading branch information
munafio committed Dec 5, 2022
1 parent 31f2cf6 commit b5925e1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
26 changes: 24 additions & 2 deletions src/ChatifyMessenger.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,36 @@ public function push($channel, $event, $data)
/**
* Authentication for pusher
*
* @param User $requestUser
* @param User $authUser
* @param string $channelName
* @param string $socket_id
* @param array $data
* @return void
*/
public function pusherAuth($channelName, $socket_id, $data = null)
public function pusherAuth($requestUser, $authUser, $channelName, $socket_id)
{
return $this->pusher->socket_auth($channelName, $socket_id, $data);
// Auth data
$authData = json_encode([
'user_id' => $authUser->id,
'user_info' => [
'name' => $authUser->name
]
]);
// check if user authenticated
if (Auth::check()) {
if($requestUser->id == $authUser->id){
return $this->pusher->socket_auth(
$channelName,
$socket_id,
$authData
);
}
// if not authorized
return response()->json(['message'=>'Unauthorized'], 401);
}
// if not authenticated
return response()->json(['message'=>'Not authenticated'], 403);
}

/**
Expand Down
25 changes: 7 additions & 18 deletions src/Http/Controllers/Api/MessagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,12 @@ class MessagesController extends Controller
*/
public function pusherAuth(Request $request)
{
// Auth data
$authData = json_encode([
'user_id' => Auth::user()->id,
'user_info' => [
'name' => Auth::user()->name
]
]);
// check if user authorized
if (Auth::check()) {
return Chatify::pusherAuth(
$request['channel_name'],
$request['socket_id'],
$authData
);
}
// if not authorized
return response()->json(['message'=>'Unauthorized'], 401);
return Chatify::pusherAuth(
$request->user(),
Auth::user(),
$request['channel_name'],
$request['socket_id']
);
}

/**
Expand Down Expand Up @@ -157,7 +146,7 @@ public function send(Request $request)
$messageData = Chatify::fetchMessage($messageID);

// send to user using pusher
Chatify::push('private-chatify', 'messaging', [
Chatify::push("private-chatify.".$request['id'], 'messaging', [
'from_id' => Auth::user()->id,
'to_id' => $request['id'],
'message' => Chatify::messageCard($messageData, 'default')
Expand Down
23 changes: 6 additions & 17 deletions src/Http/Controllers/MessagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,12 @@ class MessagesController extends Controller
*/
public function pusherAuth(Request $request)
{
// Auth data
$authData = json_encode([
'user_id' => Auth::user()->id,
'user_info' => [
'name' => Auth::user()->name
]
]);
// check if user authorized
if (Auth::check()) {
return Chatify::pusherAuth(
$request['channel_name'],
$request['socket_id'],
$authData
);
}
// if not authorized
return response()->json(['message'=>'Unauthorized'], 401);
return Chatify::pusherAuth(
$request->user(),
Auth::user(),
$request['channel_name'],
$request['socket_id']
);
}

/**
Expand Down

0 comments on commit b5925e1

Please sign in to comment.