首页 文章

使用邮件和密码通过REST API进行身份验证[Firebase]

提问于
浏览
46

我想知道是否 possible 实际 authenticate 使用 custom authentication 到Firebase REST API withouth

我已经与Firebase合作了一段时间,我现在正在考虑将我的后端迁移到Firebase . 使用后端的应用程序当前使用REST API,根本不需要实时数据 . 因此,我只想在客户端上使用REST API而不是完整的Android框架 .

Is it possible to get an auth token using the mail & password authentication of Firebase via HTTP-requests?

old docs我只找到了自定义登录的解决方案,在new docs中,您似乎需要一个Google服务帐户 .

任何帮助或建议表示赞赏 .

5 回答

  • 2

    来自Firebase指南使用自定义身份验证系统在网站上使用Firebase进行身份验证(请参阅https://firebase.google.com/docs/auth/web/custom-auth

    您可以通过修改身份验证服务器将Firebase身份验证与自定义身份验证系统集成,以便在用户成功登录时生成自定义签名令牌 . 您的应用会收到此令牌并使用它来对Firebase进行身份验证 .

    Here's the key idea:

    1)将Firebase添加到您的Web项目并使用Firebase REST JavaScript SDK进行身份验证,并使用Firebase访问存储/实时数据库 .

    // TODO: Replace with your project's customized code snippet
      <script src="https://www.gstatic.com/firebasejs/3.0.2/firebase.js"></script>
      <script>
        // Initialize Firebase
        var config = {
          apiKey: '<your-api-key>',
          authDomain: '<your-auth-domain>',
          databaseURL: '<your-database-url>',
          storageBucket: '<your-storage-bucket>'
        };
        firebase.initializeApp(config);
      </script>
    

    2)您的应用用户使用他们的用户名和密码登录您的身份验证服务器 . 您的服务器检查凭据并返回自定义令牌(如果它们有效) .

    3)从验证服务器收到自定义令牌后,将其传递给signInWithCustomToken以登录用户

    firebase.auth().signInWithCustomToken(token).catch(function(error) {
      // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
      // ...
    });
    
  • 1

    我相信你可以做以下其中一件事:

    GitHub上还有用于生成Firebase令牌的项目:

  • 0

    使用电子邮件和密码进行身份验证后,您需要返回令牌,根据您可以使用 getToken(opt_forceRefresh) 返回用户令牌的文档,可从以下URL获得 .

    https://firebase.google.com/docs/reference/js/firebase.User#getToken

  • 3

    更新:现在记录Firebase REST身份验证!

    View the documentation


    Firebase REST身份验证

    我想通过检查Javascript API发送的请求,找出如何为Firebase执行电子邮件和密码身份验证 .

    These APIs are undocumented and unsupported


    Firebase 3

    Firebase 3身份验证是Google Identity Toolkit的更新和重命名版本 . 旧文档不完全准确,但可能有用,可以在这里找到:https://developers.google.com/identity/toolkit/web/reference/

    Firebase 3 requires all requests to have Content-Type: application/json in the header

    API密钥

    Firebase 3需要将API密钥附加到所有身份验证请求 . 您可以访问Firebase项目概述并单击“将Firebase添加到您的Web应用程序”,找到数据库的API密钥 . 您应该看到一个包含如下代码的窗口:

    <script src="https://www.gstatic.com/firebasejs/live/3.0/firebase.js">    </script>
    <script>
      // Initialize Firebase
      var config = {
        apiKey: "<my-firebase-api-key>",
        authDomain: "my-firebase.firebaseapp.com",
        databaseURL: "https://my-firebase.firebaseio.com",
        storageBucket: "my-firebase.appspot.com",
      };
      firebase.initializeApp(config);
    </script>
    

    复制apiKey值并保存以供日后使用 .

    注册

    方法: POST

    网址: https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=<my-firebase-api-key>

    有效载荷:

    {
        email: "<email>",
        password: "<password>",
        returnSecureToken: true
    }
    

    响应:

    {
        "kind": "identitytoolkit#SignupNewUserResponse",
        "localId": "<firebase-user-id>", // Use this to uniquely identify users
        "email": "<email>",
        "displayName": "",
        "idToken": "<provider-id-token>", // Use this as the auth token in database requests
        "registered": true,
        "refreshToken": "<refresh-token>",
        "expiresIn": "3600"
    }
    

    登录

    方法: POST

    网址: https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=<my-firebase-api-key>

    有效载荷:

    {
        email: "<email>",
        password: "<password>",
        returnSecureToken: true
    }
    

    响应:

    {
        "kind": "identitytoolkit#VerifyPasswordResponse",
        "localId": "<firebase-user-id>", // Use this to uniquely identify users
        "email": "<email>",
        "displayName": "",
        "idToken": "<provider-id-token>", // Use this as the auth token in database requests
        "registered": true,
        "refreshToken": "<refresh-token>",
        "expiresIn": "3600"
    }
    

    获取帐户信息

    方法: POST

    网址: https://www.googleapis.com/identitytoolkit/v3/relyingparty/getAccountInfo?key=<my-firebase-api-key>

    有效载荷:

    {
        idToken: "<provider-id-token>"
    }
    

    响应:

    {
        "kind": "identitytoolkit#GetAccountInfoResponse",
        "users": [
        {
            "localId": "<firebase-user-id>",
            "email": "<email>",
            "emailVerified": false,
            "providerUserInfo": [
            {
                "providerId": "<password>",
                "federatedId": "<email>",
                "email": "<email>",
                "rawId": "<email>"
            }],
            "passwordHash": "<hash>",
            "passwordUpdatedAt": 1.465327109E12,
            "validSince": "1465327108",
            "createdAt": "1465327108000"
        }]
    }
    

    Firebase 2

    这些请求返回Firebase文档中描述的JSON数据 . https://www.firebase.com/docs/web/guide/login/password.html#section-logging-in

    登录

    您可以通过发送具有以下格式的GET请求进行身份验证:

    https://auth.firebase.com/v2/<db_name>/auth/password?&email=<email>&password=<password>
    

    注册

    也可以通过使用 _method=POST 作为查询字符串的一部分发送相同的GET请求来执行用户创建

    https://auth.firebase.com/v2/<db_name>/users?&email=<email>&password=<password>&_method=POST
    
  • 56

    如果您尝试通过 REST API ,则必须在 your Apllication 中执行所有操作 .

    只需 grab json data 并选中 your authenticate or not 即可 .

    使用 retrofit Get method 并从 firebase app 中获取所有数据 .

    这是我的帖子Rerofit + Firebase,我是为初学者发布的,以了解firebase和Retrofit的连接 .

    OR

    请通过这个链接,它会帮助你.....................

    REST auth

    User Authnitication

    Example

    喜欢编码.......

相关问题