2017-04-02 12 views
0

私はfeathters.jsアプリケーションを持っていますが、今度は作成と更新のフックを保護したいと思います。私はsocket.ioクライアントを使い、現在はJWTに行く予定です。私は追加する必要があると思ったものを追加しましたが、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; 

ファイル:バックエンド\ CONFIG \ 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

トークンを使用するには、トークンが生成されていることを確認する必要があります。私は秘密鍵をトークンとして使用していましたが、これは正しくありません。最初に 'ローカル'タイプ(デフォルトの電子メールとパスワード)で認証すると、トークンが作成され、それが「トークン」メソッドと一緒に使用できます。

関連する問題