File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ ]);
Original file line number Diff line number Diff line change 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" ) ;
Original file line number Diff line number Diff line change 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+ ]);
You can’t perform that action at this time.
0 commit comments