Skip to content

Commit 1b46724

Browse files
committed
update
1 parent 4bdf770 commit 1b46724

9 files changed

Lines changed: 249 additions & 35 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
vendor

api.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

auth.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
session_start();
3+
4+
header('Content-Type: application/json');
5+
header('Access-Control-Allow-Origin: *');
6+
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
7+
header('Access-Control-Allow-Headers: Content-Type, Authorization');
8+
9+
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
10+
http_response_code(200);
11+
exit;
12+
}
13+
14+
// if (!isset($_SESSION['user_id'])) {
15+
// http_response_code(403);
16+
// echo json_encode(['error' => 'Unauthorized']);
17+
// exit;
18+
// }
19+
20+
$secretApiKey = 'sk_dev_4oLJSBeKQJF8n3tCiEnqkC8f9mMM2gBhlIMIVZImq98FqiTNa_-SIsps6EMaQuG0';
21+
$userId = $_GET['user_id'];
22+
$roomId = $_POST['room_id'] ?? 'my-room';
23+
24+
$payload = [
25+
'userId' => $userId,
26+
'room' => $roomId,
27+
];
28+
29+
$signature = hash_hmac('sha256', json_encode(value: $payload), $secretApiKey);
30+
31+
echo json_encode([
32+
'userId' => $userId,
33+
'room' => $roomId,
34+
'token' => $signature,
35+
]);

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"firebase/php-jwt": "^6.10"
4+
}
5+
}

composer.lock

Lines changed: 82 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<title>Liveblocks Auth</title>
7-
<script type="module" src="/room.js"></script>
87
</head>
98
<body>
109
<div>
1110
<p id="status">Not connected</p>
1211
<button id="connect">Connect to Room</button>
1312
</div>
1413

15-
<script>
14+
<!-- Import the room.js file -->
15+
<script type="module">
16+
import { client } from './room.js';
17+
1618
document.getElementById('connect').addEventListener('click', () => {
17-
const { room, leave } = window.client.enterRoom("my-room");
19+
const { room, leave } = client.enterRoom("my-room");
1820

1921
room.subscribe("others", (others) => {
2022
document.getElementById('status').innerText = `There are ${others.length} other user(s) online`;

package-lock.json

Lines changed: 114 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
"author": "",
1313
"license": "ISC",
1414
"dependencies": {
15-
"@liveblocks/client": "^2.7.2"
15+
"@liveblocks/client": "^2.7.2",
16+
"node-fetch": "^3.3.2",
17+
"ws": "^8.18.0"
1618
},
1719
"devDependencies": {
1820
"vite": "^5.4.7"
1921
}
20-
}
22+
}

room.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { createClient } from "@liveblocks/client";
22

33
const client = createClient({
4-
publicApiKey: "pk_dev_4hBTxoM5Vkir7gRMcIEP99BTDpqay7jjxcG9ELGa4fomzhWfbzkWgOh4qbU4Rhup",
5-
authEndpoint: "http://localhost/auth.php",
4+
// publicApiKey: "pk_dev_4hBTxoM5Vkir7gRMcIEP99BTDpqay7jjxcG9ELGa4fomzhWfbzkWgOh4qbU4Rhup",
5+
authEndpoint: "http://localhost:8080/auth.php?user_id=1",
66
});
77

8-
export const { room, leave } = client.enterRoom("my-room");
8+
export { client };

0 commit comments

Comments
 (0)