actingAs<T> static method

void actingAs<T>(
  1. T user, {
  2. String? key,
})

Set an authenticated user for the current test.

Example:

NyTest.actingAs<User>(User(id: 1, name: 'Test User'));
// Now Nylo.user<User>() returns this user

Implementation

static void actingAs<T>(T user, {String? key}) {
  _currentUser = user;

  // Try to get auth key from Nylo if initialized, otherwise use default
  String authKey = key ?? 'auth_user';
  if (Nylo.isInitialized()) {
    authKey = key ?? Nylo.instance.getAuthKey() ?? 'auth_user';
  }

  Backpack.instance.save(authKey, user);

  // Also set auth key if Nylo is initialized and key not already set
  if (Nylo.isInitialized() && Nylo.instance.getAuthKey() == null) {
    Nylo.instance.addAuthKey(authKey);
  }
}