Skip to content

Commit 76d30dc

Browse files
committed
make sure we still can have valid instance if auth and appCheck initialized after firebaseai init and before api call
1 parent da69515 commit 76d30dc

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

packages/firebase_ai/firebase_ai/lib/src/base_model.dart

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,19 +282,23 @@ abstract class BaseModel {
282282
) {
283283
return () async {
284284
Map<String, String> headers = {};
285+
286+
final effectiveAppCheck = appCheck ?? app?.getService<FirebaseAppCheck>();
287+
final effectiveAuth = auth ?? app?.getService<FirebaseAuth>();
288+
285289
// Override the client name in Google AI SDK
286290
headers['x-goog-api-client'] =
287291
'gl-dart/$packageVersion fire/$packageVersion';
288-
if (appCheck != null) {
292+
if (effectiveAppCheck != null) {
289293
final appCheckToken = useLimitedUseAppCheckTokens == true
290-
? await appCheck.getLimitedUseToken()
291-
: await appCheck.getToken();
294+
? await effectiveAppCheck.getLimitedUseToken()
295+
: await effectiveAppCheck.getToken();
292296
if (appCheckToken != null) {
293297
headers['X-Firebase-AppCheck'] = appCheckToken;
294298
}
295299
}
296-
if (auth != null) {
297-
final idToken = await auth.currentUser?.getIdToken();
300+
if (effectiveAuth != null) {
301+
final idToken = await effectiveAuth.currentUser?.getIdToken();
298302
if (idToken != null) {
299303
headers['Authorization'] = 'Firebase $idToken';
300304
}

0 commit comments

Comments
 (0)