signInAnonymously method

Future<CloudBaseAuthState> signInAnonymously()

Implementation

Future<CloudBaseAuthState> signInAnonymously() async {
  /// 如果本地存有uuid则匿名登录时传给server
  String? uuid = await cache.getStore(cache.anonymousUuidKey);
  String? refreshToken = await cache.getStore(cache.refreshTokenKey);
  final CloudBaseAuthType? loginType =
      await cache.getStore(cache.loginTypeKey);
  final CloudBaseResponse? res = await CloudBaseRequest(super.core)
      .postWithoutAuth('auth.signInAnonymously', {
    'anonymous_uuid': uuid,
    'refresh_token': refreshToken,
    'currLoginType': loginType?.index
  });

  if (res == null) {
    throw new CloudBaseException(
        code: CloudBaseExceptionCode.NULL_RESPONSE,
        message: "unknown error, res is null");
  }

  if (res.code != null) {
    throw new CloudBaseException(code: res.code, message: res.message);
  }

  if (res.data != null &&
      res.data['refresh_token'] != null &&
      res.data['uuid'] != null) {
    String newUuid = res.data['uuid'];
    String newRefreshToken = res.data['refresh_token'];
    await _setAnonymousUUID(newUuid);
    await setRefreshToken(newRefreshToken);
    await refreshAccessToken();

    return CloudBaseAuthState(
        authType: CloudBaseAuthType.ANONYMOUS, refreshToken: newRefreshToken);
  } else {
    throw CloudBaseException(
        code: CloudBaseExceptionCode.AUTH_FAILED, message: '匿名登录失败');
  }
}