completionId: cgen-ed6c365d9b8e404b869193275ec63edf

cgen-ed6c365d9b8e404b869193275ec63edf
This commit is contained in:
Builder.io 2025-11-08 20:59:24 +00:00
parent bc7dd49ff8
commit 294a2920da

View file

@ -59,12 +59,14 @@ export const DiscordActivityProvider: React.FC<DiscordActivityProviderProps> = (
setIsLoading(true); setIsLoading(true);
// Import the Discord SDK dynamically // Import the Discord SDK dynamically
const { DiscordSDKMixin } = await import( const { DiscordSDK } = await import(
"@discord/embedded-app-sdk" "@discord/embedded-app-sdk"
); );
const sdk = new DiscordSDKMixin({ const clientId = import.meta.env.VITE_DISCORD_CLIENT_ID || "578971245454950421";
clientId: import.meta.env.VITE_DISCORD_CLIENT_ID || "578971245454950421",
const sdk = new DiscordSDK({
clientId,
}); });
setDiscordSdk(sdk); setDiscordSdk(sdk);
@ -72,35 +74,64 @@ export const DiscordActivityProvider: React.FC<DiscordActivityProviderProps> = (
// Wait for SDK to be ready // Wait for SDK to be ready
await sdk.ready(); await sdk.ready();
// Authorize with Discord // Get the current user from the SDK
const { access_token } = await sdk.commands.authorize({ const currentUser = await sdk.user.getUser();
client_id: import.meta.env.VITE_DISCORD_CLIENT_ID || "578971245454950421",
response_type: "token",
scope: ["identify", "guilds"],
});
// Exchange access token for user data via our proxy if (!currentUser) {
const response = await fetch("/api/discord/activity-auth", { // User not authenticated, authorize them
method: "POST", const { access_token } = await sdk.commands.authorize({
headers: { scopes: ["identify", "guilds"],
"Content-Type": "application/json", });
},
body: JSON.stringify({ access_token }),
});
if (!response.ok) { // Exchange access token for user data via our proxy
const errorData = await response.json(); const response = await fetch("/api/discord/activity-auth", {
setError(errorData.error || "Failed to authenticate"); method: "POST",
setIsLoading(false); headers: {
return; "Content-Type": "application/json",
} },
body: JSON.stringify({ access_token }),
});
const data = await response.json(); if (!response.ok) {
if (data.success && data.user) { const errorData = await response.json();
setUser(data.user); setError(errorData.error || "Failed to authenticate");
setError(null); setIsLoading(false);
return;
}
const data = await response.json();
if (data.success && data.user) {
setUser(data.user);
setError(null);
} else {
setError("Authentication failed");
}
} else { } else {
setError("Authentication failed"); // User already authenticated, just fetch via our proxy
const response = await fetch("/api/discord/activity-auth", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
access_token: currentUser.access_token || ""
}),
});
if (!response.ok) {
const errorData = await response.json();
setError(errorData.error || "Failed to fetch user data");
setIsLoading(false);
return;
}
const data = await response.json();
if (data.success && data.user) {
setUser(data.user);
setError(null);
} else {
setError("Failed to load user data");
}
} }
} catch (err: any) { } catch (err: any) {
console.error("Discord Activity initialization error:", err); console.error("Discord Activity initialization error:", err);