2017-06-12 4 views
0

Ich dachte, um einen Stack für meinen Fehler jetzt zu machen. Ich hatte einige Probleme, die Authentifizierung auslösen, aber das war für mein Arbeitsprojekt eine andere Version. Ich hatte auch ein Problem mit einer anderen Service und Spalte Namenskonvention als die Standardeinstellung. Dann scheiterte es an Sequelize und mssql mit dem 'FETCH' 'NEXT' was ich gelöst habe.feathersjs -> Fehler 401 mit der Nachricht 'Fehler' und NotAuthenticated

Umwelt

ich auf einem Linux-System entwickle. Die Datenbank, die verwendet wird, befindet sich derzeit in SQL2016. Alle Selects sind in Ordnung, und fügt/Updates für bevor ich Authentifizierung aktiviert habe ich Sachen Einfügen/Aktualisieren in den Tabellen. Versionen von Server und Client

Server 
    "feathers": "^2.1.1", 
    "feathers-authentication": "^1.2.1", 
    "feathers-authentication-jwt": "^0.3.1", 
    "feathers-authentication-local": "^0.3.4", 
    "feathers-configuration": "^0.3.3", 
    "feathers-errors": "^2.6.2", 
    "feathers-hooks": "^1.8.1", 
    "feathers-rest": "^1.7.1", 
    "feathers-sequelize": "^1.4.5", 
    "feathers-socketio": "^1.5.2", 

Client 
    "feathers": "^2.1.2", 
    "feathers-authentication": "^1.2.4", 
    "feathers-authentication-client": "^0.3.2", 
    "feathers-client": "^2.2.0", 
    "feathers-localstorage": "^1.0.0", 
    "feathers-socketio": "^2.0.0", 

Ausgabe

Wenn ich eine Authentifizierung auf einem Client zu starten, die Strategie lokaler gesetzt ist, erhalte ich die Fehler folgenden unten, während ich würde erwarten, ‚authentifiziert werden 'für den Benutzer und passord ist korrekt.

Fehler

Error authenticating! { type: 'FeathersError', 
    name: 'NotAuthenticated', 
    message: 'Error', 
    code: 401, 
    className: 'not-authenticated', 
    errors: {} } 

So offcourse einige Dateien benötigt werden. Lassen Sie uns zunächst mit der Backend-Seite beginnen. Ich habe mehrere "Cluster" von Diensten, so dass einige Codes verschoben werden müssen.

Datei: ./app.js

Dies ist die Hauptanwendungsdatei. Hier sehen Sie auch, wie mein Benutzer erstellt wird, den ich zum Testen verwende.

'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 jwt = require('feathers-authentication-jwt'); 
const local = require('feathers-authentication-local'); 
const hooks = require('feathers-hooks'); 
const rest = require('feathers-rest'); 
const bodyParser = require('body-parser'); 
const socketio = require('feathers-socketio'); 
const middleware = require('./middleware'); 
const servicesMfp = require('./services/A'); 
const servicesMic = require('./services/B'); 

const app = feathers(); 

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

app.use(compress()) 
    .options('*', cors()) 
    .use(cors()) 
    .use(favicon(path.join(app.get('public'), 'favicon.ico'))) 
    .use('/', serveStatic(app.get('public'))) 
    .use(bodyParser.json()) 
    .use(bodyParser.urlencoded({extended: true})) 
    .configure(hooks()) 
    .configure(rest()) 
    .configure(socketio()) 
    .configure(servicesMfp) 
    .configure(servicesMic) 
    .configure(middleware) 
    .configure(local({ 
     usernameField: 'user_name', 
     passwordField: 'user_password' 
    })) 
    .configure(jwt()); 

app.service('/mfp/authentication').hooks({ 
    before: { 
     create: [ 
      authentication.hooks.authenticate(['jwt', 'local']), 
     ], 
     remove: [ 
      authentication.hooks.authenticate('local') 
     ] 
    } 
}); 

/* 
const userService = app.service('/mfp/sys_users'); 
const User = { 
    user_email: '[email protected]', 
    user_password: 'ekoster', 
    user_name: 'ekoster2', 
    status_id: 1 
}; 
userService.create(User, {}).then(function(user) { 
    console.log('Created default user', user); 
}); 
*/ 

