43 lines
1.5 KiB
TypeScript
43 lines
1.5 KiB
TypeScript
import { createFileRoute } from '@tanstack/react-router';
|
|
import { zodValidator } from '@tanstack/zod-adapter';
|
|
import z from 'zod';
|
|
import { LoginForm } from '@/components/login-form';
|
|
import { generateOAuthState } from '@/lib/random';
|
|
import { getToken } from '@/lib/token';
|
|
|
|
const authorizeSchema = z.object({
|
|
response_type: z.literal('code').default('code'),
|
|
client_id: z.literal('org_client').default('org_client'),
|
|
redirect_uri: z.string().default(`${new URL(import.meta.env.VITE_APP_BASE_URL as string).toString()}token`),
|
|
state: z.string().default(generateOAuthState()),
|
|
});
|
|
|
|
export type AuthorizeSearchParams = z.infer<typeof authorizeSchema>;
|
|
|
|
export const Route = createFileRoute('/authorize')({
|
|
component: RouteComponent,
|
|
validateSearch: zodValidator(authorizeSchema),
|
|
});
|
|
|
|
function RouteComponent() {
|
|
const token = getToken();
|
|
const oauthParams = Route.useSearch();
|
|
if (token !== null) {
|
|
const base = new URL(window.location.origin);
|
|
const url = new URL('/api/v1/auth/redirect', base);
|
|
url.searchParams.set('client_id', oauthParams.client_id);
|
|
url.searchParams.set('response_type', oauthParams.response_type);
|
|
url.searchParams.set('redirect_uri', oauthParams.redirect_uri);
|
|
url.searchParams.set('state', oauthParams.state);
|
|
window.location.href = url.toString();
|
|
return null;
|
|
}
|
|
return (
|
|
<div className="bg-background flex min-h-svh flex-col items-center justify-center gap-6 p-6 md:p-10">
|
|
<div className="w-full max-w-sm">
|
|
<LoginForm oauthParams={oauthParams} />
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|