findUserByUserId static method

Future<UserInfo?> findUserByUserId(
  1. Session session,
  2. int userId, {
  3. bool useCache = true,
})

Find a user by its id. Returns null if no user is found. By default the result is cached locally on the server. You can configure the cache lifetime in AuthConfig, or disable it on a call to call basis by setting useCache to false.

Implementation

static Future<UserInfo?> findUserByUserId(Session session, int userId,
    {bool useCache = true}) async {
  var cacheKey = 'serverpod_auth_userinfo_$userId';
  UserInfo? userInfo;

  if (useCache) {
    userInfo = await session.caches.local.get<UserInfo>(cacheKey);
    if (userInfo != null) return userInfo;
  }

  userInfo = await session.db.findById<UserInfo>(userId);

  if (useCache && userInfo != null) {
    await session.caches.local.put(
      cacheKey,
      userInfo,
      lifetime: AuthConfig.current.userInfoCacheLifetime,
    );
  }

  return userInfo;
}