Feathers客户端(@ feathers/client)无法在ReactJs应用程序中验证用户名/密码 [英] Feathers client (@feathers/client) is unable to authenticate a username/password in an ReactJs app

查看:301
本文介绍了Feathers客户端(@ feathers/client)无法在ReactJs应用程序中验证用户名/密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Featherjs客户端身份验证文档,我已经进行了设置并在我的React App中初始化了模块.在单击Login按钮后,我以正确的数据格式调用推荐的app.authenticate(data)方法.这会导致客户端软件包feathers.js文件中的Uncaught TypeError: Cannot read property 'create' of undefined立即出现错误.

According to the Featherjs Client Authentication docs, I have setup and initialised the module in my React App. After that on my Login button click, I call the recommended app.authenticate(data) method with the correct data format. This results in an error immediately Uncaught TypeError: Cannot read property 'create' of undefined in the feathers.js file from the client package.

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

It will be very helpful if you could point me to the right direction.

对于此应用:

  1. 我目前正在开发服务器上.
  2. ReactJs应用程序位于localhost:3000上
  3. 位于localhost:3030上的FeathersJs应用程序.
  4. React应用由CRA引导,而Feathers服务器由CLI生成.
  5. 在React应用程序中,我正在使用npm中的@feathersjs/client包.
  1. I am currently working on development servers.
  2. The ReactJs app is on localhost:3000
  3. The FeathersJs app in on localhost:3030.
  4. The React app is bootstrapped with CRA and the Feathers server is generated by the CLI.
  5. In the React app I am using the @feathersjs/client package from npm.

我已经尝试通过终端中的curl请求服务器,并且使用正确的凭据进行响应.之后,我通过React应用程序发出了AJAX请求,该请求也有效.如果我使用AJAX请求进行身份验证,则可以成功获取用户的tokenid.

I have already tried to request the server via curl in the terminal and that responds with the correct credentials. Following that, I made an AJAX request via the React app and that also worked. If I use AJAX request to do the authentication, I successfully get the token and the id of the user.

实际上,我可以继续进行下去.但是会出现问题,需要使用相同的令牌对用户进行重新身份验证并注销用户.我确实知道有解决方法.但是我想使用FeathersJs客户端,因为它提供了现成的reAuthenticatelogout方法.

In reality I can further carry on. But the problem occurs to re-authenticate the user with the same token and to logout the user. I do understand that there are workarounds. But I would like to use the FeathersJs Client side as it provides ready to use reAuthenticate and logout methods.

启动羽毛客户端

import feathers from '@feathersjs/client';

const client = feathers();

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

export default client;

App.js中的

login函数称为

login function in App.js called

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
        });
      });
    };

调用该函数时,将引发以下错误:

When the function is called the following error is thrown:

在开发控制台中

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应用程序的浏览器中,显示以下错误:

In the browser running the ReactJs application, the following error is shown:

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客户?

What am I doing wrong and how can I make use of the FeathersJs client?

推荐答案

您忘记初始化 Socket.io 客户端连接,如 React Native API .应该是

You forgot to initialize a REST or Socket.io client connection as also shown in the React Native API. It should be

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;

这篇关于Feathers客户端(@ feathers/client)无法在ReactJs应用程序中验证用户名/密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