getAuthCode function

Future<void> getAuthCode({
  1. required List<String> scopes,
  2. void success(
    1. AuthCodeResult result
    )?,
  3. void fail()?,
  4. void complete()?,
})

Requests an authorization code with the specified scopes.

This function initiates an OAuth-style authorization flow through the Hylid Bridge. The user will be prompted to authorize the requested scopes, and an authorization code will be returned on success.

Example:

await getAuthCode(
  scopes: ['auth_base', 'USER_ID'],
  success: (AuthCodeResult result) {
    final code = result.authCode?.toDart;
    print('Auth code: $code');
    // Send code to your backend to exchange for access token
  },
  fail: () {
    print('User denied authorization');
  },
);

Parameters:

  • scopes: List of authorization scopes to request (e.g., 'auth_base', 'USER_ID').
  • success: Called when authorization succeeds with the auth code.
  • fail: Called when authorization fails or is denied.
  • complete: Called when the authorization flow completes (success or fail).

Implementation

Future<void> getAuthCode({
  required List<String> scopes,
  void Function(AuthCodeResult result)? success,
  void Function()? fail,
  void Function()? complete,
}) async {
  // Ensure the Hylid Bridge script is loaded before calling
  await scriptLoader.ensureInitialized();

  final authParameters = _AuthCodeParameters(
    scopes: scopes.map((s) => s.toJS).toList().toJS,
    success: success?.toJS,
    fail: fail?.toJS,
    complete: complete?.toJS,
  );
  _fetchAuthCodeFromBridge(authParameters);
}