mirror of
https://github.com/MetaFam/TheGame.git
synced 2026-01-15 01:17:57 -05:00
Fixed oauth handler / guild insert
This commit is contained in:
committed by
Alec LaLonde
parent
7a86124e97
commit
c080e196af
@@ -1,7 +1,5 @@
|
||||
/* eslint-disable no-console */
|
||||
import {
|
||||
exchangeCodeForAccessToken,
|
||||
getCurrentAuthorization,
|
||||
GuildDiscordMetadata,
|
||||
OAuth2CodeExchangeResponse,
|
||||
PartialGuild,
|
||||
@@ -23,7 +21,7 @@ export const handleOAuthCallback = async (
|
||||
req: Request,
|
||||
res: Response,
|
||||
): Promise<void> => {
|
||||
const { input } = req.body;
|
||||
const { input, session_variables: sessionVariables } = req.body;
|
||||
|
||||
try {
|
||||
// when confirmed, `code` parameter is sent.
|
||||
@@ -79,20 +77,14 @@ export const handleOAuthCallback = async (
|
||||
// Guild doesn't already exist: persist guild info fetched in this request
|
||||
const discordMetadata = parseDiscordMetadata(response.oauthResponse);
|
||||
|
||||
const currentDiscordUser = await getCurrentAuthorization(
|
||||
response.oauthResponse.access_token,
|
||||
);
|
||||
|
||||
const getPlayerResponse = await client.GetPlayerByDiscordId({
|
||||
discordId: currentDiscordUser.user.id,
|
||||
});
|
||||
const playerId = sessionVariables['x-hasura-user-id'];
|
||||
|
||||
let createGuildResponse: DiscordGuildAuthResponse = { success: false };
|
||||
try {
|
||||
createGuildResponse = await createNewGuild(
|
||||
discordGuild,
|
||||
discordMetadata,
|
||||
getPlayerResponse.player[0].id,
|
||||
playerId,
|
||||
);
|
||||
} catch (creationError) {
|
||||
// if there was a guildname clash, try again with a uuid
|
||||
@@ -101,9 +93,11 @@ export const handleOAuthCallback = async (
|
||||
createGuildResponse = await createNewGuild(
|
||||
discordGuild,
|
||||
discordMetadata,
|
||||
getPlayerResponse.player[0].id,
|
||||
playerId,
|
||||
);
|
||||
res.json(createGuildResponse);
|
||||
} else {
|
||||
throw new Error(creationError);
|
||||
}
|
||||
}
|
||||
res.json(createGuildResponse);
|
||||
@@ -141,6 +135,7 @@ const createNewGuild = async (
|
||||
guildname: discordGuild.name.toLowerCase().replace(/[^a-z0-9]/g, ''),
|
||||
discord_id: discordGuild.id,
|
||||
status: GuildStatus_Enum.Pending,
|
||||
position: 'EXTERNAL',
|
||||
};
|
||||
|
||||
if (discordMetadata.logoHash != null) {
|
||||
|
||||
@@ -13,7 +13,6 @@ export const tokenRequestData = {
|
||||
client_secret: CONFIG.discordBotClientSecret,
|
||||
grant_type: 'authorization_code',
|
||||
redirect_uri: `${CONFIG.frontendUrl}/${Constants.DISCORD_OAUTH_CALLBACK_PATH}`,
|
||||
scope: Constants.DISCORD_OAUTH_SCOPES,
|
||||
};
|
||||
|
||||
export const exchangeCodeForAccessToken = async (
|
||||
@@ -24,13 +23,16 @@ export const exchangeCodeForAccessToken = async (
|
||||
code,
|
||||
};
|
||||
|
||||
const discordResponse = await fetch('https://discord.com/api/oauth2/token', {
|
||||
method: 'POST',
|
||||
body: new URLSearchParams(data),
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
const discordResponse = await fetch(
|
||||
'https://discord.com/api/v8/oauth2/token',
|
||||
{
|
||||
method: 'POST',
|
||||
body: new URLSearchParams(data),
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
},
|
||||
});
|
||||
);
|
||||
const response: DiscordAccessTokenResponse = {
|
||||
statusCode: discordResponse.status,
|
||||
};
|
||||
|
||||
@@ -7,14 +7,15 @@ const GuildSetupAuthCallback: React.FC = () => {
|
||||
const router = useRouter();
|
||||
|
||||
const [authGuildRes, authGuild] = useAuthenticateDiscordGuildMutation();
|
||||
|
||||
const [error, setError] = useState<string>('');
|
||||
const [fetching, setFetching] = useState<boolean>(false);
|
||||
|
||||
useEffect(() => {
|
||||
// when auth request is denied, we get `error=access_denied` and `error_description` and `state` parameters
|
||||
const { code, error_description: discordErrorDetail } = router.query;
|
||||
if (discordErrorDetail != null) {
|
||||
setError(discordErrorDetail as string);
|
||||
return;
|
||||
}
|
||||
|
||||
const submitAuthCode = async () => {
|
||||
@@ -32,10 +33,11 @@ const GuildSetupAuthCallback: React.FC = () => {
|
||||
}
|
||||
}
|
||||
};
|
||||
if (!error.length && code) {
|
||||
if (!fetching && code) {
|
||||
setFetching(true);
|
||||
submitAuthCode();
|
||||
}
|
||||
}, [router, authGuild, error]);
|
||||
}, [router, authGuild, error, fetching, setFetching]);
|
||||
|
||||
return (
|
||||
<PageContainer>
|
||||
|
||||
Reference in New Issue
Block a user