intercept<BodyType> method
- Chain<
BodyType> chain
override
Implementation
@override
FutureOr<Response<BodyType>> intercept<BodyType>(
Chain<BodyType> chain,
) async {
final clientKey =
clientSecret.isNotEmpty ? '$clientId:$clientSecret' : clientId;
final response = await http.post(
Uri.parse(tokenUrl),
headers: {
'Authorization': 'Basic ${base64Encode(utf8.encode(clientKey))}',
},
body: {
'grant_type': 'password',
'username': username,
'password': password,
'scope': scopes.join(' '),
},
);
final json = jsonDecode(response.body) as Map<String, dynamic>;
final idToken = json['id_token'];
final accessToken = json['access_token'];
if (idToken is! String || accessToken is! String) {
throw ArgumentError('The user could not be logged in. '
'Response from authentication server: ${response.body}');
}
final request = chain.request.copyWith(
headers: {
...chain.request.headers,
'Authorization': 'bearer $accessToken',
},
);
return await chain.proceed(request);
}