authorize method

Future<Session> authorize(
  1. AuthMode mode,
  2. Credential account, {
  3. Context? ctx,
})

Implementation

Future<Session> authorize(
  AuthMode mode,
  Credential account, {
  Context? ctx,
}) async {
  await ready();
  final credential = Credential(
    clientId: account.clientId,
    authType: account.authType?.isNotEmpty == true
        ? account.authType
        : mode.wireName,
    accessKey: account.accessKey,
    secretKey: account.secretKey,
    captcha: account.captcha,
  );
  _setState(_state.copyWith(authenticating: true));
  try {
    final accessToken = await _tokenizer.quickauth(credential, ctx: ctx);
    final token = accessToken?.token ?? '';
    if (token.isEmpty) {
      throw Status.unauthorized.err();
    }
    final session =
        await _tokenizer.authenticate(token, ctx: _withToken(ctx, token));
    if (session == null) {
      throw Status.unauthorized.err();
    }
    await _persist(
      token: token,
      session: session,
      expiresAt: accessToken?.expiresAt,
    );
    _setState(
      _state.copyWith(
        ready: true,
        session: session,
        expiresAt: accessToken?.expiresAt,
        authenticating: false,
      ),
    );
    return session;
  } catch (_) {
    _setState(_state.copyWith(authenticating: false));
    rethrow;
  }
}