Files
blitzkit/packages/website/src/pages/[...locale]/auth/[provider]/_index.tsx
T

61 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-10-09 19:40:55 -05:00
import { useEffect } from 'react';
2024-12-27 12:38:38 -06:00
import { App } from '../../../../stores/app';
2024-10-09 19:40:55 -05:00
2024-10-18 10:29:58 -05:00
export const AUTH_PROVIDERS = ['wargaming'] as const;
2024-10-09 19:40:55 -05:00
2025-01-14 19:22:41 -06:00
export type AuthProvider = (typeof AUTH_PROVIDERS)[number];
2024-10-09 19:40:55 -05:00
export const AUTH_PROVIDER_NAMES: Record<AuthProvider, string> = {
wargaming: 'Wargaming',
};
interface AuthorizeProps {
provider: AuthProvider;
}
2024-10-10 15:23:30 -05:00
export function Authorize(props: AuthorizeProps) {
return (
<App.Provider>
<Content {...props} />
</App.Provider>
);
}
function Content({ provider }: AuthorizeProps) {
2024-10-09 21:04:49 -05:00
const mutateApp = App.useMutation();
2024-10-09 19:40:55 -05:00
useEffect(() => {
(async () => {
const searchParams = new URLSearchParams(window.location.search);
switch (provider) {
case 'wargaming': {
if (
!searchParams.has('expires_at') ||
!searchParams.has('access_token') ||
!searchParams.has('account_id')
) {
break;
}
2024-10-09 21:09:59 -05:00
mutateApp((draft) => {
draft.logins.wargaming = {
expires: Number(searchParams.get('expires_at')) * 1000,
token: searchParams.get('access_token')!,
id: Number(searchParams.get('account_id'))!,
};
2024-10-09 19:40:55 -05:00
});
break;
}
default:
throw new Error(`Unknown provider: ${provider}`);
}
window.location.href = searchParams.get('return') ?? '/';
})();
}, []);
return null;
}