Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 of 5 tasks
danyel117 opened this issue Dec 24, 2020 · 15 comments · Fixed by #1219
Closed
2 of 5 tasks

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

danyel117 opened this issue Dec 24, 2020 · 15 comments · Fixed by #1219
Assignees
Labels
enhancement New feature or request help needed The maintainer needs help due to time constraint/missing knowledge

Comments

@danyel117
Copy link

danyel117 commented Dec 24, 2020

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 (




Username



Password


Sign in

);
}

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
  • Could not find relevant documentation
  • Found the example project helpful
  • Did not find the example project helpful
@danyel117 danyel117 added the question Ask how to do something or how something works label Dec 24, 2020
@YuriFontella
Copy link

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

@balazsorban44 balazsorban44 self-assigned this Jan 2, 2021
@balazsorban44 balazsorban44 added enhancement New feature or request and removed question Ask how to do something or how something works labels Jan 2, 2021
@balazsorban44
Copy link
Member

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

@marco-koenen
Copy link

marco-koenen commented Jan 5, 2021

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.

@balazsorban44 balazsorban44 removed their assignment Jan 7, 2021
@joeynimu
Copy link

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

@balazsorban44 balazsorban44 added the help needed The maintainer needs help due to time constraint/missing knowledge label Jan 25, 2021
@marioestradaf
Copy link

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
Copy link

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

@github-actions
Copy link

github-actions bot commented Feb 1, 2021

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

The release is available on:

Your semantic-release bot 📦🚀

@nickforbizz
Copy link

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 }

@KayBeSee
Copy link

KayBeSee commented Feb 8, 2023

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.

@Arcalise08
Copy link

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

@its-kos
Copy link

its-kos commented Dec 5, 2023

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 ?

@edgarsbarajas
Copy link

edgarsbarajas commented May 21, 2024

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

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

@its-kos
Copy link

its-kos commented May 21, 2024

Try preventing the default action of the form when submitting.

@aromeronavia

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help needed The maintainer needs help due to time constraint/missing knowledge
Projects
None yet
Development

Successfully merging a pull request may close this issue.

15 participants