From 2d2615447e66dceba624f87df6f84882bbdf4c2d Mon Sep 17 00:00:00 2001 From: Mythie Date: Tue, 26 Sep 2023 16:17:01 +1000 Subject: [PATCH] fix: redirectless authentication --- apps/web/src/components/forms/signin.tsx | 47 +++++++++--------------- 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/apps/web/src/components/forms/signin.tsx b/apps/web/src/components/forms/signin.tsx index 2a209ddbe..aed0a4d3d 100644 --- a/apps/web/src/components/forms/signin.tsx +++ b/apps/web/src/components/forms/signin.tsx @@ -1,8 +1,6 @@ 'use client'; -import { useEffect, useState } from 'react'; - -import { useSearchParams } from 'next/navigation'; +import { useState } from 'react'; import { zodResolver } from '@hookform/resolvers/zod'; import { Eye, EyeOff, Loader } from 'lucide-react'; @@ -40,8 +38,6 @@ export type SignInFormProps = { }; export const SignInForm = ({ className }: SignInFormProps) => { - const searchParams = useSearchParams(); - const { toast } = useToast(); const [showPassword, setShowPassword] = useState(false); @@ -57,36 +53,29 @@ export const SignInForm = ({ className }: SignInFormProps) => { resolver: zodResolver(ZSignInFormSchema), }); - const errorCode = searchParams?.get('error'); - - useEffect(() => { - let timeout: NodeJS.Timeout | null = null; - - if (isErrorCode(errorCode)) { - timeout = setTimeout(() => { - toast({ - variant: 'destructive', - description: ERROR_MESSAGES[errorCode] ?? 'An unknown error occurred', - }); - }, 0); - } - - return () => { - if (timeout) { - clearTimeout(timeout); - } - }; - }, [errorCode, toast]); - const onFormSubmit = async ({ email, password }: TSignInFormSchema) => { try { - await signIn('credentials', { + const result = await signIn('credentials', { email, password, callbackUrl: LOGIN_REDIRECT_PATH, - }).catch((err) => { - console.error(err); + redirect: false, }); + + if (result?.error && isErrorCode(result.error)) { + toast({ + variant: 'destructive', + description: ERROR_MESSAGES[result.error], + }); + + return; + } + + if (!result?.url) { + throw new Error('An unknown error occurred'); + } + + window.location.href = result.url; } catch (err) { toast({ title: 'An unknown error occurred',