Replies: 9 comments 2 replies
-
This endpoint returns an HTTP 302 because it needs to be able to redirect a client either to the callback URL (if successful) or to an error page (if not successful), which isn't possible with an HTTP 200 response. It is up to the destination (e.g. the callback URL, or error page) to return an HTTP 200 (or some other status code). Commonly request libraries resolve redirects automatically as their default behaviour which covers cases like this. Is there a particular reason for asking? (e.g. are you trying to handle success/failed responses better on a custom page?) |
Beta Was this translation helpful? Give feedback.
-
^ I ask because we have had recently had people asking about this and I think we have scope to maybe provide some more detailed examples and/or enhanced API behaviour. If you are looking to do a custom sign in page page, note that you can customise both the error page and the sign in page and point both at the same URL (e.g. Using a post submission and customising the error URL is the supported way of doing this right now. If you want to do something a bit fancier, you can also pass const res = await fetch('/api/auth/signin/credentials', {
method: 'post',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: _encodedForm({
...args,
csrfToken: await getCsrfToken(),
callbackUrl: 'http://localhost:3000/signin',
json: true
})
})
const data = await res.json()
const url = data.url The response just contains a URL object currently, but we are considering adding an error object to it for unsuccessful responses, to make it easier to handle errors within JavaScript. We might make some changes to the client API to facilitate this too. |
Beta Was this translation helpful? Give feedback.
-
That is exactly what I was trying to do: handle success/failed responses better on a custom page. Thank you for your response, I think an example project with Custom Credentials and a custom login page would be highly appreciated :) |
Beta Was this translation helpful? Give feedback.
-
@iaincollins I'm also trying to handle the URL error in a custom page to display a customized error message to the user without a page redirection, I did what you suggested in your last comment but it's always returning the same response, for example:
It looks like the fetch request isn't even reaching the credentials provider inside If I use Please any idea how to fix it? Thank you |
Beta Was this translation helpful? Give feedback.
-
@alexandrepaivaa I experienced the same problem and what helped for me was changing the request's url from |
Beta Was this translation helpful? Give feedback.
-
@wkocjan that's working now, thanks!! |
Beta Was this translation helpful? Give feedback.
-
Hello, I am having this exact same issue, which I've re-documented in this discussion: I did add json: true to my POST request, but it still doesn't seem to return any response that I can handle and redirect the user with my SPA's router. Sorry to say, but while I salute the intention of having these authentication systems safer, people really do need credentials login. And of course SPA support. I believe a working example of how we can customize the credentials page so it can create true SPA behavior and not refresh the page is key to the credentials provider. |
Beta Was this translation helpful? Give feedback.
-
I am trying to implement next-auth in next-js. when I try to sing in It gives |
Beta Was this translation helpful? Give feedback.
-
Add
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Your question
/api/auth/callback/credentials returns a 302, shouldn't it return a 200?
What are you trying to do
When trying to signin with Credentials using the call to /api/auth/callback/credentials with a login/password and a csrfToken, the returned response is a 302, why is that? Shouldn't it return a 200?
The session is created which means that the signin did work, still I get a 302 response.
Also, what should the response actually be if successful?
Feedback
I followed the exact same steps in the documentation on here for using a custom signin page:
https://next-auth.js.org/configuration/pages#credentials-sign-in
Thank you !
Beta Was this translation helpful? Give feedback.
All reactions