Skip to content

Conversation

@firede
Copy link
Contributor

@firede firede commented Aug 20, 2020

Issue

const res = await fetch("/api/auth/signin/email", {
  method: "post",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ ...values, json: true /* NOTE */ }),
})
console.log(res)

NOTE

  • json: true triggers a 302 redirect
  • json: "true" reponse as JSON

Expected

  • json: true reponse as JSON

@vercel
Copy link

vercel bot commented Aug 20, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/nextauthjs/next-auth/wr3efg6j3
✅ Preview: https://next-auth-git-fork-firede-fix-redirect-reponse-as-json.nextauthjs.vercel.app

@vercel vercel bot temporarily deployed to Preview August 20, 2020 13:02 Inactive
@iaincollins
Copy link
Member

iaincollins commented Aug 20, 2020

Thanks for the submission!

The current behaviour is correct as the form is application/x-www-form-urlencoded rather than anticipating JSON because it supports clients that do not have JavaScript enabled.

We could introduce the option to support JSON as well, but there would need to be quite a bit of regression testing, because there are other parts of the flow that would break, as the expected submission is currently always application/x-www-form-urlencoded.

The easiest way to submit a sign in form, is with the sign in client method, which handles both form encoding and CSRF token submission.

import { signIn } from 'next-auth/client'

export default ({ email }) => (
  <button onClick={() => signIn('email', { email })}>Sign in with Email</button>
)

@firede
Copy link
Contributor Author

firede commented Aug 20, 2020

@iaincollins Thanks for the explanation!

Related case: #541

I am try to implement it on the same page:

  1. Send verification email
  2. Paste verification code
  3. Verify and login

The signIn API will redirect to the verification request page by default, which cannot satisfy the above interaction.
Do you have any suggestions?

@firede firede closed this Aug 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants