Graphql-flutter: How to pass Authorization headers ???

Created on 4 Aug 2018  路  5Comments  路  Source: zino-app/graphql-flutter

After logged in user, I want to store it in flutter local storage and also want to pass to the server as Authorization header, how can I achieve this so the server can validate user data ???

I am using Prisma server as a GraphQL backend, that requires authenticated user jwt passed in Authorization headers :)

question

All 5 comments

Just call the setter apiToken on your client. You can always get the client from the context using the GraphqlProvider.

Client client = GraphqlProvider.of(context).value;

/// to set the authorization header with an api token
client. apiToken = '<YOUR_JWT>';

Hope that helps!

This will surely help. but is it statically named as "apiToken" can I change to Authorization instead.. cause Prisma server will look a key named Authorization and then decode user data inside, will apiToken do the same ???

can you make this more flexible instead, for example:-

Client client = GraphqlProvider.of(context).value;

/// to set the authorization header with an api token
client. apiToken = {
"Authorization": "Bearer ",
"Another_Header_Key": "Another Header Value"
}

so it can give more flexibility on passing data to server :)

Yes this wil actually set the authentication header. So that should work all fine!

This snippet is from the source code:

Map<String, String> get headers => {
  'Authorization': 'Bearer $apiToken',
  'Content-Type': 'application/json',
};

We are currently working on splitting the transport logic into it's own package, allowing you to have even more control over the http headers. For more details checkout PR #36

Thanks, closing since the issue is resolved :)

There is my code first token is Null but after registration i set token in preferance manager but this Configration never Call again so what i can Do next how to recall ?
Please Help !!!!!

  ValueNotifier<GraphQLClient> client = ValueNotifier(
    GraphQLClient(
      link: link,
      cache: OptimisticCache(dataIdFromObject: typenameDataIdFromObject),
    ),
  );

  static AuthLink authLink = AuthLink(
      getToken: () async {
        print("GraphQl Config: ${PreferenceHelper().getAccessToken()}");
        return
          'Bearer ' +  await PreferenceHelper().getAccessToken();
      }
  );

  static Link link = authLink.concat(httpLink);


  GraphQLClient clientToQuery() {
    return GraphQLClient(
      cache: OptimisticCache(dataIdFromObject: typenameDataIdFromObject),
      link: link,
    );
  }
Was this page helpful?
0 / 5 - 0 ratings