completionId: cgen-47627fcd65e1492788cb700f51a99223
cgen-47627fcd65e1492788cb700f51a99223
This commit is contained in:
parent
a04f1cb786
commit
2e4c958fff
1 changed files with 69 additions and 24 deletions
|
|
@ -108,45 +108,90 @@ export const DiscordActivityProvider: React.FC<
|
||||||
if (!currentUser) {
|
if (!currentUser) {
|
||||||
// User not authenticated, authorize them
|
// User not authenticated, authorize them
|
||||||
console.log("[Discord Activity] Authorizing user...");
|
console.log("[Discord Activity] Authorizing user...");
|
||||||
const { access_token } = await sdk.commands.authorize({
|
const { code } = await sdk.commands.authorize({
|
||||||
scopes: ["identify", "guilds"],
|
client_id: clientId,
|
||||||
|
response_type: "code",
|
||||||
|
state: "",
|
||||||
|
scope: "identify email guilds",
|
||||||
|
prompt: "none",
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(
|
console.log("[Discord Activity] Got authorization code, exchanging for token...");
|
||||||
"[Discord Activity] Got access token, calling activity-auth...",
|
|
||||||
);
|
// Exchange code for access token via our backend
|
||||||
// Exchange access token for user data via our proxy
|
const tokenResponse = await fetch("/api/discord/token", {
|
||||||
const response = await fetch("/api/discord/activity-auth", {
|
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ access_token }),
|
body: JSON.stringify({ code }),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!tokenResponse.ok) {
|
||||||
const errorData = await response.json();
|
const errorData = await tokenResponse.json();
|
||||||
const errMsg = errorData.error || "Failed to authenticate";
|
const errMsg = errorData.error || "Failed to exchange code";
|
||||||
console.error("[Discord Activity] Auth failed:", errMsg);
|
console.error("[Discord Activity] Token exchange failed:", errMsg);
|
||||||
setError(errMsg);
|
setError(errMsg);
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const tokenData = await tokenResponse.json();
|
||||||
if (data.success && data.user) {
|
const access_token = tokenData.access_token;
|
||||||
console.log("[Discord Activity] User authenticated successfully");
|
|
||||||
setUser(data.user);
|
console.log("[Discord Activity] Got access token, authenticating with SDK...");
|
||||||
setError(null);
|
|
||||||
} else {
|
// Authenticate with SDK using the access token
|
||||||
console.error(
|
const authResult = await sdk.commands.authenticate({
|
||||||
"[Discord Activity] Authentication response invalid:",
|
access_token,
|
||||||
data,
|
});
|
||||||
);
|
|
||||||
setError("Authentication failed");
|
if (!authResult) {
|
||||||
|
console.error("[Discord Activity] SDK authentication failed");
|
||||||
|
setError("SDK authentication failed");
|
||||||
|
setIsLoading(false);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log("[Discord Activity] Authenticated with SDK, fetching user profile...");
|
||||||
|
setAuth(authResult);
|
||||||
|
|
||||||
|
// Get user info using the access token
|
||||||
|
const userResponse = await fetch("https://discord.com/api/v10/users/@me", {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${access_token}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!userResponse.ok) {
|
||||||
|
console.error("[Discord Activity] Failed to fetch user profile");
|
||||||
|
setError("Failed to fetch user profile");
|
||||||
|
setIsLoading(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const discordUserData = await userResponse.json();
|
||||||
|
console.log("[Discord Activity] User profile fetched:", discordUserData.username);
|
||||||
|
|
||||||
|
// Store the user data
|
||||||
|
const userData: DiscordUser = {
|
||||||
|
id: discordUserData.id,
|
||||||
|
discord_id: discordUserData.id,
|
||||||
|
full_name: discordUserData.global_name || discordUserData.username,
|
||||||
|
username: discordUserData.username,
|
||||||
|
avatar_url: discordUserData.avatar
|
||||||
|
? `https://cdn.discordapp.com/avatars/${discordUserData.id}/${discordUserData.avatar}.png`
|
||||||
|
: null,
|
||||||
|
bio: null,
|
||||||
|
user_type: "community_member",
|
||||||
|
primary_arm: "labs",
|
||||||
|
};
|
||||||
|
|
||||||
|
setUser(userData);
|
||||||
|
setError(null);
|
||||||
|
console.log("[Discord Activity] User authenticated successfully");
|
||||||
} else {
|
} else {
|
||||||
// User already authenticated, just fetch via our proxy
|
// User already authenticated, just get their info
|
||||||
console.log(
|
console.log(
|
||||||
"[Discord Activity] User already authenticated, fetching user data...",
|
"[Discord Activity] User already authenticated, fetching user data...",
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue