Skip to content

Commit

Permalink
Do not signout if app initialization failed
Browse files Browse the repository at this point in the history
  • Loading branch information
GogoVega committed Nov 24, 2024
1 parent 50fda31 commit 7ed2b7c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/lib/firebase/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,16 @@ export class Client extends TypedEmitter<ClientEvents> {
if (this.signState === SignState.NOT_YET) throw new ClientError("signOut called before signIn call");
if (this.signState === SignState.SIGN_OUT) throw new ClientError("signOut already called");

// The app is created regardless of whether the client has been initialized
// If initialized sign out, otherwise skip
// Ensure the app has been created to signout and delete it
if (this._clientInitialised) {
this._signState = SignState.SIGN_OUT;
this.emit("sign-out");

if (!this.admin) await signOut(this._auth!);
}
// Admin SDK has no signOut method - internally called during deleteApp
if (!this.admin && this._auth) await signOut(this._auth);

return this.deleteClient();
return this.deleteClient();
}
}

private async wrapSignIn(config: AppOptions): Promise<void>;
Expand All @@ -182,8 +182,8 @@ export class Client extends TypedEmitter<ClientEvents> {
this._signState = SignState.SIGN_IN;
this.emit("sign-in");
this._app = admin ? new AdminApp(config, this.appName) : new App(this.appConfig, this.appName);
this._auth = admin ? undefined : getAuth(this._app.app as FirebaseApp);
this._clientInitialised = true;
this._auth = admin ? undefined : getAuth(this._app.app as FirebaseApp);
const user = await signInFn();
this._signState = SignState.SIGNED_IN;
success = true;
Expand Down
13 changes: 8 additions & 5 deletions src/lib/nodes/firebase-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ export class FirebaseClient {
private initFirestore() {
// Skip if database already instanciate
if (this.node.firestore) return;
// Skip if the client is not initialised
if (!this.node.client?.clientInitialised) return;

// TODO: Add log
Expand All @@ -258,6 +259,7 @@ export class FirebaseClient {
private initRTDB() {
// Skip if database already instanciated
if (this.node.rtdb) return;
// Skip if the client is not initialised
if (!this.node.client?.clientInitialised) return;
// TODO: pas l'idéal (comment gérer une mauvaise URL)
if (this.statusListeners.rtdb.length > 1) return;
Expand Down Expand Up @@ -341,7 +343,11 @@ export class FirebaseClient {
public logOut(done: () => void) {
(async () => {
try {
if (!this.node.client) return done();
this.disableConnectionHandler();
this.disableGlobalLogHandler();

// Do not signout if the client is not initialized
if (!this.node.client?.clientInitialised) return done();

const rtdbOnline = this.node.rtdb && !this.node.rtdb.offline;
const firestoreOnline = this.node.firestore && !this.node.firestore.offline;
Expand All @@ -352,17 +358,14 @@ export class FirebaseClient {
// Only RTDB has connection state
this.node.rtdb?.removeConnectionState();

this.disableConnectionHandler();
this.disableGlobalLogHandler();

if (!this.node.client.admin && this.node.firestore) {
// https://github.com/firebase/firebase-js-sdk/issues/7816
// TODO: The promise is not resolved due to a bug in Node.
// Workaround for now to not wait for it anymore.
this.node.client.signOut();

// Small delay to ensure deployments won't cause any issues.
await new Promise((resolve) => setTimeout(resolve, 1500));
await new Promise((resolve) => setTimeout(resolve, 1000));
} else {
await this.node.client.signOut();
}
Expand Down

0 comments on commit 7ed2b7c

Please sign in to comment.