ChatGptOAuthCredentials.decode constructor

ChatGptOAuthCredentials.decode(
  1. String value
)

Implementation

factory ChatGptOAuthCredentials.decode(String value) {
  final decoded = jsonDecode(value);
  if (decoded is! Map) {
    throw const FormatException('OAuth credentials are not an object');
  }
  String field(String name) {
    final result = decoded[name];
    if (result is! String || result.isEmpty) {
      throw FormatException('OAuth credentials are missing $name');
    }
    return result;
  }

  final accountId = decoded['chatgpt_account_id'];
  return ChatGptOAuthCredentials(
    accessToken: field('access_token'),
    refreshToken: field('refresh_token'),
    idToken: field('id_token'),
    accountId: accountId is String && accountId.isNotEmpty
        ? accountId
        : _accountIdFromJwt(field('id_token')),
    expiresAt: _persistedExpiry(decoded['expires_at']),
  );
}