【问题标题】:Use Firebase.com authenication without forcing the user to create an account在不强制用户创建帐户的情况下使用 Firebase.com 身份验证
【发布时间】:2015-12-20 18:17:09
【问题描述】:

我正在开发一个小应用程序来允许用户存储待办事项。我正在使用 Firebase ass 我的后端。 对于第一次迭代,我不希望用户必须注册,但仍然只能看到它自己的数据。我的第一个想法就是使用匿名身份验证,存储 UID 并在每次启动应用程序时重复使用它。这是不可能的,因为会话会在某个时候超时,并且用户下次会获得一个新的 UID。 我当然想确保用户只能使用 Firebase 安全和规则查看自己的项目。

我们的想法是像这样保存项目:app.firebase.com/user/123456/todo-item

123456 是用户的唯一 ID。 我可以自己创建一个唯一标识符并仍然使用 Firebase 安全和规则吗?

【问题讨论】:

    标签: firebase firebase-authentication


    【解决方案1】:

    You would have to run your own custom authentication solution.

    当 Activity 加载时,您必须向服务器发出请求。然后在服务器上,您可以在用户加载页面时为其创建令牌:

    // These payload properties are accessible 
    // by Security Rules which is awesome
    Map<String, Object> payload = new HashMap<String, Object>();
    payload.put("uid", "uniqueId1");
    payload.put("some", "arbitrary");
    payload.put("data", "here");
    
    TokenGenerator tokenGenerator = new TokenGenerator("<YOUR_FIREBASE_SECRET>");
    String token = tokenGenerator.createToken(payload);
    

    除了 Java 之外还有更多的包,所以请阅读文档。

    然后,当您将令牌交付给用户时,您需要将令牌存储在本地。

    存储令牌后,您可以检索它并进行身份验证。

    Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/");
    ref.authWithCustomToken(AUTH_TOKEN, new Firebase.AuthResultHandler() {
        @Override
        public void onAuthenticationError(FirebaseError error) {
            System.err.println("Login Failed! " + error.getMessage());
        }
    
        @Override
        public void onAuthenticated(AuthData authData) {
            // authenticated
        }
    });
    

    您可能对必须运行服务器并不感到兴奋,但不妨看看将 Google AppEngine 与 Firebase JVM 客户端结合使用。它非常简单,为您处理服务器维护。

    This tutorial by a former Google Cloud tools member, and current Firebase team member is a great place to start.

    【讨论】:

    • 是否有机会生成令牌而无需运行额外的服务器?
    • 技术上可以在客户端上运行所有代码。但是,您将向世界公开您的密钥,然后您的 Firebase 数据库将不再安全。所以不,你需要一个服务器。但就像我说的,AppEngine 设置简单且维护成本低。
    • 感谢您的洞察力。我会试试这个。
    猜你喜欢
    • 2021-02-07
    • 1970-01-01
    • 2023-02-02
    • 2014-08-09
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 2020-06-10
    • 2016-12-15
    相关资源
    最近更新 更多