heroiclabs / nakama-dart

Pure Dart Client for Nakama Server
https://heroiclabs.com/docs/nakama/client-libraries/dart
182 stars 55 forks source link

Authentication issues #73

Closed Dragon863 closed 11 months ago

Dragon863 commented 1 year ago

Hi there! Thanks for the useful package, unfortunately I'm running into two issues. Firstly, the timestamps for the session expiry seem to be reported incorrectly (at some point in 1970), I believe this is because in session.dart fromMillisecondsSinceEpoch is being called on the decoded token which stores the token in seconds since epoch. Whilst this can be worked around by changing the arguments to (token['exp'] as int) * 1000 in every line where this function is called in the file, I am still getting the message "Auth token invalid" when using any function with the session object (for example addFriends). I'm not sure if this is an issue with the package or the server I'm running, but I thought it could be relevant or maybe caused by the first issue.

I used the official docs and logged using email and then attempted to add a friend to the account using the following:

session = await client.authenticateEmail(
    username: "example",
    password: "example",
);
print(session.expiresAt);
await client.addFriends(
    usernames: ["test"],
    session: session,
);

This gives the following output:

flutter: 1970-01-20 15:29:28.873
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: gRPC Error (code: 16, codeName: UNAUTHENTICATED, message: Auth token invalid, details: [], rawResponse: null, trailers: {})

After applying the fix I mentioned above the session is reported as expiring at the correct date, but the second error still occurs.

I am using flutter 3.13.0 and the latest version of this ibrary on pub.dev, and the latest docker image of nakama. Thank you in advance!

obrunsmann commented 1 year ago

Thank you @Dragon863 for reporting. I am just back from vacation and checking in your issue shortly.

LucaJeevanjee commented 1 year ago

Hi I am getting the same! Auth token always invalid. Any progress?

taciomedeiros commented 1 year ago

This error is occuring because there is no token sended by the client. There is an options parameter in the methods that make GRPC calls and a method to prepare session as token

Captura de Tela 2023-11-12 às 09 03 38

Just add this line on the methods that you want

ilmalte commented 1 year ago

Bump :)

edit: I was facing the same issue while authenticating and using the token for creating a group. I confirm @taciomedeiros solution helped me fix this issue.

in session.dart

    expiresAt: DateTime.fromMillisecondsSinceEpoch(
        (token['exp'] as int) * 1000,
    ),
    refreshExpiresAt: DateTime.fromMillisecondsSinceEpoch(
    (refreshToken['exp'] as int) * 1000,
    ),

in nakama_grpc_client.dart

      final res = await _client.createGroup(api.CreateGroupRequest(
      name: name,
      avatarUrl: avatarUrl,
      description: description,
      langTag: langTag,
      maxCount: maxCount,
      open: open,
    ),
    options: _getSessionCallOptions(session),
  );
obrunsmann commented 11 months ago

Token parsing issue and missing options fixed in next release (releasing today)