fetchCredentials method
Get credentials for PowerSync.
This should always fetch a fresh set of credentials - don't use cached values.
Return null if the user is not signed in. Throw an error if credentials cannot be fetched due to a network error or other temporary error.
This token is kept for the duration of a sync connection.
Implementation
@override
Future<PowerSyncCredentials?> fetchCredentials() async {
final devCredentials = await loadDevCredentials();
if (devCredentials?.token == null) {
// Not signed in
return null;
}
final uri = Uri.parse(devCredentials!.endpoint).resolve('dev/token.json');
final res = await http
.post(uri, headers: {'Authorization': 'Token ${devCredentials.token}'});
if (res.statusCode == 401) {
clearDevToken();
}
if (res.statusCode != 200) {
throw http.ClientException(res.reasonPhrase ?? 'Request failed', uri);
}
return PowerSyncCredentials.fromJson(jsonDecode(res.body)['data']);
}