mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-04-24 19:39:11 +00:00
fix(api): define auth token type as enum, export userAgent and use when discordFetch isn't
This commit is contained in:
parent
e1120fd88a
commit
041fe49b05
3 changed files with 19 additions and 10 deletions
|
@ -6,10 +6,12 @@ import {
|
|||
SessionData,
|
||||
} from '../../common/types';
|
||||
import {
|
||||
AuthType,
|
||||
discordFetch,
|
||||
formData,
|
||||
parsePermissions,
|
||||
resolveFailures,
|
||||
userAgent,
|
||||
} from '../utils/api-tools';
|
||||
import { Bounce } from '../utils/bounce';
|
||||
import { apiPublicURI, botClientID, botClientSecret, uiPublicURI } from '../utils/config';
|
||||
|
@ -62,6 +64,7 @@ export const LoginCallback = resolveFailures(
|
|||
method: 'POST',
|
||||
headers: {
|
||||
'content-type': 'application/x-www-form-urlencoded',
|
||||
'user-agent': userAgent,
|
||||
},
|
||||
body: formData(tokenRequest),
|
||||
});
|
||||
|
@ -98,7 +101,11 @@ export const LoginCallback = resolveFailures(
|
|||
);
|
||||
|
||||
const getUser = async (accessToken: string): Promise<DiscordUser | null> => {
|
||||
const user = await discordFetch<DiscordUser>('/users/@me', accessToken, 'Bearer');
|
||||
const user = await discordFetch<DiscordUser>(
|
||||
'/users/@me',
|
||||
accessToken,
|
||||
AuthType.Bearer
|
||||
);
|
||||
|
||||
if (!user) {
|
||||
return null;
|
||||
|
@ -122,7 +129,7 @@ const getGuilds = async (accessToken: string) => {
|
|||
const guilds = await discordFetch<UserGuildsPayload>(
|
||||
'/users/@me/guilds',
|
||||
accessToken,
|
||||
'Bearer'
|
||||
AuthType.Bearer
|
||||
);
|
||||
|
||||
if (!guilds) {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { SessionData } from 'roleypoly/common/types';
|
||||
import { formData, respond, withSession } from '../utils/api-tools';
|
||||
import { formData, respond, userAgent, withSession } from '../utils/api-tools';
|
||||
import { botClientID, botClientSecret } from '../utils/config';
|
||||
import { Sessions } from '../utils/kv';
|
||||
|
||||
|
@ -15,6 +15,7 @@ export const RevokeSession = withSession(
|
|||
method: 'POST',
|
||||
headers: {
|
||||
'content-type': 'application/x-www-form-urlencoded',
|
||||
'user-agent': userAgent,
|
||||
},
|
||||
body: formData(tokenRequest),
|
||||
});
|
||||
|
|
|
@ -58,17 +58,22 @@ export const getSessionID = (request: Request): { type: string; id: string } | n
|
|||
return { type, id };
|
||||
};
|
||||
|
||||
const userAgent =
|
||||
export const userAgent =
|
||||
'DiscordBot (https://github.com/roleypoly/roleypoly, git-main) (+https://roleypoly.com)';
|
||||
|
||||
export enum AuthType {
|
||||
Bearer = 'Bearer',
|
||||
Bot = 'Bot',
|
||||
}
|
||||
|
||||
export const discordFetch = async <T>(
|
||||
url: string,
|
||||
auth: string,
|
||||
authType: 'Bearer' | 'Bot' = 'Bearer'
|
||||
authType: AuthType = AuthType.Bearer
|
||||
): Promise<T | null> => {
|
||||
const response = await fetch('https://discord.com/api/v8' + url, {
|
||||
headers: {
|
||||
authorization: `${authType} ${auth}`,
|
||||
authorization: `${AuthType[authType]} ${auth}`,
|
||||
'user-agent': userAgent,
|
||||
},
|
||||
});
|
||||
|
@ -116,10 +121,6 @@ const NotAuthenticated = (extra?: string) =>
|
|||
{ status: 403 }
|
||||
);
|
||||
|
||||
type WithSessionOpts = {
|
||||
mustAuthenticate?: boolean;
|
||||
};
|
||||
|
||||
export const withSession = (
|
||||
wrappedHandler: (session: SessionData) => Handler
|
||||
): Handler => async (request: Request): Promise<Response> => {
|
||||
|
|
Loading…
Add table
Reference in a new issue