Replies: 4 comments
-
Why is
|
Beta Was this translation helpful? Give feedback.
-
Thanks - this helpful as the official documentation is lacking. My code is pretty much the same as the one you kindly shared, aside probably from the login form. My expectation was that if I use /api endpoint that I could get a JWT token that I can use later to perform more /api calls. The issue for me - when running this code (pretty similar/same as mine) - when calling endpoint /api/auth/callback/credentials I get the following:
I have a custom / very basic login page with inputs for email and password and not much else. So instead of posting json I tried posting x-www-form-urlencoded params and this at least logs the user in, but I still get a HTML page back not token. My expectation is when calling /api that I should get a JWT token. Should I try somehow in the login page to figure out if the caller is using /api route and then issue token or to pass token somehow in HTML? I don't know how would I access Next-Auth's JWT token for the session even if I was to do that... also, how would I then consume that token? Should I use What is the correct pattern with next-auth so I can have HTML login page for humans and /api endpoint for software clients that understand JWT/REST? |
Beta Was this translation helpful? Give feedback.
-
Hi, I’m facing the same issue as mentioned above. I need the login system to work with a React Native (or any cookie-less) environment, but when I send the login request via cURL (POST to /api/auth/callback/credentials), it returns the HTML of the login page instead of the expected response (JWT token which client can store and pass in subsequent requests). Is there a solution for this? |
Beta Was this translation helpful? Give feedback.
-
You need to make sure you are getting a CSRF token string ( Make CSRF request to get token and save cookie: csrf_request=$(curl -s -c next_cookies.txt http://localhost:3000/api/auth/csrf) Get the token string from the returned CSRF_TOKEN=$(echo "$csrf_request" | jq -r .csrfToken) Log in with CSRF token string, CSRF cookie, and credentials to get session token cookie ( curl -s -b next_cookies.txt -c next_cookies.txt \
-H 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode "[email protected]" \
--data-urlencode "password=yoursecretpassword" \
--data-urlencode "csrfToken=$CSRF_TOKEN" \
http://localhost:3000/api/auth/callback/credentials Get current session info using session token cookie: curl -b next_cookies.txt http://localhost:3000/api/auth/session |
Beta Was this translation helpful? Give feedback.
-
I have a nextjs 15 project with api router and auth v5 beta 25 and I am struggling a little with the REST endpoints.
So far, I have setup login page (/login) with email and password and a couple of providers although I use 'credentials' for now. Everything works well if I want to login via the web page.
I wanted to use credentials provider to allow 'devices' to log-on by providing email/password to the same mechanism as the users. The hope was that I could call /api/auth/login and get session/token so I can use this with REST calls from the 'device' client.
So I have setup /api/[...nextauth] as per the guide but when I try to do POST to endpoints I get errors. My router.ts in [...nextauth] is completely vanilla ie
I also have
I want to spare you reading thousands of lines of config files as I feel I may be doing something fundamentally bad. Happy to share configs etc if you feel I am calling endpoints correctly and seeing configs would be useful.
What I tried doing is:
POST json body {email: "[email protected]", password: "plain text"} to /api/auth/login - I get error
[auth][error] UnknownAction: Cannot parse action at /api/auth/login. Read more at https://errors.authjs.dev#unknownaction
GET /api/auth/csrf - this works and I get csrf
I tried POST to /api/auth/signin - but this returns web page with error "missing CSRF" which I guess is not the endpoint I am looking for
I have successfully created 'protected' /api/something routes that can pick up if the user has logged on via the web browser
Do I need to create a custom action for REST login or am I not calling the endpoints correctly or something else??
Beta Was this translation helpful? Give feedback.
All reactions