Skip to content

Commit

Permalink
Merge pull request #4 from samelhusseini/patch-1
Browse files Browse the repository at this point in the history
Use an observer to check auth status awaiting firebase initialization
  • Loading branch information
resulturan authored Nov 6, 2022
2 parents f0d4dd0 + 978bfeb commit 9501830
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/firebaseAuth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createUserWithEmailAndPassword, sendPasswordResetEmail, signInWithEmailAndPassword, updateEmail, updatePassword, getAuth, signOut, Auth, RecaptchaVerifier, updateProfile, sendEmailVerification, browserLocalPersistence, browserSessionPersistence, RecaptchaParameters, getIdTokenResult, ParsedToken } from "firebase/auth";
import { createUserWithEmailAndPassword, sendPasswordResetEmail, signInWithEmailAndPassword, updateEmail, updatePassword, getAuth, signOut, Auth, RecaptchaVerifier, updateProfile, sendEmailVerification, browserLocalPersistence, browserSessionPersistence, RecaptchaParameters, getIdTokenResult, ParsedToken, User as FirebaseUser } from "firebase/auth";
import { FirebaseApp } from "@firebase/app";
import { IRegisterArgs, ILoginArgs, IUser, IAuthCallbacks, IAuthContext } from "./interfaces";

Expand Down Expand Up @@ -102,9 +102,18 @@ export class FirebaseAuth {
name: user?.displayName || ""
};
}

private getFirebaseUser(): Promise<FirebaseUser> {
return new Promise<FirebaseUser>((resolve, reject) => {
const unsubscribe = this.auth?.onAuthStateChanged(user => {
unsubscribe();
resolve(user);
}, reject);
});
}

private async handleCheckAuth() {
if (this.auth?.currentUser) {
if (await this.getFirebaseUser()) {
return Promise.resolve();
} else {
return Promise.reject("User is not found");
Expand Down Expand Up @@ -134,4 +143,4 @@ export class FirebaseAuth {
getUserIdentity: this.getUserIdentity,
};
}
}
}

0 comments on commit 9501830

Please sign in to comment.