TokenResponse.fromJson constructor

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

Creates a TokenResponse from JSON

Implementation

factory TokenResponse.fromJson(Map<String, dynamic> json) {
  // Handle the case where issued_at might not be in the response
  final now = DateTime.now();
  final issuedAt = json['issued_at'] != null
      ? DateTime.parse(json['issued_at'] as String)
      : now;

  // Handle scope as either string or list
  List<String>? scopeList;
  if (json['scope'] != null) {
    if (json['scope'] is String) {
      scopeList = (json['scope'] as String).split(' ');
    } else if (json['scope'] is List) {
      scopeList = (json['scope'] as List).cast<String>();
    }
  }

  return TokenResponse(
    accessToken: json['access_token'] as String,
    refreshToken: json['refresh_token'] as String,
    expiresIn: json['expires_in'] as int,
    tokenType: json['token_type'] as String? ?? 'Bearer',
    issuedAt: issuedAt,
    scope: scopeList,
  );
}