Feathers客户端(@ feathers/client)无法在ReactJs应用程序中验证用户名/密码 [英] Feathers client (@feathers/client) is unable to authenticate a username/password in an ReactJs app
问题描述
根据 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.
对于此应用:
- 我目前正在开发服务器上.
- ReactJs应用程序位于localhost:3000上
- 位于localhost:3030上的FeathersJs应用程序.
- React应用由CRA引导,而Feathers服务器由CLI生成.
- 在React应用程序中,我正在使用npm中的
@feathersjs/client
包.
- I am currently working on development servers.
- The ReactJs app is on localhost:3000
- The FeathersJs app in on localhost:3030.
- The React app is bootstrapped with CRA and the Feathers server is generated by the CLI.
- In the React app I am using the
@feathersjs/client
package from npm.
我已经尝试通过终端中的curl
请求服务器,并且使用正确的凭据进行响应.之后,我通过React应用程序发出了AJAX请求,该请求也有效.如果我使用AJAX请求进行身份验证,则可以成功获取用户的token
和id
.
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客户端,因为它提供了现成的reAuthenticate
和logout
方法.
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屋!