httpLinkForNhost function
Link
httpLinkForNhost(})
Creates an HTTP link that configures automatically based on nhostAuth
's
authentication state.
defaultHeaders
(optional) A set of headers that will be provided with
all requests passing through the link.
httpClientOverride
(optional) can be provided to customize the network
request, such as to configure proxies, introduce interceptors, etc.
Implementation
Link httpLinkForNhost(
String nhostGqlEndpointUrl,
Auth nhostAuth, {
http.Client? httpClientOverride,
Map<String, String>? defaultHeaders = const {},
}) {
final unauthenticatedLink = HttpLink(
nhostGqlEndpointUrl,
httpClient: httpClientOverride,
defaultHeaders: defaultHeaders ?? const {},
);
// Introduce an Authorization header
final addAuthenticationLink = Link.function((request, [forward]) {
if (nhostAuth.authenticationState == AuthenticationState.loggedIn) {
request = request.updateContextEntry<HttpLinkHeaders>(
(entry) => HttpLinkHeaders(
headers: {
...?entry?.headers,
'Authorization': 'Bearer ${nhostAuth.jwt}',
},
),
);
}
return forward!(request);
});
return Link.concat(addAuthenticationLink, unauthenticatedLink);
}