heroiclabs / nakama-js

JavaScript client for Nakama server written in TypeScript.
https://heroiclabs.com/docs/nakama/client-libraries/javascript/
Apache License 2.0
195 stars 55 forks source link

client.linkEmail returns error code 400 - Bad Request #110

Closed jquentin-lion closed 2 years ago

jquentin-lion commented 2 years ago

If I call await client.linkEmail(session, email, password); It raises an error code 400 - Bad Request. The email is not already in use in another account. authenticateEmail with create=true works. Calling LinkEmailAsync with Unity client on the same server works.

mofirouz commented 2 years ago

Can you please provide the full error details on what comes back from the server? Also what version of the game server and what version of the JS SDK are you using?

jquentin-lion commented 2 years ago

The exception thrown: Response {type: 'cors', url: 'http://serverIP:7350/v2/account/link/email?', redirected: false, status: 400, ok: false, …} body: (...) bodyUsed: false headers: Headers {} ok: false redirected: false status: 400 statusText: "Bad Request" type: "cors" url: "http://serverIP:7350/v2/account/link/email?"

Server version: 3.9.0+6f4c456b nakama-js@2.3.0

mofirouz commented 2 years ago

Can you please open the body and JSON decode and print it?

jquentin-lion commented 2 years ago

Not sure how to do that. I'm getting this when expanding the body: body: ReadableStream locked: false

cancel: ƒ cancel() getReader: ƒ getReader() locked: false pipeThrough: ƒ pipeThrough() pipeTo: ƒ pipeTo() tee: ƒ tee() constructor: ƒ ReadableStream() Symbol(Symbol.toStringTag): "ReadableStream" get locked: ƒ locked()

jquentin-lion commented 2 years ago

So the body's json seems to be: {locked: false} Is that what you wanted?

mofirouz commented 2 years ago

That doesn’t seem like a response error sent by Nakama. Is it possible for you to zip up your project and either email it to me and upload it here in GH?

jquentin-lion commented 2 years ago

Here's a simple sample to demonstrate the issue: https://drive.google.com/file/d/1GbcTz0tqULN2GOZTejMSQyVn76aTkE3M/view?usp=sharing And here it is uploaded to my www for testing: http://jeremyquentin.fr/NakamaTest/

mofirouz commented 2 years ago

Hi @jquentin-lion - Just had a chance to look at the sample code you've sent through; Thanks for the code examples, it was really helpful.

A couple of things:

  1. To read the error body, you should do:
try {
    const session = await client.authenticateEmail(email, password, create, "testusername");
} catch(e) {
    e.text().then(errMsg => console.log("error": errMessage)) // you can also try e.json()...
}
  1. Secondly, your code has an issue with sending Linking request:

Instead of:

await client.linkEmail(session, email, password);

you should:

await client.linkEmail(session, {email: email, password: password});

I've tested this and it works now locally. If this solves the issue, kindly close the issue.

jquentin-lion commented 2 years ago

@mofirouz I see. I'll try this. Thanks. Is there somewhere we can find these function signatures?

mofirouz commented 2 years ago

This will be published soon on the main website documentation, but in the meantime you can find the client api docs here: https://heroiclabs.github.io/nakama-js/classes/Client.html#linkEmail

jquentin-lion commented 2 years ago

@mofirouz That does seem to work! Thanks!

Now there is one other problem: it's that authenticateEmail with create=false throws POST http://Ser.ver.IP:7350/v2/account/authenticate/email?create=false&username=testusername& 404 (Not Found) Even though authenticateEmail is in a try block. It's not blocking because it works when the account is found, but it doesn't sound like an intended behaviour. Should I create a new issue for this, as it's not really related?

mofirouz commented 2 years ago

Sorry a tad bit lost - When an account is not found, and you try to AuthEmail with Create=false, the server returns a 404 and the client returns an error?

If my understanding is correct, the behaviour is correct too (though unsure about 404 vs 401).

Can you clarify what you mean?

jquentin-lion commented 2 years ago

I guess that might be the correct behaviour internally. In that case, the only thing is that the 404 is printed in the browser's console, even if it's caught by a try catch.

mofirouz commented 2 years ago

I think what you are seeing is the browser's default request logger into the console when the console is open. There isn't any printing in the SDK.