mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-20 15:33:12 +09:00
Link Discord from the user page and document the setup
The user page gets a Link Discord button. It asks the controller for a one-time ticket bound to the logged in user, then goes through the authorize route with it. The callback links the Discord account to that user directly, without the in-game code, and goes back to the user page. The link flow always shows the Discord consent screen. The README covers registering the redirect URL and the config. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8e9f780cc2
commit
e4fda18276
6 files changed
+261
-30
No files matched your search
@@ -1,14 +1,25 @@
|
||||
import React, { useContext, useEffect, useState } from "react";
|
||||
import { Button, Descriptions, Popconfirm } from "antd";
|
||||
import { Alert, Button, Descriptions, Popconfirm } from "antd";
|
||||
|
||||
import {
|
||||
ControlContext, PluginExtensionProps, SectionHeader, notifyErrorHandler, useAccount,
|
||||
} from "@clusterio/web_ui";
|
||||
|
||||
import { DiscordLinkGetRequest, DiscordUnlinkRequest } from "../../messages.js";
|
||||
import { DiscordLinkGetRequest, DiscordLinkStartRequest, DiscordUnlinkRequest } from "../../messages.js";
|
||||
|
||||
declare const webRoot: string;
|
||||
|
||||
type Link = { id: string, name: string } | null;
|
||||
|
||||
/** An error from linking on Discord, taken out of the url so a reload does not show it again. */
|
||||
function readError() {
|
||||
const error = new URLSearchParams(window.location.hash.slice(1)).get("exp_discord_error");
|
||||
if (error) {
|
||||
window.history.replaceState(null, "", window.location.pathname + window.location.search);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
/** The Discord account a user is linked to, shown on their user page. */
|
||||
export default function UserDiscordLink(props: PluginExtensionProps<"UserViewPage">) {
|
||||
const control = useContext(ControlContext);
|
||||
@@ -17,6 +28,7 @@ export default function UserDiscordLink(props: PluginExtensionProps<"UserViewPag
|
||||
const canGet = own || account.hasPermission("exp_discord.link.get");
|
||||
const canDelete = own || account.hasPermission("exp_discord.link.delete");
|
||||
const [link, setLink] = useState<Link | undefined>(undefined);
|
||||
const [error, setError] = useState(readError);
|
||||
|
||||
useEffect(() => {
|
||||
if (!canGet) {
|
||||
@@ -36,8 +48,21 @@ export default function UserDiscordLink(props: PluginExtensionProps<"UserViewPag
|
||||
setLink(null);
|
||||
}
|
||||
|
||||
async function startLink() {
|
||||
const ticket = await control.send(new DiscordLinkStartRequest(props.user.name));
|
||||
window.location.href = `${webRoot}api/exp_discord/authorize?link=${encodeURIComponent(ticket)}`;
|
||||
}
|
||||
|
||||
return <>
|
||||
<SectionHeader title="Discord" />
|
||||
{error && <Alert
|
||||
type="error"
|
||||
message={error}
|
||||
showIcon
|
||||
closable
|
||||
onClose={() => setError(null)}
|
||||
style={{ marginBottom: 16 }}
|
||||
/>}
|
||||
<Descriptions bordered size="small" column={1}>
|
||||
<Descriptions.Item label="Linked account">
|
||||
{link ? <>
|
||||
@@ -48,7 +73,14 @@ export default function UserDiscordLink(props: PluginExtensionProps<"UserViewPag
|
||||
>
|
||||
<Button danger size="small" style={{ marginLeft: 8 }}>Unlink</Button>
|
||||
</Popconfirm>}
|
||||
</> : "Not linked"}
|
||||
</> : <>
|
||||
Not linked
|
||||
{own && <Button
|
||||
size="small"
|
||||
style={{ marginLeft: 8 }}
|
||||
onClick={() => { startLink().catch(notifyErrorHandler("Error linking Discord")); }}
|
||||
>Link Discord</Button>}
|
||||
</>}
|
||||
</Descriptions.Item>
|
||||
</Descriptions>
|
||||
</>;
|
||||
|
||||
Reference in new issue
Block a user