Chrome扩展弹出窗口中的Firebase身份验证

Div*_*ero 7 javascript google-chrome-extension firebase firebase-authentication

我正在尝试在Chrome扩展程序中使用身份验证(电子邮件/密码).如果我将我的身份验证代码放在后台脚本中,我似乎工作正常.但是我似乎无法将其作为浏览器动作脚本工作.

我使用以下代码作为我的扩展的基础:https://github.com/firebase/firebase-chrome-extension

我将browser_action.js更改为:

Firebase.enableLogging(true);
var f = new Firebase('https://myapp.firebaseio.com/');

f.authWithPassword({    
    email    : "a@b.cd",
    password : "1234"
}, function(error, authData) {
    if (error) {
        alert("Login Failed!", error);
} else {
    alert("Authenticated successfully with payload:", authData);
}
});
Run Code Online (Sandbox Code Playgroud)

我保持现有的browser_action.html不变:

<!doctype html>


<style type="text/css">
    #mainPopup {
        padding: 10px;
        height: 200px;
        width: 400px;
        font-family: Helvetica, Ubuntu, Arial, sans-serif;
    }
    h1 {
        font-size: 2em;
    }
</style>

<div id="mainPopup">
<h1>Firebase test!</h1>
<p>Clicks: <span id="contents"></span></p>
</div>

<script type="text/javascript" src="https://cdn.firebase.com/v0/firebase.js"></script>
<script src="browser_action.js"></script>
Run Code Online (Sandbox Code Playgroud)

当我加载扩展并单击图标时,它在控制台中给出以下错误:

Uncaught TypeError: f.authWithPassword is not a function
Run Code Online (Sandbox Code Playgroud)

如果我将firebase代码和身份验证代码移动到background.js文件,它可以工作并向我发出一个警告,表明它已成功通过身份验证.

我做错了什么或为什么这不可能?

hee*_*nee 6

该示例chrome扩展程序在3年内没有更新,因此其Firebase版本已过时。https://cdn.firebase.com/v0/firebase.jshttps://cdn.firebase.com/js/client/2.2.1/firebase.jsin 替换,browser_action.html您应该可以authWithPassword成功使用。


小智 6

对于自 2016 年 7 月以来遇到此问题的任何人,Firebase 提供了一组更新的关于如何为 Chrome 扩展程序设置身份验证的说明(在他们的示例中,他们使用 Google 身份验证)。在看到这篇文章之前,我跳过了一堆圈子……希望能节省一些时间。https://github.com/firebase/quickstart-js/tree/master/auth/chromextension

  • 问题是,该代码示例也已过时:在自述文件中,作者引用了 Firebase 控制台中不再存在的 UI 元素:来自外部项目的白名单客户端 ID(可选)。 (2认同)