Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit 21d9585

Browse files
committed
add auth, change type config
1 parent 69f2a62 commit 21d9585

File tree

5 files changed

+91
-55
lines changed

5 files changed

+91
-55
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ return [
2020

2121
'firebase' => [
2222
'driver' => 'firebase',
23-
'type' => 'rtdb', // rtdb or fsdb
23+
'type' => 'firestore', // database or firestore
2424
'databaseURL' => env('FB_DB_URL'), // the real time database url
2525
'creds_file' => env('FB_CREDENTIALS_FILE'), // service account json file
2626
'collection_name' => env('FB_COLLECTION_NAME'), // ex.notifications

src/Broadcasters/Common.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace ctf0\Firebase\Broadcasters;
4+
5+
use Illuminate\Broadcasting\Broadcasters\UsePusherChannelConventions;
6+
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
7+
8+
trait Common
9+
{
10+
use UsePusherChannelConventions;
11+
12+
/**
13+
* {@inheritdoc}
14+
*/
15+
public function auth($request)
16+
{
17+
$channelName = $this->normalizeChannelName($request->channel_name);
18+
19+
if (
20+
$this->isGuardedChannel($request->channel_name) &&
21+
!$this->retrieveUser($request, $channelName)
22+
) {
23+
throw new AccessDeniedHttpException();
24+
}
25+
26+
return parent::verifyUserCanAccessChannel(
27+
$request,
28+
$channelName
29+
);
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function validAuthenticationResponse($request, $result)
36+
{
37+
if (is_bool($result)) {
38+
return json_encode($result);
39+
}
40+
41+
$channelName = $this->normalizeChannelName($request->channel_name);
42+
43+
return json_encode([
44+
'channel_data' => [
45+
'user_id' => $this->retrieveUser($request, $channelName)->getAuthIdentifier(),
46+
'user_info' => $result,
47+
],
48+
]);
49+
}
50+
}

src/Broadcasters/FSDB.php

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ctf0\Firebase\Broadcasters;
44

5+
use Illuminate\Support\Arr;
56
use Illuminate\Support\Str;
67
use Kreait\Firebase\ServiceAccount;
78
use Morrislaptop\Firestore\Factory;
@@ -11,6 +12,8 @@
1112

1213
class FSDB extends Broadcaster
1314
{
15+
use Common;
16+
1417
protected $db;
1518
protected $config;
1619

@@ -28,38 +31,28 @@ public function __construct()
2831
->createFirestore();
2932
}
3033

31-
/**
32-
* {@inheritdoc}
33-
*/
34-
public function auth($request)
35-
{
36-
}
37-
38-
/**
39-
* {@inheritdoc}
40-
*/
41-
public function validAuthenticationResponse($request, $result)
42-
{
43-
}
44-
4534
/**
4635
* {@inheritdoc}
4736
*/
4837
public function broadcast(array $channels, $event, array $payload = [])
4938
{
50-
$db = $this->db;
51-
52-
try {
53-
$coll = $db->collection($this->config['collection_name']);
54-
$doc = $coll->document(md5(Str::uuid()));
55-
$doc->set([
56-
'timestamp' => round(now()->valueOf()), // return date == to js Date.now()
57-
'channels' => $this->formatChannels($channels),
58-
'data' => $payload,
59-
'event' => $event,
60-
]);
61-
} catch (ApiException $e) {
62-
throw new BroadcastException($e);
39+
$db = $this->db;
40+
$socket = Arr::pull($payload, 'socket');
41+
42+
foreach ($this->formatChannels($channels) as $channel) {
43+
try {
44+
$coll = $db->collection($this->config['collection_name']);
45+
$doc = $coll->document(md5(Str::uuid()));
46+
$doc->set([
47+
'channel' => $channel,
48+
'data' => $payload,
49+
'event' => $event,
50+
'socket' => $socket,
51+
'timestamp' => round(now()->valueOf()), // return date == to js Date.now()
52+
]);
53+
} catch (ApiException $e) {
54+
throw new BroadcastException($e);
55+
}
6356
}
6457
}
6558
}

src/Broadcasters/RTDB.php

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ctf0\Firebase\Broadcasters;
44

5+
use Illuminate\Support\Arr;
56
use Kreait\Firebase\Factory;
67
use Kreait\Firebase\ServiceAccount;
78
use Kreait\Firebase\Exception\ApiException;
@@ -10,6 +11,8 @@
1011

1112
class RTDB extends Broadcaster
1213
{
14+
use Common;
15+
1316
protected $db;
1417
protected $config;
1518

@@ -28,37 +31,27 @@ public function __construct($config)
2831
->create();
2932
}
3033

31-
/**
32-
* {@inheritdoc}
33-
*/
34-
public function auth($request)
35-
{
36-
}
37-
38-
/**
39-
* {@inheritdoc}
40-
*/
41-
public function validAuthenticationResponse($request, $result)
42-
{
43-
}
44-
4534
/**
4635
* {@inheritdoc}
4736
*/
4837
public function broadcast(array $channels, $event, array $payload = [])
4938
{
50-
$db = $this->db->getDatabase();
51-
52-
try {
53-
$db->getReference($this->config['collection_name'])
54-
->push([
55-
'timestamp' => round(now()->valueOf()), // return date == to js Date.now()
56-
'channels' => $this->formatChannels($channels),
57-
'data' => $payload,
58-
'event' => $event,
59-
]);
60-
} catch (ApiException $e) {
61-
throw new BroadcastException($e);
39+
$db = $this->db->getDatabase();
40+
$socket = Arr::pull($payload, 'socket');
41+
42+
foreach ($this->formatChannels($channels) as $channel) {
43+
try {
44+
$db->getReference($this->config['collection_name'])
45+
->push([
46+
'channel' => $channel,
47+
'data' => $payload,
48+
'event' => $event,
49+
'socket' => $socket,
50+
'timestamp' => round(now()->valueOf()), // return date == to js Date.now()
51+
]);
52+
} catch (ApiException $e) {
53+
throw new BroadcastException($e);
54+
}
6255
}
6356
}
6457
}

src/FireBaseBroadcastProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function boot()
1717
app(BroadcastManager::class)->extend('firebase', function ($app) {
1818
$config = config('broadcasting.connections.firebase');
1919

20-
return $config['type'] == 'rtdb' ? new RTDB($config) : new FSDB($config);
20+
return $config['type'] == 'database' ? new RTDB($config) : new FSDB($config);
2121
});
2222
}
2323

0 commit comments

Comments
 (0)