module.exports = app; 

Datei: ./services/multifunctionalportal/authentiction/index.js

'use strict'; 

const authentication = require('feathers-authentication'); 

module.exports = function() { 
    const app = this; 
    let config = app.get('mfp_auth'); 

    app.configure(authentication(config)); 
}; 

Datei: ./services/multifunctionalportal/sys_user/index.js

Dies ist die Indexdatei für den Service. Hier wird auch die Authentifizierung für die Daten in dieser Datenbank konfiguriert.

'use strict'; 
const authentication = require('./authentication/index'); 
const sys_user = require('./sys_user/index'); 
const sys_metadata = require('./sys_metadata/index'); 
const sys_term = require('./sys_term'); 
const sys_source = require('./sys_source/index'); 
const sys_source_user = require('./sys_source_user/index'); 
const survey = require('./survey/index'); 
const survey_question = require('./survey_question/index'); 
const Sequelize = require('sequelize'); 

module.exports = function() { 
    const app = this; 

    //TODO make it more cross DB (different dbtypes) 
    const sequelize = new Sequelize(app.get('mfp_db_database'), app.get('mfp_db_user'), app.get('mfp_db_password'), { 
     host: app.get('mfp_db_host'), 
     port: app.get('mfp_db_port'), 
     dialect: 'mssql', 
     logging: true, 
     dialectOptions: { 
      instanceName: app.get('mfp_db_instance') 
     } 
    }); 
    app.set('mfp_sequelize', sequelize); 

    app.configure(authentication); 
    app.configure(sys_user); 
    app.configure(sys_metadata); 
    app.configure(sys_term); 
    app.configure(sys_source); 
    app.configure(sys_source_user); 
    app.configure(survey); 
    app.configure(survey_question); 

    Object.keys(sequelize.models).forEach(function(modelName) { 
     if ("associate" in sequelize.models[modelName]) { 
      sequelize.models[modelName].associate(); 
     } 
    }); 

    sequelize.sync(
     { 
      force: false 
     } 
    ); 
}; 

Die Konfiguration in der Datei verwendet oben ist die folgende

"mfp_auth": { 
     "path": "/mfp/authentication", 
     "service": "/mfp/sys_users", 
     "entity": "sys_user", 
     "strategies": [ 
      "local", 
      "jwt" 
     ], 
     "secret": "WHO_KNOWS" 
    } 

file: ./services/multifunctionalportal/sys_user/sys_user-model.js

'use strict'; 

const Sequelize = require('sequelize'); 

module.exports = function (sequelize) { 
    const sys_user = sequelize.define('sys_users', { 
     user_email: { 
      type: Sequelize.STRING(256), 
      allowNull: false, 
      unique: true 
     }, 
     user_name: { 
      type: Sequelize.STRING(256), 
      allowNull: false, 
      unique: true 
     }, 
     user_password: { 
      type: Sequelize.STRING(256), 
      allowNull: false 
     }, 
     status_id: { 
      type: Sequelize.INTEGER, 
      allowNull: false 
     } 
    }, { 
     freezeTableName: true, 
     paranoid: true, 
     timestamps : true, 
     underscored : true, 
     classMethods: { 
      associate() { 
       sys_user.belongsTo(sequelize.models.sys_metadata, { 
        allowNull: false, 
        as: 'status' 
       }); 
       sys_user.hasMany(sequelize.models.sys_source_users, { 
        as: 'user', 
        foreignKey: 'user_id', 
        targetKey: 'user_id', 
        onDelete: 'no action' 
       }); 
      } 
     } 
    }); 

    sys_user.sync(); 

    return sys_user; 
}; 

Datei : ./services/multifunctionalportal/sys_user/hooks/index.js

'use strict'; 

const globalHooks = require('../../../../hooks'); 
const hooks = require('feathers-hooks'); 
const authentication = require('feathers-authentication'); 
const local = require('feathers-authentication-local'); 

exports.before = { 
    all: [], 
    find: [ 
     authentication.hooks.authenticate('jwt') 
    ], 
    get: [], 
    create: [ 
     local.hooks.hashPassword({ passwordField: 'user_password' }) 
    ], 
    update: [], 
    patch: [], 
    remove: [] 
}; 

