parse static method

AccessToken parse(
  1. String token
)

Parses a JWT access token string.

This method validates the JWT structure and extracts the payload information including user ID and expiration time.

token The JWT token string to parse.

Returns an AccessToken instance with parsed information.

Throws FormatException if the token format is invalid or cannot be parsed.

Example

try {
  final accessToken = AccessToken.parse(tokenString);
  print('User: ${accessToken.userId}');
} catch (e) {
  print('Invalid token: $e');
}

Implementation

static AccessToken parse(String token) {
  final parts = token.split('.');
  if (parts.length < 2) {
    throw FormatException('Invalid JWT token: $token');
  }
  final base64Payload = base64.normalize(
    parts[1].replaceAll('-', '+').replaceAll('_', '/'),
  );
  final payloadMap = _decodePayload(base64Payload);
  return AccessToken._(
    token,
    userId: payloadMap['uid'],
    exp: payloadMap['exp'],
  );
}