authClientForUser static method

Future<AutoRefreshingAuthClient?> authClientForUser(
  1. Session session,
  2. int userId
)

Returns an authenticated client for a specific, authenticated user. The client can be used to access Google's APIs. To be able to get a client the user must have been authenticated with Google, otherwise null is returned.

Implementation

static Future<AutoRefreshingAuthClient?> authClientForUser(
  Session session,
  int userId,
) async {
  assert(
    clientSecret != null,
    'Google client secret from $_configFilePath is not loaded',
  );

  var refreshTokenData = await GoogleRefreshToken.findSingleRow(
    session,
    where: (t) => t.userId.equals(userId),
  );
  if (refreshTokenData == null) {
    return null;
  }

  var credentials = AccessCredentials.fromJson(
    jsonDecode(refreshTokenData.refreshToken),
  );

  var client = http.Client();

  var clientId = ClientId(clientSecret!.clientId, clientSecret!.clientSecret);

  return AutoRefreshingClient(
    client,
    clientId,
    credentials,
    closeUnderlyingClient: true,
  );
}