如何创建多个Feathers客户端连接?

时间:2019-03-30 06:19:21

标签: feathersjs feathers-authentication

我有一个Slack机器人的eventListener来监听传入消息:

slackEvents.on('message', (message: any, body: any) => {
  BotMessageInterpreter.handleIncomingMessage(app, message, body);
});

然后根据消息来自哪个平台创建bot实例(松弛不是唯一的一个)。 我想验证写邮件的用户,以便他/她可以例如在收件箱中获取他/她的邮件。可能会进行很多身份验证 可能是来自不同用户的许多邮件。

static handleIncomingMessage(app: any, message: any, body: any): void { 
    const bot = BotRequestFactory.createBotRequest(body.token);
    const authentication = bot.authenticate(body.email, body.password);

    if (authentication instanceof Error) {
      console.log('Error creating bot. Unknown type.');
    } else if (!authentication) {
      bot.sendMessageToPlatform(body, 'Error: Could not authenticate you.');
    } 

    bot.sendMessageToPlatform(body.event, message.text);
  }

不同的机器人类型类别如下:

import request from 'request';

import { Authentication } from '../authentication';

export class BotType1 {
  private data: any;
  private app: any;
  private authentication: any;

  constructor(app: any, data: any) {
    this.app = app;
    this.data = data;
    this.authentication = new Authentication(app);
  }

  public authenticate(email: string, password: string): boolean {
    // Unique payload specific to Slack
    const payload = {
      strategy: 'local',
      email: process.env.FEATHERS_AUTHENTICATION_EMAIL,
      password: process.env.FEATHERS_AUTHENTICATION_PASSWORD
    };

    // Authenticate
    const success = this.app.authenticate(payload)
    .then((response: any) => { 
      return true; 
    })
    .catch((e: any) => {
      console.log('Error. Could not authenticate the user.');
      return false; 
    });

    return success;
  }
}

第一次运行正常,但是第二次我发送一条消息并调用authenticate方法时,出现以下错误:

Error: Only one default client provider can be configured
    at Function.initialize (/Users/test/Documents/projects/bot/node_modules/@feathersjs/client/dist/feathers.js:4993:13)
    at Function.configure (/Users/test/Documents/projects/bot/node_modules/@feathersjs/client/dist/feathers.js:3507:8)
    at new Authentication (/Users/test/Documents/projects/bot/app/classes/authentication.ts:14:13)
    at new SlackBotRequest (/Users/test/Documents/projects/bot/app/classes/bot-engine/bot-request.ts:14:31)
    at Function.createBotRequest (/Users/test/Documents/projects/bot/app/classes/bot-engine/bot-request-factory.ts:9:24)
    at Function.handleIncomingMessage (/Users/test/Documents/projects/bot/app/classes/bot-engine/bot-message-interpreter.ts:7:61)
    at SlackEventAdapter.slackEvents.on (/Users/test/Documents/projects/bot/app/app.ts:42:57)
    at emitTwo (events.js:126:13)
    at SlackEventAdapter.emit (events.js:214:7)
    at /Users/test/Documents/projects/bot/node_modules/@slack/events-api/dist/http-handler.js:244:22

这是我的身份验证文件:

const io = require('socket.io-client');
const feathers = require('@feathersjs/client');
const localStorage = require('localstorage-memory');

export class Authentication {
  private socket: any;
  private client: any;
  protected app: any;

  constructor(app: any) {
    this.app = app;
    this.client = feathers();

    this.socket = io('http://localhost:3030/', {
      transports: ['websocket'],
      forceNew: true
    });

    app.configure(feathers.socketio(this.socket), {
      timeout: 10000
    });

    app.configure(feathers.authentication({
      jwtStrategy: 'jwt',
      storage: localStorage,
      storageKey: 'bot-token'
    }));
  }

}

我收到该特定错误的原因是什么,我该如何解决?非常感谢!

0 个答案:

没有答案