init
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { consumeCode } from "supertokens-web-js/recipe/passwordless";
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { fetchMe } from "@/features/players/server";
|
||||
import { useNavigate } from "@tanstack/react-router";
|
||||
import { authQueryConfig } from "@/contexts/auth-context";
|
||||
import toast from '@/lib/sonner'
|
||||
|
||||
const useConsumeCode = (onWrongCode: () => void) => {
|
||||
const navigate = useNavigate();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (code: string) => consumeCode({ userInputCode: code }),
|
||||
onSuccess: async (data) => {
|
||||
if (data.status === 'OK') {
|
||||
const data = await fetchMe();
|
||||
queryClient.setQueryData(authQueryConfig.queryKey, data);
|
||||
if (!data || !data.user) {
|
||||
navigate({ to: '/login', search: { stage: 'name' } });
|
||||
} else {
|
||||
toast.success('Successfully logged in. Welcome back!');
|
||||
navigate({ to: '/' })
|
||||
}
|
||||
} else if (data.status === 'INCORRECT_USER_INPUT_CODE_ERROR') {
|
||||
onWrongCode();
|
||||
} else if (data.status === 'EXPIRED_USER_INPUT_CODE_ERROR') {
|
||||
toast.error('Code has expired. Please request a new code.');
|
||||
} else if (data.status === "RESTART_FLOW_ERROR") {
|
||||
toast.error('Too many failed attempts. Please try again.');
|
||||
navigate({ to: '/login', search: { stage: undefined, number: undefined } });
|
||||
} else {
|
||||
toast.error('Unknown error. Please try again later.');
|
||||
}
|
||||
|
||||
return data;
|
||||
},
|
||||
onError: (error: any) => {
|
||||
if (error.isSuperTokensGeneralError === true) {
|
||||
toast.error(error.message);
|
||||
} else {
|
||||
toast.error("Unknown error. Please try again later.");
|
||||
}
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export default useConsumeCode;
|
||||
Reference in New Issue
Block a user