mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 19:49:50 +02:00
feat: remove patreon
This commit is contained in:
1 parent
da8025a0ad
commit
0df58d2d2c
13 files changed
+21
-326
No files matched your search
@@ -1,34 +0,0 @@
|
||||
import { assertSecret, type BlitzkitResponse } from '@blitzkit/core';
|
||||
import type { APIContext } from 'astro';
|
||||
|
||||
export const prerender = false;
|
||||
|
||||
export async function GET({ request, params }: APIContext) {
|
||||
const code = request.headers.get('code');
|
||||
|
||||
if (!code) {
|
||||
return Response.json({
|
||||
status: 'error',
|
||||
error: 'UNDEFINED_CODE',
|
||||
} satisfies BlitzkitResponse);
|
||||
}
|
||||
|
||||
const response = await fetch(
|
||||
`https://www.patreon.com/api/oauth2/token?code=${
|
||||
code
|
||||
}&grant_type=authorization_code&client_id=${assertSecret(
|
||||
import.meta.env.PUBLIC_PATREON_CLIENT_ID,
|
||||
)}&client_secret=${assertSecret(
|
||||
import.meta.env.PATREON_CLIENT_SECRET,
|
||||
)}&redirect_uri=${assertSecret(
|
||||
import.meta.env.PUBLIC_PATREON_REDIRECT_URI,
|
||||
)}`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
||||
},
|
||||
);
|
||||
const json = await response.json();
|
||||
|
||||
return Response.json(json);
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
import { assertSecret } from '@blitzkit/core';
|
||||
import type { APIContext } from 'astro';
|
||||
|
||||
export const prerender = false;
|
||||
|
||||
interface PatreonMembershipResponse {
|
||||
data: {
|
||||
attributes: unknown;
|
||||
id: `${number}`;
|
||||
relationships: {
|
||||
memberships: {
|
||||
data: {
|
||||
id: string; // uuid
|
||||
type: 'member';
|
||||
}[];
|
||||
};
|
||||
};
|
||||
type: 'user';
|
||||
};
|
||||
included?: (
|
||||
| {
|
||||
attributes: {
|
||||
patron_status:
|
||||
| 'active_patron'
|
||||
| 'declined_patron'
|
||||
| 'former_patron'
|
||||
| null; // null = never a paid member
|
||||
};
|
||||
id: string; // uuid
|
||||
relationships: {
|
||||
campaign: {
|
||||
data: {
|
||||
id: `${number}`; // id of my campaign to check against
|
||||
type: 'campaign';
|
||||
};
|
||||
links: {
|
||||
related: string; // url
|
||||
};
|
||||
};
|
||||
};
|
||||
type: 'member';
|
||||
}
|
||||
| {
|
||||
attributes: {};
|
||||
id: `${number}`;
|
||||
type: 'campaign'; // ignore this discriminator
|
||||
}
|
||||
)[];
|
||||
links: {
|
||||
self: 'https://www.patreon.com/api/oauth2/v2/user/124201389';
|
||||
};
|
||||
}
|
||||
|
||||
export async function GET({ request }: APIContext) {
|
||||
const token = request.headers.get('token');
|
||||
|
||||
if (!token) return Response.json(false);
|
||||
|
||||
const patreonResponse = await fetch(
|
||||
encodeURI(
|
||||
'https://www.patreon.com/api/oauth2/v2/identity?include=memberships.campaign&fields[member]=patron_status',
|
||||
),
|
||||
{ headers: { Authorization: `Bearer ${token}` } },
|
||||
);
|
||||
|
||||
if (!patreonResponse.ok) return Response.json(false);
|
||||
|
||||
const json = (await patreonResponse.json()) as PatreonMembershipResponse;
|
||||
|
||||
const response = Response.json(
|
||||
json.included?.some(
|
||||
(item) =>
|
||||
item.type === 'member' &&
|
||||
item.relationships.campaign.data.id ===
|
||||
assertSecret(process.env.PATREON_CAMPAIGN_ID) &&
|
||||
item.attributes.patron_status === 'active_patron',
|
||||
) ?? false,
|
||||
);
|
||||
|
||||
response.headers.set('Cache-Control', 'max-age=86400'); // a day
|
||||
|
||||
return response;
|
||||
}
|
||||
@@ -1,24 +1,14 @@
|
||||
import { useEffect } from 'react';
|
||||
import { App } from '../../../stores/app';
|
||||
|
||||
export const AUTH_PROVIDERS = ['wargaming', 'patreon'] as const;
|
||||
export const AUTH_PROVIDERS = ['wargaming'] as const;
|
||||
|
||||
type AuthProvider = (typeof AUTH_PROVIDERS)[number];
|
||||
|
||||
export const AUTH_PROVIDER_NAMES: Record<AuthProvider, string> = {
|
||||
patreon: 'Patreon',
|
||||
wargaming: 'Wargaming',
|
||||
};
|
||||
|
||||
export interface PatreonAuthResponse {
|
||||
access_token: string;
|
||||
expires_in: number;
|
||||
token_type: 'Bearer';
|
||||
scope: string;
|
||||
refresh_token: string;
|
||||
version: string;
|
||||
}
|
||||
|
||||
interface AuthorizeProps {
|
||||
provider: AuthProvider;
|
||||
}
|
||||
@@ -59,25 +49,6 @@ function Content({ provider }: AuthorizeProps) {
|
||||
break;
|
||||
}
|
||||
|
||||
case 'patreon': {
|
||||
if (!searchParams.get('code')) break;
|
||||
|
||||
const response = await fetch('/api/patreon/auth', {
|
||||
headers: { code: searchParams.get('code')! },
|
||||
});
|
||||
const data = (await response.json()) as PatreonAuthResponse;
|
||||
|
||||
mutateApp((draft) => {
|
||||
draft.logins.patreon = {
|
||||
token: data.access_token,
|
||||
refreshToken: data.refresh_token,
|
||||
expires: Date.now() + data.expires_in * 1000,
|
||||
};
|
||||
});
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
throw new Error(`Unknown provider: ${provider}`);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user