Chrome扩展程序中的Firebase身份验证快速过期 [英] Firebase authentication in Chrome extension expires quickly

查看:96
本文介绍了Chrome扩展程序中的Firebase身份验证快速过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个带有Firebase的网站,该网站可提供后端服务.我还将构建一个关联的Chrome扩展程序,该扩展程序将不定期刷新数据,并在有新内容时显示通知徽章.

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.

使用OAuth扩展中的身份验证可以正常工作,但是会话会在短暂的时间后过期,并且需要重新登录,从而使扩展无效.

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.

我尝试过在会话之间手动将持久性类型设置为默认值,但是显然在Chrome扩展程序中效果不佳.我想知道在警报API将其唤醒以进行数据提取之前,扩展是否正在睡眠"正在杀死会话.

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的示例扩展程序使用Chrome的身份API,出于简单的原因,我不想使用它,即使我自己的用户也不会使用与我的Chrome个人资料相同的登录电子邮件.

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.

如何在Chrome扩展程序中保持Firebase身份验证状态?我发现了一些与令牌有关的粗略建议,但它们都令人发疯地含糊不清.

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);
}); }

推荐答案

发现了我的错误,并且我走在正确的轨道上–确实与扩展睡眠有关.我正在将window.user设置为结果,在某些示例中,该结果在某处.当分机休眠时,该死.必须将onAuthStateChanged函数放回原处.

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现在真的在推动非持久性背景脚本,因此我不得不重新构建逻辑,因为它每次醒来都会重新运行.

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.

这篇关于Chrome扩展程序中的Firebase身份验证快速过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