createJsonWebToken function

Future<String> createJsonWebToken({
  1. required String? username,
  2. required String? password,
  3. required Client client,
  4. required String backendUrl,
})

Mints a new JSON Web Token (JWT) that you may later use with JsonWebTokenAuthorizer. Note that the client should use UsernamePasswordAuthorizer; see withClient3.

Implementation

Future<String> createJsonWebToken(
    {required String? username,
    required String? password,
    required http.Client client,
    required String backendUrl}) async {
  var response = await client.post(Uri.parse(backendUrl));
  if (response.statusCode == 403) {
    throw UnauthenticatedException(
        "Cannot authenticate user. A common cause is an invalid username, an administratively deactivated user, or an incorrect password.");
  }
  var json = convert.jsonDecode(response.body) as Map<String, dynamic>;
  assert(json["token"] != null);
  assert(json["token"] is String);
  String ret = json["token"] as String;
  assert(ret.isNotEmpty);
  return ret;
}