#Server Functions Redirects from a Client Component

7 messages · Page 1 of 1 (latest)

glass rain
#

I am having issues with a login component where I call a server function, the login is super simple like this (summary of the code:

'use client'
function LoginComponent() {
   const [state, formAction] = useFormState(LoginAction, { errors: '' })

  return <>
    <form action={formAction}>
      <!-- form here --->
    </form>
  </>

}
'use server'

export async function LoginAction(prevValue: any, formData: FormData) {

  const authResult = await GetAuth(formData);

  if (authResult.requireMFA){
    redirect('/auth/verify/')
  }

  if (authResult.success){
    redirect('/')
  } 

}

This is not working for me and I have seen a number of issues reported on github related to redirect , I am out of ideas but something this simple gives me so much pause about next.

void masonBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

radiant grove
#

What exactly isn't working, also are you checking to make sure both prevValue and formData are what you expect?

glass rain
#

yeah it is valid, I had to do a work around in the client side of doing this essentially:

    const [state, formAction] = useFormState(VerifyAction, { errors: '' })
    const router = useRouter();
    useEffect(() => {

        if (state.success) {
            router.push(`/`);
        }

        if (state.mfaSent) {
            router.push(state.mfaUrl);
        }
    }, [state])
radiant grove
#

I don't get it, so you solved the issue or not?

glass rain
#

I just did it just now

radiant grove
#

Ok