configureServer method

Future<void> configureServer(
  1. Angel app
)

Configures an Angel server to decode and validate JSON Web tokens on demand, whenever an instance of User is injected.

Implementation

Future<void> configureServer(Angel app) async {
  /*
  if (serializer == null) {
    throw StateError(
        'An `AngelAuth` plug-in was called without its `serializer` being set. All authentication will fail.');
  }
  if (deserializer == null) {
    throw StateError(
        'An `AngelAuth` plug-in was called without its `deserializer` being set. All authentication will fail.');
  }

  if (app.container == null) {
    _log.severe('Angel3 container is null');
    throw StateError(
        'Angel.container is null. All authentication will fail.');
  }
  */
  var appContainer = app.container;

  appContainer.registerSingleton(this);
  if (runtimeType != AngelAuth) {
    appContainer.registerSingleton(this, as: AngelAuth);
  }

  if (!appContainer.has<_AuthResult<User>>()) {
    appContainer
        .registerLazySingleton<Future<_AuthResult<User>>>((container) async {
      var req = container.make<RequestContext>();
      var res = container.make<ResponseContext>();
      //if (req == null || res == null) {
      //  _log.warning('RequestContext or responseContext is null');
      //  throw AngelHttpException.forbidden();
      //}

      var result = await _decodeJwt(req, res);
      if (result != null) {
        return result;
      } else {
        _log.warning('JWT is null');
        throw AngelHttpException.forbidden();
      }
    });

    appContainer.registerLazySingleton<Future<User>>((container) async {
      var result = await container.makeAsync<_AuthResult<User>>();
      return result.user;
    });

    appContainer.registerLazySingleton<Future<AuthToken>>((container) async {
      var result = await container.makeAsync<_AuthResult<User>>();
      return result.token;
    });
  }

  app.post(reviveTokenEndpoint, _reviveJwt);

  app.shutdownHooks.add((_) {
    _onLogin.close();
  });
}