Cleanup
This commit is contained in:
48
apps/web/app/auth/callback/error/page.tsx
Normal file
48
apps/web/app/auth/callback/error/page.tsx
Normal file
@@ -0,0 +1,48 @@
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
import { Alert, AlertDescription, AlertTitle } from '@kit/ui/alert';
|
||||
import { Button } from '@kit/ui/button';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
interface Params {
|
||||
searchParams: {
|
||||
error: string;
|
||||
};
|
||||
}
|
||||
|
||||
function AuthCallbackErrorPage({ searchParams }: Params) {
|
||||
const { error } = searchParams;
|
||||
|
||||
// if there is no error, redirect the user to the sign-in page
|
||||
if (!error) {
|
||||
redirect('/auth/sign-in');
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={'flex flex-col space-y-4 py-4'}>
|
||||
<div>
|
||||
<Alert variant={'destructive'}>
|
||||
<AlertTitle>
|
||||
<Trans i18nKey={'auth:authenticationErrorAlertHeading'} />
|
||||
</AlertTitle>
|
||||
|
||||
<AlertDescription>
|
||||
<Trans i18nKey={error} />
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
</div>
|
||||
|
||||
<ResendLinkForm />
|
||||
|
||||
<div className={'flex flex-col space-y-2'}>
|
||||
<Button variant={'ghost'}>
|
||||
<a href={'/auth/sign-in'}>
|
||||
<Trans i18nKey={'auth:signIn'} />
|
||||
</a>
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default AuthCallbackErrorPage;
|
||||
143
apps/web/app/auth/callback/route.ts
Normal file
143
apps/web/app/auth/callback/route.ts
Normal file
@@ -0,0 +1,143 @@
|
||||
import { redirect } from 'next/navigation';
|
||||
import type { NextRequest } from 'next/server';
|
||||
|
||||
import pathsConfig from '~/config/paths.config';
|
||||
|
||||
import { Logger } from '@kit/shared/logger';
|
||||
import { getSupabaseRouteHandlerClient } from '@kit/supabase/route-handler-client';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const requestUrl = new URL(request.url);
|
||||
const searchParams = requestUrl.searchParams;
|
||||
|
||||
const authCode = searchParams.get('code');
|
||||
const inviteCode = searchParams.get('inviteCode');
|
||||
const error = searchParams.get('error');
|
||||
const nextUrl = searchParams.get('next') ?? pathsConfig.app.home;
|
||||
|
||||
let userId: string | undefined = undefined;
|
||||
|
||||
if (authCode) {
|
||||
const client = getSupabaseRouteHandlerClient();
|
||||
|
||||
try {
|
||||
const { error, data } =
|
||||
await client.auth.exchangeCodeForSession(authCode);
|
||||
|
||||
// if we have an error, we redirect to the error page
|
||||
if (error) {
|
||||
return onError({ error: error.message });
|
||||
}
|
||||
|
||||
userId = data.user.id;
|
||||
} catch (error) {
|
||||
Logger.error(
|
||||
{
|
||||
error,
|
||||
},
|
||||
`An error occurred while exchanging code for session`,
|
||||
);
|
||||
|
||||
const message = error instanceof Error ? error.message : error;
|
||||
|
||||
return onError({ error: message as string });
|
||||
}
|
||||
|
||||
if (inviteCode && userId) {
|
||||
try {
|
||||
Logger.info(
|
||||
{
|
||||
userId,
|
||||
inviteCode,
|
||||
},
|
||||
`Attempting to accept user invite...`,
|
||||
);
|
||||
|
||||
// if we have an invite code, we accept the invite
|
||||
await acceptInviteFromEmailLink({ inviteCode, userId });
|
||||
} catch (error) {
|
||||
Logger.error(
|
||||
{
|
||||
userId,
|
||||
inviteCode,
|
||||
error,
|
||||
},
|
||||
`An error occurred while accepting user invite`,
|
||||
);
|
||||
|
||||
const message = error instanceof Error ? error.message : error;
|
||||
|
||||
return onError({ error: message as string });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return onError({ error });
|
||||
}
|
||||
|
||||
return redirect(nextUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name acceptInviteFromEmailLink
|
||||
* @description If we find an invite code, we try to accept the invite
|
||||
* received from the email link method
|
||||
* @param params
|
||||
*/
|
||||
async function acceptInviteFromEmailLink(params: {
|
||||
inviteCode: string;
|
||||
userId: string | undefined;
|
||||
}) {
|
||||
if (!params.userId) {
|
||||
Logger.error(params, `Attempted to accept invite, but no user id provided`);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Logger.info(params, `Found invite code. Accepting invite...`);
|
||||
|
||||
await acceptInviteToOrganization(
|
||||
getSupabaseRouteHandlerClient({
|
||||
admin: true,
|
||||
}),
|
||||
{
|
||||
code: params.inviteCode,
|
||||
userId: params.userId,
|
||||
},
|
||||
);
|
||||
|
||||
Logger.info(params, `Invite successfully accepted`);
|
||||
}
|
||||
|
||||
function onError({ error }: { error: string }) {
|
||||
const errorMessage = getAuthErrorMessage(error);
|
||||
|
||||
Logger.error(
|
||||
{
|
||||
error,
|
||||
},
|
||||
`An error occurred while signing user in`,
|
||||
);
|
||||
|
||||
const redirectUrl = `/auth/callback/error?error=${errorMessage}`;
|
||||
|
||||
return redirect(redirectUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given error message indicates a verifier error.
|
||||
* We check for this specific error because it's highly likely that the
|
||||
* user is trying to sign in using a different browser than the one they
|
||||
* used to request the sign in link. This is a common mistake, so we
|
||||
* want to provide a helpful error message.
|
||||
*/
|
||||
function isVerifierError(error: string) {
|
||||
return error.includes('both auth code and code verifier should be non-empty');
|
||||
}
|
||||
|
||||
function getAuthErrorMessage(error: string) {
|
||||
return isVerifierError(error)
|
||||
? `auth:errors.codeVerifierMismatch`
|
||||
: `auth:authenticationErrorAlertBody`;
|
||||
}
|
||||
Reference in New Issue
Block a user