-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdiscuz-jwt-example.php
More file actions
executable file
·65 lines (54 loc) · 2.38 KB
/
Copy pathdiscuz-jwt-example.php
File metadata and controls
executable file
·65 lines (54 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
* Example Discuz integration: generate a JWT for logged-in user and set cookie.
*
* Requirements:
* - install firebase/php-jwt via Composer: `composer require firebase/php-jwt`
* - call this script (or similar logic) after successful Discuz login.
* - set a strong secret in your environment or config and keep it private.
*
* This example sets a cookie named `discuz_token` that the browser will send
* and the client JS will forward to the game server via socket.io auth.
*/
require __DIR__ . '/vendor/autoload.php';
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
// Replace with your secret and choose a secure storage for it
$secret = getenv('JWT_SECRET') ?: 'change_this_in_production';
// Example: get Discuz user info after login. Replace with actual Discuz user variables.
// For demonstration we assume $uid and $username are available after login.
$uid = isset($uid) ? intval($uid) : (isset($_SESSION['uid']) ? intval($_SESSION['uid']) : 0);
$username = isset($username) ? $username : (isset($_SESSION['username']) ? $_SESSION['username'] : 'guest');
if (!$uid) {
http_response_code(403);
echo 'Not logged in';
exit;
}
$issuedAt = time();
$expire = $issuedAt + 60 * 60 * 24; // token valid for 24 hours; tune as needed
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'] ?? '';
$avatarUrl = $host ? ($scheme . '://' . $host . '/uc_server/avatar.php?uid=' . rawurlencode((string)$uid) . '&size=middle') : '';
$payload = [
'uid' => $uid,
'username' => $username,
'avatarUrl' => $avatarUrl,
'iat' => $issuedAt,
'exp' => $expire,
// you can add more custom claims if needed
];
$jwt = JWT::encode($payload, $secret, 'HS256');
// Set cookie for the frontend. Adjust domain and secure flags for production.
setcookie('discuz_token', $jwt, [
'expires' => $expire,
'path' => '/',
'domain' => '', // set your domain if needed: '.example.com'
'secure' => isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off',
'httponly' => false, // must be accessible by JS so set false
'samesite' => 'Lax'
]);
// You can redirect back to the app or return JSON
header('Content-Type: application/json; charset=utf-8');
echo json_encode(['success' => true, 'token_set' => true]);
// Example usage: include or call this code after Discuz login hook so the cookie is present for the browser.
?>