2017-04-02 153 views
0

我有一个feathters.js应用程序,现在我想确保创建和更新挂钩。我使用一个socket.io客户端,目前正在为智威汤逊。我已经添加了我认为我需要添加的内容,但是获得了Error: Authentication token missingError Authenticating。我后来知道这是来自我的代码。我有一个后端/前端的情况feathers.js - >身份验证令牌丢失

所以这就是我到目前为止所实现的。

文件:后端\ backend.js(称为后端\ index.js用于该应用程序的配置)

'use strict'; 

const path = require('path'); 
const serveStatic = require('feathers').static; 
const favicon = require('serve-favicon'); 
const compress = require('compression'); 
const cors = require('cors'); 
const feathers = require('feathers'); 
const configuration = require('feathers-configuration'); 
const authentication = require('feathers-authentication'); 
const hooks = require('feathers-hooks'); 
const rest = require('feathers-rest'); 
const bodyParser = require('body-parser'); 
const socketio = require('feathers-socketio'); 
const middleware = require('./middleware/index'); 
const services = require('./services/index'); 

const appFeathers = feathers(); 

appFeathers.configure(configuration(path.join(__dirname, '..'))); 

appFeathers.use(compress()) 
    .options('*', cors()) 
    .use(cors()) 
    .use(favicon(path.join(appFeathers.get('public'), 'favicon.ico'))) 
    .use('/', serveStatic(appFeathers.get('public'))) 
    .use(bodyParser.json()) 
    .use(bodyParser.urlencoded({extended: true})) 
    .configure(hooks()) 
    .configure(rest()) 
    .configure(socketio()) 
    .configure(services) 
    .configure(middleware) 
    .configure(authentication()); 

module.exports = appFeathers; 

文件:后端\设置\ default.json

{ 
    "host": "localhost", 
    "port": 3001, 
    "mysql_connection": "mysql://CONNECTION_STRING", 
    "public": "../public/", 
    "auth": { 
     "idField": "id", 
     "token": { 
      "secret": "SECRET_KEY" 
     }, 
     "local": {} 
    } 
} 

在前端的工作部件:

<template> 
    <div class="vttIndex"> 
     idnex.vue 
     todo: eagle.js slideshow 
     todo: first info 
     <ul> 
      <li v-for="message in listMessages"> 
       {{ message }} 
      </li> 
     </ul> 
    </div> 
</template> 

<script> 
    import feathers from 'feathers/client'; 
    import socketio from 'feathers-socketio/client'; 
    import hooks from 'feathers-hooks'; 
    import io from 'socket.io-client'; 
    import authentication from 'feathers-authentication/client'; 
    import * as process from "../nuxt.config"; 

    const vttSocket = io(process.env.backendUrl); 
    const vttFeathers = feathers() 
     .configure(socketio(vttSocket)) 
     .configure(hooks()) 
     .configure(authentication()); 

    const serviceMessage = vttFeathers.service('messages'); 


vttFeathers.authenticate({ 
    type: 'token', 
    'token ': 'SECRET_KEY' 
}).then(function(result){ 
    console.log('Authenticated!', result); 
}).catch(function(error){ 
    console.error('Error authenticating!', error); 
}); 

    export default { 
     layout: 'default', 
     data: function() { 
      return { 
       listMessages: [] 
      } 
     }, 
     mounted: function() { 
      serviceMessage.find().then(page => { 
       this.listMessages = page.data; 
      }); 
      serviceMessage.on('created', (serviceMessage) => { 
       this.listMessages.push(serviceMessage); 
      }); 
     } 
    } 
</script> 

至于原因,我有后台的JSON文件的密钥。如您所见,现在我只尝试记录控制台消息。它正在做一些事情,我的错误消息是从那里来的。

问题

我在哪里丢失有什么这个功能?

目标

万一它的需要。我的目标是在我的客户端使用令牌选择所有“公共”数据,然后管理部分可能使用0auth。所以一般的'SELECT'东西通过一个令牌来保证,而不是根本不认证。

解决方案

好吧,我解决了这个问题,有点。我首先需要创建一个用户。然后,我需要与用户进行本地登录。这返回一个令牌。如果我使用它,那么根本没有问题。

+1

你明白了!登录用户是获得accessToken的预期工作流程。 –

回答

0

要使用令牌,您必须首先确保它已生成。我使用密钥作为标记,这是不正确的。当你首先使用“本地”类型(默认电子邮件和密码)进行身份验证时,它会创建一个令牌,然后可以使用该方法的“令牌”