#Send post request in loader and based on the result navigate to another page

1 messages · Page 1 of 1 (latest)

umbral wraith
#

Hi, I am trying to use the loader function in order to do complete the authentication process, which involves grabbing tokens by sending a post request to an endpoint with a code that is passed in the search parameters (I do not control this)
My router object is this:

{
          path: '/authenticated',
          loader: async ({ request }) => {
            const url = new URL(request.url)
            const code = url.searchParams.get('code')
            if (code) {
              if (await authContext.AzureSignIn(code)) {
                console.log('redirecting to home, signin success')
                return redirect('/home')
              } else {
                console.log(
                  'redirecting to login, signin failed(await azuresignin)'
                )
                return redirect('/login')
              }
            } else {
              notify('No code found in search params')
              return redirect('/login')
            }
          },
        },

when AzureSignIn(code) returns null then grabbing the tokens failed.
This function executes twice which always results in a 400 code because of the authentication api, which does not accept the same code twice. Why is it being executed twice if there is a redirect?
StrictMode is NOT enabled

AzureSignIn was previously a useCallback, now it is a simple async function

#

^ Here as you can see from the console logs, that the authentication process completed once successfully and now the code is being run again, even though null has been returned