decodeExpSeconds function

int? decodeExpSeconds(
  1. String? token
)

Đọc exp (giây epoch) từ phần payload của token, không xác thực chữ ký.

Việc xác thực là của edge; ở client ta chỉ cần biết khi nào token vô dụng để khỏi gửi lên.

Implementation

int? decodeExpSeconds(String? token) {
  if (token == null || token.isEmpty) return null;
  final dot = token.indexOf('.');
  if (dot <= 0) return null;
  try {
    var part = token.substring(0, dot);
    part = part.padRight(part.length + (4 - part.length % 4) % 4, '=');
    final decoded = jsonDecode(utf8.decode(base64Url.decode(part)));
    if (decoded is Map && decoded['exp'] is num) {
      return (decoded['exp'] as num).toInt();
    }
  } catch (_) {
    // Token do server phát; hình dạng lạ thì coi như không đọc được hạn.
  }
  return null;
}