OAuthToken.fromJson constructor

OAuthToken.fromJson(
  1. Map<String, dynamic> json
)

Implementation

factory OAuthToken.fromJson(Map<String, dynamic> json) {
  final extra = Map<String, dynamic>.from(json);
  // Remove standard fields
  extra.removeWhere(
    (key, value) => [
      'access_token',
      'token_type',
      'expires_in',
      'refresh_token',
      'scope',
      'issued_at',
    ].contains(key),
  );

  return OAuthToken(
    accessToken: json['access_token'] as String,
    tokenType: json['token_type'] as String? ?? 'Bearer',
    expiresIn: (json['expires_in'] as num?)?.toInt(),
    refreshToken: json['refresh_token'] as String?,
    scopes: json['scope'] is String
        ? (json['scope'] as String).split(' ').where((s) => s.isNotEmpty).toList()
        : null,
    issuedAt:
        json['issued_at'] != null
            ? DateTime.fromMillisecondsSinceEpoch(json['issued_at'] as int)
            : DateTime.now(),
    extra: extra.isNotEmpty ? extra : null,
  );
}