getAuthToken function
Function that generates a bearer token from username and password This should be used server-side only to get a token for client-side use
Note: This function is excluded from test coverage as it requires actual network connections to Outseta's API. See test/coverage_exclusions/README.md for more information.
Implementation
Future<String> getAuthToken({
required String baseUrl,
required String username,
required String password,
}) async {
final client = http.Client();
try {
final response = await client.post(
Uri.parse('$baseUrl/tokens'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({'username': username, 'password': password}),
);
if (response.statusCode == 200) {
final Map<String, dynamic> data = jsonDecode(response.body);
return data['access_token'];
} else {
throw Exception(
'Failed to get authentication token: ${response.statusCode}',
);
}
} finally {
client.close();
}
}