I am trying to build a Chrome extension with Google Firebase for authentication, I am using a web pack for building my project, the Firebase is being used in the background script.
On the first init or when the script sleeps, Firebase returns null for the user and takes some time to read the current user so it returns null for the user.
The question is how can I load Firebase and then make the request so I won't get null?
Some snippets from my code
const getUser = async (): Promise<firebase.User> =>
new Promise((resolve, reject) => {
chrome.runtime.sendMessage(
{ message: MESSAGES.getCurrentUser },
(user: firebase.User) => {
if (user) resolve(user);
reject();
}
);
});
and in the background script
import firebase from "firebase/app";
import "firebase/auth";
firebase.initializeApp(firebaseConfig);
chrome.runtime.onMessage.addListener(
(request: Request, sender, sendResponse) => {
switch (message) {
case MESSAGES.logOut:
logOut(sendResponse);
break;
case MESSAGES.getCurrentUser:
getCurrentUser(sendResponse);
break;
break;
}
return true;
}
);
const getCurrentUser = async (sendResponse: (response?: any) => void) => {
const user = await firebase.auth().currentUser;
sendResponse(user);
};
my manifest file
{
"short_name": "",
"name": "",
"manifest_version": 3,
"version": "0.2.0",
"icons": {
"16": "logo192.png",
"48": "logo192.png",
"128": "logo192.png"
},
"action": {
"default_popup": "popup.html"
},
"permissions": ["activeTab", "storage", "identity", "downloads"],
"options_page": "options.html",
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
]
}