Adjust MFA factor fetching and verification process
This commit refines the Multi-Factor Authentication (MFA) handling by removing 'requireUser' method, optimizing 'useFetchMfaFactors' hook to avoid fetching stale data, and improving error logging. The changes enhance the system's user session management and the MFA challenge response, ensuring smoother user experience and potential troubleshooting.
This commit is contained in:
@@ -2,7 +2,6 @@ import { redirect } from 'next/navigation';
|
|||||||
|
|
||||||
import { MultiFactorChallengeContainer } from '@kit/auth/mfa';
|
import { MultiFactorChallengeContainer } from '@kit/auth/mfa';
|
||||||
import { checkRequiresMultiFactorAuthentication } from '@kit/supabase/check-requires-mfa';
|
import { checkRequiresMultiFactorAuthentication } from '@kit/supabase/check-requires-mfa';
|
||||||
import { requireUser } from '@kit/supabase/require-user';
|
|
||||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
||||||
|
|
||||||
import pathsConfig from '~/config/paths.config';
|
import pathsConfig from '~/config/paths.config';
|
||||||
@@ -25,6 +24,15 @@ export const generateMetadata = async () => {
|
|||||||
|
|
||||||
async function VerifyPage(props: Props) {
|
async function VerifyPage(props: Props) {
|
||||||
const client = getSupabaseServerComponentClient();
|
const client = getSupabaseServerComponentClient();
|
||||||
|
|
||||||
|
const {
|
||||||
|
data: { user },
|
||||||
|
} = await client.auth.getUser();
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
redirect(pathsConfig.auth.signIn);
|
||||||
|
}
|
||||||
|
|
||||||
const needsMfa = await checkRequiresMultiFactorAuthentication(client);
|
const needsMfa = await checkRequiresMultiFactorAuthentication(client);
|
||||||
|
|
||||||
if (!needsMfa) {
|
if (!needsMfa) {
|
||||||
@@ -32,15 +40,10 @@ async function VerifyPage(props: Props) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const redirectPath = props.searchParams.next ?? pathsConfig.app.home;
|
const redirectPath = props.searchParams.next ?? pathsConfig.app.home;
|
||||||
const auth = await requireUser(client);
|
|
||||||
|
|
||||||
if (auth.error) {
|
|
||||||
redirect(auth.redirectTo);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<MultiFactorChallengeContainer
|
<MultiFactorChallengeContainer
|
||||||
userId={auth.data.id}
|
userId={user.id}
|
||||||
paths={{
|
paths={{
|
||||||
redirectPath,
|
redirectPath,
|
||||||
}}
|
}}
|
||||||
|
|||||||
@@ -292,9 +292,15 @@ function FactorQrCode({
|
|||||||
<FactorNameForm
|
<FactorNameForm
|
||||||
onCancel={onCancel}
|
onCancel={onCancel}
|
||||||
onSetFactorName={async (name) => {
|
onSetFactorName={async (name) => {
|
||||||
const data = await enrollFactorMutation.mutateAsync(name);
|
const data = await enrollFactorMutation
|
||||||
|
.mutateAsync(name)
|
||||||
|
.catch((error) => {
|
||||||
|
console.error(error);
|
||||||
|
|
||||||
if (!data) {
|
return;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (data === undefined) {
|
||||||
return setError(true);
|
return setError(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -171,7 +171,6 @@ export function MultiFactorChallengeContainer({
|
|||||||
|
|
||||||
function useVerifyMFAChallenge() {
|
function useVerifyMFAChallenge() {
|
||||||
const client = useSupabase();
|
const client = useSupabase();
|
||||||
|
|
||||||
const mutationKey = ['mfa-verify-challenge'];
|
const mutationKey = ['mfa-verify-challenge'];
|
||||||
|
|
||||||
const mutationFn = async (params: {
|
const mutationFn = async (params: {
|
||||||
|
|||||||
@@ -20,5 +20,6 @@ export function useFetchAuthFactors(userId: string) {
|
|||||||
return useQuery({
|
return useQuery({
|
||||||
queryKey,
|
queryKey,
|
||||||
queryFn,
|
queryFn,
|
||||||
|
staleTime: 0,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user