exports.after = { 
    all: [], 
    find: [], 
    get: [], 
    create: [], 
    update: [], 
    patch: [], 
    remove: [] 
}; 

Als nächstes ist natürlich der Client. Ich habe nuxtjs, aber ich habe auch einen Kunden, der nicht nuxtjs ist und das gleiche Problem hat. Also stelle ich das für eine Datei und für das Debuggen einfacher.

'use strict'; 
const feathers = require('feathers/client'); 
const socketio = require('feathers-socketio/client'); 
const hooks = require('feathers-hooks'); 
const io = require('socket.io-client'); 
const authentication = require('feathers-authentication-client'); 
const localstorage = require('feathers-localstorage'); 
const process = require('../../config'); 
const winston = require('winston'); 
const tslog =() => (new Date()).toLocaleTimeString(); 

const mfpSocket = io(process.env.backendUrl); 
const mfpFeathers = feathers() 
    .configure(socketio(mfpSocket)) 
    .configure(hooks()) 
    .configure(authentication()); 

const surveyLog = new (winston.Logger)({ 
    transports: [ 
     new (winston.transports.Console)({ 
      timestamp: tslog, 
      colorize: true 
     }), 
     new (require('winston-daily-rotate-file'))({ 
      filename: process.env.logDirectory + '/-' + process.env.logFileSurvey, 
      timestamp: tslog, 
      datePattern: 'yyyyMMdd', 
      prepend: true, 
      level: process.env.logLevelSurvey 
     }) 
    ] 
}); 


//TODO login then continue 
mfpFeathers.authenticate({ 
    strategy: 'local', 
    'user_name': 'ekoster', 
    'user_password': 'ekoster2' 
}).then(function(result){ 
    console.log('Authenticated!', result); 
}).catch(function(error){ 
    console.error('Error authenticating!', error); 
}); 

Bei Bedarf ich diesen Code erweitern, denn ich habe die Sachen unter diesem Abschnitt entfernt, die nicht bei der Lösung half (irrelevent)

Anfrage

Ist es möglich, jemand kann Zeige mir in die richtige Richtung. Kann es sein, muss ich die benutzerdefinierten Felder noch irgendwo anders konfigurieren? Ich habe versucht, nach dem Problem zu suchen, um zu sehen, ob ich etwas in "Fehler:" einfügen kann, aber nur gefunden, dass es aus zwei Dateien in "feathers authenticate" zu kommen scheint, aber ich weiß nicht wo.

Solving

ich das Problem denke ist in einen Teil des Server-Setup mit in den ‚index.js‘ der Dienste, und einen Teil in den ‚app.js‘ des Backends . Nur ich sehe noch nicht wo.

[20170612 1630] Neue Dateien Ich habe einige Änderungen an einigen Dateien vorgenommen. Gleiches Ergebnis, passt aber besser. Scheint aber, dass ein nächster Schritt nicht genannt wird.

Datei: app.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 hooks = require('feathers-hooks'); 
const rest = require('feathers-rest'); 
const bodyParser = require('body-parser'); 
const socketio = require('feathers-socketio'); 
const middleware = require('./middleware'); 
const servicesMfp = require('./services/multifunctionalportal'); 
const servicesMic = require('./services/mexonincontrol'); 

const app = feathers(); 

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

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

/* 
const userService = app.service('/mfp/sys_users'); 
const User = { 
    user_email: '[email protected]', 
    user_password: 'ekoster', 
    user_name: 'ekoster2', 
    status_id: 1 
}; 
userService.create(User, {}).then(function(user) { 
    console.log('Created default user', user); 
}); 
*/ 

module.exports = app; 

Datei: ./services/multifunctionalportal/index.js

'use strict'; 
const authentication = require('./authentication/index'); 
const jwt = require('feathers-authentication-jwt'); 
const local = require('feathers-authentication-local'); 
const sys_user = require('./sys_user/index'); 
const sys_metadata = require('./sys_metadata/index'); 
const sys_term = require('./sys_term'); 
const sys_source = require('./sys_source/index'); 
const sys_source_user = require('./sys_source_user/index'); 
const survey = require('./survey/index'); 
const survey_question = require('./survey_question/index'); 
const Sequelize = require('sequelize'); 

