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

Firebase authentication in Chrome Extension popup

本文关键字:身份验证 窗口 Firebase 扩展 程序 Chrome      更新时间:2023-09-26

我正在尝试让身份验证(电子邮件/密码)在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);
}
});

我保持现有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>

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

Uncaught TypeError: f.authWithPassword is not a function

如果我将 firebase 代码和身份验证代码移到后台.js文件,它会起作用,并提醒我它已成功通过身份验证。

我做错了什么,或者为什么这是不可能的?

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

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