initialize static method
Initialize auth manager with secure storage callbacks
Implementation
static Future<void> initialize({
required SDKEnvironment environment,
String? baseURL,
}) async {
if (_isInitialized) return;
try {
final lib = PlatformLoader.loadCommons();
// Resolve the synchronous helper before installing callbacks. Missing
// native storage is an initialization error, not an in-memory fallback.
DartBridgeSecureStorage.instance;
final initAuth = lib
.lookupFunction<
Void Function(Pointer<RacSecureStorageCallbacksStruct>),
void Function(Pointer<RacSecureStorageCallbacksStruct>)
>('rac_auth_init');
// Allocate and set up secure storage callbacks
final storagePtr = calloc<RacSecureStorageCallbacksStruct>();
storagePtr.ref.store = Pointer.fromFunction<RacSecureStoreCallbackNative>(
_secureStoreCallback,
_exceptionalReturnInt,
);
storagePtr.ref.retrieve =
Pointer.fromFunction<RacSecureRetrieveCallbackNative>(
_secureRetrieveCallback,
_exceptionalReturnInt,
);
storagePtr.ref.deleteKey =
Pointer.fromFunction<RacSecureDeleteCallbackNative>(
_secureDeleteCallback,
_exceptionalReturnInt,
);
storagePtr.ref.context = nullptr;
// Commons copies the vtable, so the temporary struct can be released.
try {
initAuth(storagePtr);
} finally {
calloc.free(storagePtr);
}
final loadFn = lib.lookupFunction<Int32 Function(), int Function()>(
'rac_auth_load_stored_tokens',
);
final loadResult = loadFn();
if (loadResult != RacResultCode.success &&
loadResult != RacResultCode.errorFileNotFound) {
SDKException.throwIfError(loadResult);
}
// Wire token refresh hooks into the shared HTTP client so any
// request with `requiresAuth: true` can pick up / refresh tokens
// without a direct dependency on this bridge.
HTTPClientAdapter.shared.setTokenResolver(instance._resolveToken);
HTTPClientAdapter.shared.setRefreshCallback(instance._refreshForAdapter);
_isInitialized = true;
_logger.debug(
'Auth manager initialized',
metadata: {
'environment': environment.toString(),
'hasBaseURL': '${baseURL != null && baseURL.isNotEmpty}',
},
);
} catch (_) {
_isInitialized = false;
_logger.error('Auth initialization failed');
rethrow;
}
}