authClientForUser static method
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 {
if (clientSecret == null) {
throw StateError(
'Google client secret from $_configFilePath is not loaded',
);
}
var refreshTokenData = await GoogleRefreshToken.db.findFirstRow(
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,
const GoogleAuthEndpoints(),
clientId,
credentials,
closeUnderlyingClient: true,
);
}