module.exports = function() { 
    const app = this; 

    //TODO make it more cross DB (different dbtypes) 
    const sequelize = new Sequelize(app.get('mfp_db_database'), app.get('mfp_db_user'), app.get('mfp_db_password'), { 
     host: app.get('mfp_db_host'), 
     port: app.get('mfp_db_port'), 
     dialect: 'mssql', 
     logging: true, 
     dialectOptions: { 
      instanceName: app.get('mfp_db_instance') 
     } 
    }); 
    app.set('mfp_sequelize', sequelize); 

    app.configure(authentication); 
    app.configure(local({ 
     usernameField: 'user_name', 
     passwordField: 'user_password' 
    })); 
    app.configure(jwt()); 
    app.configure(sys_user); 
    app.configure(sys_metadata); 
    app.configure(sys_term); 
    app.configure(sys_source); 
    app.configure(sys_source_user); 
    app.configure(survey); 
    app.configure(survey_question); 

    Object.keys(sequelize.models).forEach(function(modelName) { 
     if ("associate" in sequelize.models[modelName]) { 
      sequelize.models[modelName].associate(); 
     } 
    }); 

    sequelize.sync(
     { 
      force: false 
     } 
    ); 
}; 

Datei: ./services/multifunctionalportal/authentication/index .js

'use strict'; 
const authentication = require('feathers-authentication'); 

module.exports = function() { 
    const app = this; 
    const config = app.get('mfp_auth'); 
    const authService = app.service('/mfp/authentication'); 

    app.configure(authentication(config)); 

    authService.before({ 
     create: [ 
      authentication.hooks.authenticate(['jwt', 'local']), 
     ], 
     remove: [ 
      authentication.hooks.authenticate('local') 
     ] 
    }); 
}; 

[20.170.612 16.45] Änderung ‚erfordern‘ ändert die Fehler

I geändert haben, die für die Authentifizierung erfordern in‘./services/multifunctionalportal/index.js' von„erfordern (./ Authentifizierungs/index) "to" erfordern ('feaths-authentication') "und jetzt gibt es einen Fehler und gibt an, den app.passport nicht zu finden. Und wenn die Authentifizierung vor der Authentifizierung lokal konfiguriert ist, ist dies der Fall.

[20170612 19:00] Verschoben Konfiguration zur Authentifizierung

meine Config-Einstellungen für die anthentication So in den 'index.js' war der Dienst 'multifunctionalportal/Authentifizierung'. Ich habe das auf die 'index.js' der Dienste verschoben und jetzt ist diese Nachricht weg, aber ich habe jetzt ein benutzerloses Token. Wenn ich also ein falsches Passwort eingabe, erstellt es immer noch ein Token. Wenn Sie im Back-End-Protokoll suchen, wird keine Benutzerauswahl angezeigt.

[20170612 20.00] In einer Schleife

Diese letzte Änderung wird durch fehlende Haken verursacht. Die Hooks zur Authentifizierung befinden sich derzeit in der Datei index.js des Authentifizierungsdienstes. Wenn ich sie in die app.js verschiebe, ist das Problem weg und die Nachricht keine Authentifizierung gibt zurück. Es scheint also, dass eine Konfiguration nicht korrekt ist. Ich suche derzeit, ob ich eine Fehlermeldung im 'messages'-Teil des Anfangsfehlers

+0

Können Sie Ihr brechendes Repository irgendwo auf GitHub oder BitBucket ablegen, um den Fehler zu reproduzieren? Diese Menge an Code ist in einer SO-Frage schwer zu verstehen. – Daff

Antwort

0

lösen konnte. Lösung hier war, dass die Einfügung eines Testbenutzers die Sequenz' user_password '' user_name 'hatte und der Login-Test mit' user_name 'war '' user_password '' Ich kam über dies mit einem neuen Benutzer, wo der Benutzer/Passwort gleich war.Als dieser Benutzer arbeitete, fand ich es heraus.

Der Fehler besagt nicht, dass die Anmeldung aufgrund eines falschen Passworts fehlgeschlagen ist, aber wenn Sie DEBUG=feathers-authentication* npm start tun, wird dies angezeigt.