# Token Expire Error Flutter

**URL:** <https://forum.social.plus/t/token-expire-error-flutter/774>\
**Category:** General\
**Created:** [June 20, 2023, 11:47pm UTC](https://forum.social.plus/t/token-expire-error-flutter/774 "2023-06-20T23:47:22Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![socialjawn](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@socialjawn](https://forum.social.plus/u/socialjawn)\
**Post date:** [June 20, 2023, 11:47pm UTC](https://forum.social.plus/t/token-expire-error-flutter/774/1 "2023-06-20T23:47:22Z")

</div>

I keep receiving a token expire message when trying to login with flutter.  
I set up secure login. I receive a token from my server. Then I do the following:

```auto
final token = await AmityUserTokenManager(
    endpoint: AmityRegionalHttpEndpoint.US,
    apiKey: SocialJawn.amityApiKey,
  ).createUserToken(
    user.id,
    displayname: user.socialInfo!.username,
    secureToken: data.data,
  );
  final u = await AmityCoreClient.login(user.id)
      .displayName(user.socialInfo!.username)
      .authToken(token.accessToken)
      .submit();

```

Yet I always either receive a token expire message or Parameters validation error! error for the login function as an exception.  
I am using amity\_sdk: ^0.21.0

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [June 21, 2023, 11:25am UTC](https://forum.social.plus/t/token-expire-error-flutter/774/2 "2023-06-21T11:25:42Z")

</div>

Hi @socialjawn ,

Apologies if already implied but just to confirm, you log in immediately after receiving the token correct?

Amity Support

---

<div class="post-metadata">

**Author:** ![socialjawn](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@socialjawn](https://forum.social.plus/u/socialjawn)\
**Post date:** [June 21, 2023, 12:36pm UTC](https://forum.social.plus/t/token-expire-error-flutter/774/3 "2023-06-21T12:36:42Z")

</div>

Yes I long in right after

---

<div class="post-metadata">

**Author:** ![socialjawn](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@socialjawn](https://forum.social.plus/u/socialjawn)\
**Post date:** [June 21, 2023, 12:44pm UTC](https://forum.social.plus/t/token-expire-error-flutter/774/4 "2023-06-21T12:44:34Z")

</div>

@SocialPlus_Support It would be nice to have a full working example we could follow and not just small code snippets.

---

<div class="post-metadata">

**Author:** ![socialjawn](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@socialjawn](https://forum.social.plus/u/socialjawn)\
**Post date:** [June 21, 2023, 3:15pm UTC](https://forum.social.plus/t/token-expire-error-flutter/774/5 "2023-06-21T15:15:15Z")

</div>

@SocialPlus_Support if I turn off secure mode and try to login without the authToken it will log me in.

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [June 27, 2023, 11:46am UTC](https://forum.social.plus/t/token-expire-error-flutter/774/6 "2023-06-27T11:46:46Z")

</div>

Hi @socialjawn ,

Sincere apologies for the delayed response.

The current usage of secured mode is incorrect. The method `createUserToken` is intended for obtaining a user access token, not an authentication token.

To obtain an authentication token, follow these steps:

1. Your front-end (FE) should make a call to retrieve the authentication token.
2. On the back-end (BE) side, invoke the authentication API ([![](https://europe1.discourse-cdn.com/flex013/uploads/amity/original/1X/2be018022476e1091060cd5ca4417f3bd0272e7c.png)Amity API](https://api-docs.amity.co/#/Authentication/get_api_v3_authentication_token) ) using the `userId` obtained in step 1 and the securely stored `serverKey` on the BE side.
3. Pass the token received from the authentication API back to the front-end.
4. Utilize the authentication token for logging in."

Amity Support

---

<div class="post-metadata">

**Author:** ![socialjawn](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@socialjawn](https://forum.social.plus/u/socialjawn)\
**Post date:** [June 27, 2023, 12:38pm UTC](https://forum.social.plus/t/token-expire-error-flutter/774/7 "2023-06-27T12:38:48Z")

</div>

@SocialPlus_Support that is what I am doing

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [June 28, 2023, 9:57am UTC](https://forum.social.plus/t/token-expire-error-flutter/774/8 "2023-06-28T09:57:57Z")

</div>

@socialjawn ,

Can you please help share a screenshot of the error received? Thank you

---

<div class="post-metadata">

**Author:** ![socialjawn](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@socialjawn](https://forum.social.plus/u/socialjawn)\
**Post date:** [July 8, 2023, 3:25am UTC](https://forum.social.plus/t/token-expire-error-flutter/774/9 "2023-07-08T03:25:04Z")

</div>

It has always been the token expired message.

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [July 10, 2023, 11:58am UTC](https://forum.social.plus/t/token-expire-error-flutter/774/10 "2023-07-10T11:58:24Z")

</div>

Hello, could you please double-check your code if you follow the steps we have provided here: [Token Expire Error Flutter - #6 by amitysupport](https://forum.social.plus/t/token-expire-error-flutter/774/6)

If you confirm that you have these precise steps implemented, please help share your current code.

Additionally, please refer to this section for more detail on how to implement with the secure mode on:

- [Security - Amity Docs](https://docs.amity.co/console/settings/security#how-to-use-server-key-to-create-auth-token)

---

<div class="post-metadata">

**Author:** ![socialjawn](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@socialjawn](https://forum.social.plus/u/socialjawn)\
**Post date:** [July 11, 2023, 1:25pm UTC](https://forum.social.plus/t/token-expire-error-flutter/774/11 "2023-07-11T13:25:08Z")

</div>

> [@SocialPlus\_Support](#):
>
> Security - Amity Docs

This is my code to create the token which it does.

```auto
const AMITY_KEY = Deno.env.get('AMITY_KEY')!

serve(async (req) => {
  const { userId } = await req.json()
  
  const request = new Request(`https://api.us.amity.co/api/v4/authentication/token`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'x-server-key': AMITY_KEY
    },
    body: JSON.stringify({
      'userId': userId,
    })
  })

  const response = await fetch(request)
  const data = await response.json()
  console.log(`Response: ${data}`);
  

  return new Response(
    JSON.stringify(data),
    { headers: { "Content-Type": "application/json" } },
  )
})

```

The issue comes when I try to use the code right after it is created in the application.

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [July 11, 2023, 2:54pm UTC](https://forum.social.plus/t/token-expire-error-flutter/774/12 "2023-07-11T14:54:36Z")

</div>

Thank you for providing the code, we’ve passed it to our team to help check.

---

<div class="post-metadata">

**Author:** ![socialjawn](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@socialjawn](https://forum.social.plus/u/socialjawn)\
**Post date:** [July 11, 2023, 3:07pm UTC](https://forum.social.plus/t/token-expire-error-flutter/774/13 "2023-07-11T15:07:26Z")

</div>

@SocialPlus_Support thanks.  
Also it would be nice if the Flutter amity package sdk dependences were updated as with the latest Flutter stable release we can no longer run the application. Mainly the Dio package.

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [July 11, 2023, 3:13pm UTC](https://forum.social.plus/t/token-expire-error-flutter/774/14 "2023-07-11T15:13:07Z")

</div>

Thank you for bringing this to my attention. I have some good news to share – we are actively working on updating the library in our upcoming version, which we are aiming to deploy in the next release 🙂

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [July 11, 2023, 3:29pm UTC](https://forum.social.plus/t/token-expire-error-flutter/774/15 "2023-07-11T15:29:40Z")

</div>

After the team investigated the token issue, they confirmed that it is functioning as expected. However, it is important to ensure that the auth token is provided.

Here’s the sample code:

```auto
import 'package:amity_sdk/amity_sdk.dart';
import 'package:flutter/material.dart';

void main() async {
  runApp(const MyApp());
  await AmityCoreClient.setup(
      option: AmityCoreClientOption(
          apiKey: "",
          httpEndpoint: AmityRegionalHttpEndpoint.SG),
      sycInitialization: true);
  await AmityCoreClient.login("johnwick6")
      .authToken("")
      .submit()
      .then((value) {
    print(value);
  });
}

```

Did you receive any error when you call ` AmityCoreClient.login` ?

---

<div class="post-metadata">

**Author:** ![socialjawn](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@socialjawn](https://forum.social.plus/u/socialjawn)\
**Post date:** [July 11, 2023, 3:57pm UTC](https://forum.social.plus/t/token-expire-error-flutter/774/16 "2023-07-11T15:57:06Z")

</div>

Yes when I provide the auth token I generated it would receive an error that the token has expired. If I turned off secured mode and didn’t use the authToken I was logged right in.

---

<div class="post-metadata">

**Author:** ![SocialPlus\_Support](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.social.plus/socialplus_support/32/798_2.png) [@SocialPlus\_Support](https://forum.social.plus/u/SocialPlus_Support)\
**Post date:** [July 12, 2023, 12:15pm UTC](https://forum.social.plus/t/token-expire-error-flutter/774/17 "2023-07-12T12:15:15Z")

</div>

Hello, could you please verify if your server key is up to date and has not been revoked?

Additionally, please ensure that the user for whom you generated the token is the same user who is attempting to log in.

---

<div class="post-metadata">

**Author:** ![socialjawn](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@socialjawn](https://forum.social.plus/u/socialjawn)\
**Post date:** [July 12, 2023, 12:40pm UTC](https://forum.social.plus/t/token-expire-error-flutter/774/18 "2023-07-12T12:40:41Z")

</div>

@SocialPlus_Support yes the key is the current key. Yes the user is the current user. I am following the instructions correctly.
