next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

Is it possible to avoid page reloading when using custom credentials?

Open danyel117 opened this issue 5 years ago • 17 comments

Your question Is it possible to avoid page reloading when using custom credentials? (like an SPA behaviour)

What are you trying to do I'm using custom credentials because I'm using a customer's database which has its own api, and I've designed my own login page. When I hit login after entering credentials, I handle the authentication with my cusotmer's api inside the [...nextauth].js file. When authentication happens, the page goes to whichever page I've specified in the callback.

However, I would prefer to show a "loading" component and wait until authentication happens, listening the session inside a useEffect, for example. Once the session variable has data, I would then handle the logic without seing a reload on the page.

Reproduction Implement the custom login page in the documentation and watch the redirect after hitting Sign In: import React from 'react'; import { csrfToken } from 'next-auth/client';

export default function SignIn({ csrfToken }) { return (

); }

SignIn.getInitialProps = async (context) => { return { csrfToken: await csrfToken(context), }; };

Feedback Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.

  • [ ] Found the documentation helpful
  • [ ] Found documentation but was incomplete
  • [x] Could not find relevant documentation
  • [ ] Found the example project helpful
  • [x] Did not find the example project helpful

danyel117 avatar Dec 24 '20 02:12 danyel117

I'm looking for that too. It would be interesting if the singIn function had a return response instead of reloading the page, and we had to do something like this:

Promise.reject ('/ login? error = true') The form data is lost and no user interaction occurs.

image

YuriFontella avatar Dec 26 '20 15:12 YuriFontella

Hi there! Could someone tell me, ideally what would you like to be returned? Or would simply don't redirecting anywhere/reloading enough?

balazsorban44 avatar Jan 02 '21 00:01 balazsorban44

Hi all, I think that not reloading the page is not enough. For example, how to deal with errors when normally the errors are passed with the URL? For me it would be best if something is returned with the SignIn() function. For the beginning I think it would be sufficient if two fields are returned:

{
  "status": 200,
  "error": null
}

If the status is 200, the signIn was successful. Everything above 400 is an error.

{
  "status": 401,
  "error": "CredentialsSignin"
}

or

{
  "status": 500,
  "error": "internalServerError"
}

What do the others think about it? Am I missing something? Maybe it would be helpful to return the CallbackUrl as well.

marco-koenen avatar Jan 05 '21 11:01 marco-koenen

I am also looking for a solution for this. Is it possible to achieve this? If I can be pointed to the implementation details, I'd be happy to add this feature.

PS: I am new to the code base

joeynimu avatar Jan 25 '21 15:01 joeynimu

Looking forward to have this enhancement.

I'm using next-auth as a login service then would be nice to handle logic, redirects an error handling at page level when next-auth authorize or fail to sign in the user from a custom login (credentials) form.

marioestradaf avatar Jan 27 '21 13:01 marioestradaf

I'm looking for that too. It would be interesting if the singIn function had a return response instead of reloading the page, and we had to do something like this:

Promise.reject ('/ login? error = true') The form data is lost and no user interaction occurs.

image

nice approach

marioestradaf avatar Jan 27 '21 13:01 marioestradaf

:tada: This issue has been resolved in version 3.3.0-canary.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Feb 01 '21 10:02 github-actions[bot]

setting redirect to false seemed to work for me const onSubmit = async (data,e) => { e.preventDefault() let response = await signIn("credentials",{redirect: false, ...data}); console.log(response); reset(); // SignIntoProvider('EmailPassword', { callbackUrl: "/Dashboard"}) }

My response OK { "error": "CredentialsSignin", "status": 200, "ok": true, "url": null } invalid { "error": "CredentialsSignin", "status": 401, "ok": false, "url": null }

nickforbizz avatar Apr 18 '22 07:04 nickforbizz

thanks guys :) , [nickforbizz] comment work for me

chaboxx avatar Jun 07 '22 23:06 chaboxx

Is there a way to do this with other oauth signin options?

I have a page with images on it that are stored in state. When the user goes to sign in, the page refreshes and images are no longer persisted.

KayBeSee avatar Feb 08 '23 05:02 KayBeSee

Is there a way to do this with other oauth signin options?

I have a page with images on it that are stored in state. When the user goes to sign in, the page refreshes and images are no longer persisted.

I second this

Arcalise08 avatar Jun 14 '23 20:06 Arcalise08

I still get an issue that when I invoke signIn, I have a page refresh (its actually a redirect to the same page). Even when i set { redirect: false } in the signIn call. Any ideas ?

its-kos avatar Dec 05 '23 13:12 its-kos

I still get an issue that when I invoke signIn, I have a page refresh (its actually a redirect to the same page). Even when i set { redirect: false } in the signIn call. Any ideas ?

SAMEEEEEEEEEE

mrsawy avatar Apr 23 '24 03:04 mrsawy

Any solve for signIn with google? Setting redirect: false refreshes the page causing loss of form data.

await signIn("google", { redirect: false })

edgarsbarajas avatar May 21 '24 07:05 edgarsbarajas

Try preventing the default action of the form when submitting.

its-kos avatar May 21 '24 07:05 its-kos

has

I have my code like this and it still does a refresh.

  form.addEventListener('submit', async (e) => {
    e.preventDefault();
    btnSubmit.setAttribute('disabled', 'disabled');

    const formData = new FormData(form);

    const resp = await signIn('credentials', {
      email: formData.get('email') as string,
      password: formData.get('password') as string,
      redirect: false,
    } as any);

    console.log({ resp });

    if (resp) {
      Swal.fire({
        icon: 'error',
        title: 'Error al ingresar',
        text: 'Usuario / Contraseña no son correctos',
      });
      btnSubmit.removeAttribute('disabled');
      return;
    }
  });

danielmoreno3thd avatar Dec 09 '24 02:12 danielmoreno3thd

Hi there! Could someone tell me, ideally what would you like to be returned? Or would simply don't redirecting anywhere/reloading enough?

https://postgresql-1068.github.io/daxter64/

node-csv-parse-agrarian avatar Oct 10 '25 15:10 node-csv-parse-agrarian