generateToken static method

Future<String?> generateToken(
  1. dynamic payload, {
  2. int? seconds,
  3. String? key,
})

Generates an encrypted token with an optional expiration time.

Implementation

static Future<String?> generateToken(
  dynamic payload, {
  int? seconds,
  String? key,
}) async {
  try {
    final iat = DateTime.now().millisecondsSinceEpoch ~/ 1000;
    int? exp;

    if (seconds != null && seconds > 0) {
      exp = iat + seconds;
    }

    final tokenPayload = {'iat': iat, 'exp': exp, 'payload': payload};

    // Use the mock Encrypter to encrypt the payload
    return await Encrypter.encryptAES256CBC(tokenPayload, key: key);
  } catch (e) {
    rethrow;
  }
}