Skip to content

Commit 97d167d

Browse files
committed
init
1 parent f1c7e38 commit 97d167d

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

api.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
session_start();
3+
header('Content-Type: application/json');
4+
5+
if (!isset($_SESSION['user_id'])) {
6+
http_response_code(403);
7+
echo json_encode(['error' => 'Unauthorized']);
8+
exit;
9+
}
10+
11+
$secretApiKey = 'sk_dev_4oLJSBeKQJF8n3tCiEnqkC8f9mMM2gBhlIMIVZImq98FqiTNa_-SIsps6EMaQuG0';
12+
$userId = $_SESSION['user_id'];
13+
$roomId = $_POST['room_id'] ?? 'my-room';
14+
15+
$payload = [
16+
'userId' => $userId,
17+
'room' => $roomId,
18+
];
19+
20+
$signature = hash_hmac('sha256', json_encode($payload), $secretApiKey);
21+
22+
echo json_encode([
23+
'userId' => $userId,
24+
'room' => $roomId,
25+
'token' => $signature,
26+
]);

room.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createClient } from "@liveblocks/client";
2+
3+
const client = createClient({
4+
publicApiKey: "pk_dev_4hBTxoM5Vkir7gRMcIEP99BTDpqay7jjxcG9ELGa4fomzhWfbzkWgOh4qbU4Rhup",
5+
authEndpoint: "/auth.php",
6+
});
7+
8+
export const { room, leave } = client.enterRoom("my-room");

test.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
header('Content-Type: application/json');
3+
4+
// Your secret API key from Liveblocks dashboard (not the public key)
5+
$secretApiKey = 'sk_prod_your_secret_api_key';
6+
7+
// Fetch the user's credentials (use a session, database, etc. to manage users)
8+
$userId = 'user-123'; // Replace this with your actual user authentication logic
9+
$roomId = 'my-room'; // The room the user is trying to access
10+
11+
if (!$userId || !$roomId) {
12+
// If there's no user or room specified, deny access
13+
http_response_code(403);
14+
echo json_encode(['error' => 'Unauthorized']);
15+
exit;
16+
}
17+
18+
// Prepare the payload for signing
19+
$payload = [
20+
'userId' => $userId,
21+
'room' => $roomId,
22+
];
23+
24+
// Generate the signature using HMAC with SHA256
25+
$signature = hash_hmac('sha256', json_encode($payload), $secretApiKey);
26+
27+
// Send the signed response back to the client
28+
echo json_encode([
29+
'userId' => $userId,
30+
'room' => $roomId,
31+
'token' => $signature,
32+
]);

0 commit comments

Comments
 (0)