# Access token created in Flutter is wrong and fails with Invalid Compact JWS

**URL:** <https://community.auth0.com/t/access-token-created-in-flutter-is-wrong-and-fails-with-invalid-compact-jws/103367>\
**Category:** Get Help\
**Tags:** configuration, application\
**Created:** [March 27, 2023, 2:45am UTC](https://community.auth0.com/t/access-token-created-in-flutter-is-wrong-and-fails-with-invalid-compact-jws/103367 "2023-03-27T02:45:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![developers.deve](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/developers.deve/32/45786_2.png) [@developers.deve](https://community.auth0.com/u/developers.deve)\
**Post date:** [March 27, 2023, 2:45am UTC](https://community.auth0.com/t/access-token-created-in-flutter-is-wrong-and-fails-with-invalid-compact-jws/103367/1 "2023-03-27T02:45:56Z")

</div>

Hi! I am developing a mobile app and a web app for my university.

My backend is made in NodeJS and I authenticate my users with this code:

```auto
import { auth } from 'express-oauth2-jwt-bearer';

export const validateAuthorizationMiddleware = auth({
    audience: `https://${process.env.AUTH0_DOMAIN}/api/v2/`,
    issuerBaseURL: `https://${process.env.AUTH0_DOMAIN}/`,
});

```

My web app is made in React and it is in production, so backend and web app work right.

I started to develop my mobile app in Flutter and I have a problem with the access token when I send the token to my backend server, it says ‘Invalid Compact JWS’ (it also happened developing my react app when I use wrong credentials, but I think that it is not the case now).  
I think it happen because I use “demo” scheme, but I don’t know how to use other scheme (I understand that demo is not for production)

This is my AuthProvider

```auto
class AuthProvider extends ChangeNotifier {
  late Auth0 _auth0;
  late String _scheme;

  bool _loading = true;
  Credentials? _credentials;
  UserProfile? _profile;

  AuthProvider(String domain, String clientId, String scheme) {
    _auth0 = Auth0(domain, clientId);
    _scheme = scheme;
    _checkCredentials();
  }

  bool get loading => _loading;
  Credentials? get credentials => _credentials;
  UserProfile? get profile => _profile;
  String get token => _credentials?.accessToken ?? '';
  String get userId => _profile?.sub ?? '';

  void _checkCredentials() async {
    if (await _auth0.credentialsManager.hasValidCredentials()) {
      _credentials = await _auth0.credentialsManager.credentials();
      if (_credentials != null) _retrieveProfile(_credentials!.accessToken);
    }
    _loading = false;
    notifyListeners();
  }

  void login() async {
    _credentials = await _auth0.webAuthentication(scheme: _scheme).login();
    notifyListeners();
  }

  void logout() async {
    await _auth0.webAuthentication(scheme: _scheme).logout();
    _credentials = null;
    notifyListeners();
  }

  void _retrieveProfile(String accessToken) async {
    _profile = await _auth0.api.userProfile(accessToken: accessToken);
  }
}

```

and this is the token that is sent to my backend (it has a different format of the token I have in my web app):

 ![image](https://global.discourse-cdn.com/auth0/original/3X/c/2/c24b5343e606bd555ee43ec0f56bec1f192d309a.png)

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [March 28, 2023, 11:37pm UTC](https://community.auth0.com/t/access-token-created-in-flutter-is-wrong-and-fails-with-invalid-compact-jws/103367/3 "2023-03-28T23:37:44Z")

</div>

Hey there @developers.deve!

Thanks for the detailed description of the issue you’re seeing, very helpful! What do you get if you decode the access token from your flutter app at [jwt.io](http://jwt.io)?

---

<div class="post-metadata">

**Author:** ![developers.deve](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/developers.deve/32/45786_2.png) [@developers.deve](https://community.auth0.com/u/developers.deve)\
**Post date:** [March 28, 2023, 11:53pm UTC](https://community.auth0.com/t/access-token-created-in-flutter-is-wrong-and-fails-with-invalid-compact-jws/103367/4 "2023-03-28T23:53:52Z")

</div>

Thank you for your answer!

It says that payload is not a valid JSON Object (it is an empty string). Then I have the following headers:

```auto
{
  "alg": "dir",
  "enc": "A256GCM",
  "iss": "https://dev-xxx.us.auth0.com/"
}

```

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [March 29, 2023, 12:59am UTC](https://community.auth0.com/t/access-token-created-in-flutter-is-wrong-and-fails-with-invalid-compact-jws/103367/5 "2023-03-29T00:59:43Z")

</div>

Of course, I’m happy to help!

It doesn’t look like you are passing an `audience` param in your Flutter code - If that’s the case then you’ll receive an opaque access token. Some more on the `audience` param and opaque tokens can be found [here](https://community.auth0.com/t/what-is-the-audience/71414).

The audience used in your Flutter app should be the same as the audience specified on your backend - A note on that: It looks like you have the [management API](https://auth0.com/docs/secure/tokens/access-tokens/get-management-api-access-tokens-for-production#get-access-tokens) set as your audience in your backend. I assume you are protecting your own API? In that case the audience should be whatever name (API identifier) you gave your API when [registering it in Auth0](https://auth0.com/docs/get-started/auth0-overview/set-up-apis).

Keep us posted!

---

<div class="post-metadata">

**Author:** ![developers.deve](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/developers.deve/32/45786_2.png) [@developers.deve](https://community.auth0.com/u/developers.deve)\
**Post date:** [March 29, 2023, 1:35am UTC](https://community.auth0.com/t/access-token-created-in-flutter-is-wrong-and-fails-with-invalid-compact-jws/103367/6 "2023-03-29T01:35:44Z")

</div>

You are mi hero! 😄 I found that I can send the audience value when I do the login, now I have a payload in my access token! I was looking for this value in the library contract but I didn’t find it, thanks!

```auto
void login() async {
    _credentials = await _auth0.webAuthentication(scheme: _scheme).login(
      audience: _audience,
    );
    notifyListeners();
  }

```

Thank you for your advice about audience value.

About scheme, should I use demo? I don’t know if I can get another

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [March 29, 2023, 10:47pm UTC](https://community.auth0.com/t/access-token-created-in-flutter-is-wrong-and-fails-with-invalid-compact-jws/103367/7 "2023-03-29T22:47:53Z")

</div>

That’s great news, happy to hear!

Regarding the scheme - I believe you can use whatever you want, we use `demo` because `https` causes issues with Android and deep linking. See:

> **[GitHub - auth0/Auth0.Android: Android toolkit for Auth0 API](https://github.com/auth0/Auth0.Android#a-note-about-app-deep-linking)**
>
> Android toolkit for Auth0 API. Contribute to auth0/Auth0.Android development by creating an account on GitHub.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [April 29, 2023, 12:04am UTC](https://community.auth0.com/t/access-token-created-in-flutter-is-wrong-and-fails-with-invalid-compact-jws/103367/8 "2023-04-29T00:04:38Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
