简体   繁体   English

在ngx-admin中设置令牌以连接到Firebase

[英]Setting the token in ngx-admin for connection to Firebase

I am trying to configure my ngx-admin to connect to Firebase using REST API. 我正在尝试将ngx-admin配置为使用REST API连接到Firebase。 Most of the posts on Stack Overflow instruct to rewrite the authentication code using Firebase's SDK. Stack Overflow上的大多数帖子都指示使用Firebase的SDK重写身份验证代码。 This is not what I want. 这不是我想要的。 Instead, ngx-admin is already designed to use REST API for authentication, which is also supported by Firebase. 相反,ngx-admin已经设计为使用REST API进行身份验证,Firebase也支持此功能。 I therefore decided to take this approach to use the ngx-admin codebase as much as possible. 因此,我决定采用这种方法来尽可能多地使用ngx-admin代码库。

My setting under NB_CORE_PROVIDERS in core.module.ts is as follows (showing only the login setting for simplicity): 我在core.module.ts中的core.module.ts下的设置如下(为简单起见,仅显示登录设置):

providers: {
      email: {
        service: NbEmailPassAuthProvider,
        config: {
          baseEndpoint: 'https://www.googleapis.com/identitytoolkit/v3/relyingparty',
          delay: 3000,
          login: {
            rememberMe: true,
            endpoint: '/verifyPassword?key='+environment.firebase.apiKey,
          },
          // ...
          token: {
            // How to set this properly for Firebase?
          }, 
        },
      },
    },

My difficulty is in how to set the token properly for Firebase. 我的困难在于如何为Firebase正确设置令牌。 The tutorial at https://akveo.github.io/nebular/#/docs/auth/getting-user-token says that https://akveo.github.io/nebular/#/docs/auth/getting-user-token上的教程说

By default Nebular expects that your token is located under the data.token keys of the JSON response 默认情况下,Nebular希望您的令牌位于JSON响应的data.token键下

This translates to the following default setting in @nebular/auth/providers/email-pass-auth.provider.ts : 这将转换为@nebular/auth/providers/email-pass-auth.provider.ts的以下默认设置:

token: {
  key: 'data.token',
  getter: (module: string, res: HttpResponse<Object>) => getDeepFromObject(res.body,
        this.getConfigValue('token.key')),
},

The tutorial then gives an example which assumes that: 然后,本教程提供了一个示例,该示例假定:

our API returns a token as just {token: 'some-jwt-token'} not wrapping your response in the data property 我们的API返回的令牌只是{token:'some-jwt-token'}而未将您的响应包装在data属性中

and this translates to: 转换为:

token: {
  key: 'token', // this parameter tells Nebular where to look for the token
},

First I tried skipping the token setting (which means I use the default, which is key: 'data.token' . While I could got a 200 status code (which means I successfully connected to Firebase), I got the following error: NbEmailPassAuthProvider: Token is not provided under 'data.token' key with getter 'function (module, res) { return Object(__WEBPACK_IMPORTED_MODULE_9__helpers__["getDeepFromObject"])(res.body, _this.getConfigValue('token.key')); }', check your auth configuration. 首先,我尝试跳过令牌设置(这意味着我使用默认值,这是key: 'data.token' 。虽然我可以得到200状态代码(这意味着我已成功连接到Firebase),但遇到了以下错误: NbEmailPassAuthProvider: Token is not provided under 'data.token' key with getter 'function (module, res) { return Object(__WEBPACK_IMPORTED_MODULE_9__helpers__["getDeepFromObject"])(res.body, _this.getConfigValue('token.key')); }', check your auth configuration.

Then I tried key: 'token' which gave me a similar error. 然后我尝试了key: 'token' ,这给了我一个类似的错误。

Upon consulting the Firebase documentation at https://firebase.google.com/docs/reference/rest/auth/#section-sign-in-email-password , I found that the response payload of Firebase consists of six properties: kind, idToken, email, refreshToken, expliresIn, localId, registered . https://firebase.google.com/docs/reference/rest/auth/#section-sign-in-email-password中查阅Firebase文档后,我发现Firebase的响应负载包括六个属性: kind, idToken, email, refreshToken, expliresIn, localId, registered I then tried key: 'idToken' and this time it worked. 然后,我尝试了key: 'idToken' ,这次它起作用了。 I could successfully logged into Firebase. 我可以成功登录Firebase。 Trying the same for signup also successfully signed me up in Firebase, as I could verify from the Firebase console. 我可以从Firebase控制台进行验证,尝试同样的注册方式也可以在Firebase中成功注册我。

However, this only gets one of the six properties returned. 但是,这只会获得返回的六个属性之一。 I tried key: 'data' instead but it gave me a similar error as above. 我尝试使用key: 'data'代替,但是它给了我与上面类似的错误。 How can I configure the token key properly so that I can store all the six properties of the token? 如何正确配置令牌密钥,以便可以存储令牌的所有六个属性?

Or, should I not attempt to get the other properties but should stick to idToken instead? 或者,我不应该尝试获取其他属性,而应该坚持使用idToken吗? In that case, how could I specify different tokens when the API returns something else in the payload (eg when requesting email verification Firebase only returns kind and email in the payload). 在那种情况下,当API在有效负载中返回其他内容时(例如,在请求电子邮件验证时,Firebase仅在有效负载中返回kindemail ),我如何指定不同的令牌。

Thank you very much. 非常感谢你。

try with this: 试试这个:

  key: 'idToken'
},

it is working for me, with a successful authResult. 它为我工作,具有成功的authResult。 Good Coding; 良好的编码;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM