【问题标题】:Feathers client (@feathers/client) is unable to authenticate a username/password in an ReactJs appFeathers 客户端 (@feathers/client) 无法在 ReactJs 应用程序中验证用户名/密码
【发布时间】:2020-01-05 20:09:28
【问题描述】:

根据Featherjs Client Authentication docs,我已经在我的 React App 中设置并初始化了模块。然后在我的Login 按钮点击之后,我使用正确的数据格式调用推荐的app.authenticate(data) 方法。这会立即导致客户端包中的feathers.js 文件中的Uncaught TypeError: Cannot read property 'create' of undefined 出现错误。

如果您能指出正确的方向,那将非常有帮助。

对于这个应用程序:

  1. 我目前正在开发服务器。
  2. ReactJs 应用位于 localhost:3000
  3. FeathersJs 应用程序位于 localhost:3030。
  4. React 应用由 CRA 引导,Feathers 服务器由 CLI 生成。
  5. 在 React 应用程序中,我使用来自 npm 的 @feathersjs/client 包。

我已经尝试通过终端中的curl 请求服务器,并使用正确的凭据进行响应。之后,我通过 React 应用程序发出了 AJAX 请求,这也有效。如果我使用AJAX请求进行认证,我成功获得了用户的tokenid

实际上我可以继续进行下去。但是使用相同的令牌重新验证用户并注销用户时会出现问题。我明白有一些解决方法。但我想使用 FeathersJs 客户端,因为它提供了可以使用的 reAuthenticatelogout 方法。

初始化羽毛客户端

import feathers from '@feathersjs/client';

const client = feathers();

client.configure(feathers.authentication({
  storage: window.localStorage
}));

export default client;

App.js 中的login 函数调用

login = () => {
      const self = this;
      client.authenticate({
        strategy: 'local',
        email: 'hello@robin.com',
        password: 'supersecret'
      })
      .then(data => {
        console.log(data);
        self.setState({
          isAuthenticated: true
        });
        return data;
      })
      .catch(err => {
        console.log(err);
        self.setState({
          isAuthenticated: false
        });
      });
    };

调用函数时会抛出以下错误:

在开发控制台中

Uncaught TypeError: Cannot read property 'create' of undefined
    at AuthenticationClient.authenticate (feathers.js:2838)
    at Object.App.login (App.js:50)
    at onClick (Login.js:8)

在运行ReactJs应用的浏览器中,显示如下错误:

TypeError: Cannot read property 'create' of undefined
AuthenticationClient.authenticate
node_modules/@feathersjs/client/dist/feathers.js:2838

> 2838 | var promise = this.service.create(authentication).then(function (authResult) {


我做错了什么,如何使用 FeathersJs 客户端?

【问题讨论】:

    标签: reactjs feathersjs feathers-authentication


    【解决方案1】:

    您忘记初始化RESTSocket.io 客户端连接,如React Native API 中所示。应该是

    import io from 'socket.io-client';
    import feathers from '@feathersjs/feathers';
    import socketio from '@feathersjs/socketio-client';
    
    const socket = io('http://api.my-feathers-server.com', {
      transports: ['websocket'],
      forceNew: true
    });
    const client = feathers();
    
    client.configure(socketio(socket));
    client.configure(feathers.authentication({
      storage: window.localStorage
    }));
    
    export default client;
    

    【讨论】:

      猜你喜欢
      • 2021-01-01
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-03
      • 2021-11-10
      相关资源
      最近更新 更多