Firebase authentication in Chrome extension expires quickly

£可爱£侵袭症+ 提交于 2020-01-04 10:17:18

问题


I have built a website with Firebase serving back-end duties. I'm also building an associated Chrome extension that will refresh data every once in a while and display a notifications badge when there's new content.

Authentication in the extension is working just fine using OAuth, but the session expires after a brief amount of time and requires a new login, which renders the extension useless.

I've tried manually setting the persistence type to what should already have been the default, between sessions, but it apparently doesn't play nice with Chrome extensions. I'm wondering if the extension "sleeping" until the alarms API wakes it for the data fetch is killing the session.

Google's example extension uses Chrome's identity API, which I don't want to use for the simple reason that even my own user doesn't use the same login email as my Chrome profile.

How can I persist the Firebase authentication state in a Chrome extension? I have found a couple cursory suggestions related to tokens, but they've all been maddeningly vague.

function login() {
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider).then(function (result) {
    window.user = result.user;
    adminCheck();
}).catch(function (error) {
    console.log(error.code, error.message);
}); }

回答1:


Found my mistake, and I was on the right track--it did have to do with the extension sleeping. I was setting window.user with the result, which was in some example somewhere. That dies when the extension sleeps. Had to have the onAuthStateChanged function put it back.

firebase.auth().onAuthStateChanged(function (user) {
    if (user) {
        window.user = user;
        reinitialized = true;
        refreshNotifications();
    } else {
        askForLogin();
    }
});

Google's really pushing the nonpersistent background scripts now, so I had to restructure the logic a bit since it runs anew every time it wakes.



来源:https://stackoverflow.com/questions/51404240/firebase-authentication-in-chrome-extension-expires-quickly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!