auth method

Future<Res> auth(
  1. String authVar,
  2. String authVal,
  3. String password,
  4. List<String>? groups,
)

Implementation

Future<Res> auth(
  String authVar,
  String authVal,
  String password,
  List<String>? groups,
) {
  if (authState == AuthState.authed) {
    throw UserAuthorisedException();
  }

  authState = AuthState.authing;
  final Doc doc = {};

  // Wrap generateAuthHash with try..catch to revert authState if exception
  // is to be thrown
  try {
    doc['hash'] = generateAuthHash(authVar, authVal, password);
  } catch (err) {
    authState = AuthState.notAuthed;
    throw err;
  }

  doc[authVar] = authVal;
  doc['groups'] = groups ?? [];
  Future<Res> call = this
      .call(
    'session/auth',
    doc: doc,
  )
      .catchError((err) {
    _onAuthStateChange(AuthState.notAuthed);
    NawahDI.get<INawahLogger>()?.error(this, 'auth call err: $err');
  });

  return call;
}