feathersjs / feathers

The API and real-time application framework
https://feathersjs.com
MIT License
15.06k stars 752 forks source link

local authentication configuration is not working #1654

Closed Chellhooo closed 4 years ago

Chellhooo commented 4 years ago

hi. After i update feathers to v4,following error occurs Error: 'local' authentication strategy requires a 'usernameField' setting.

I do as what the document said, do I miss something important?:(

my authentication.js is following.

const { AuthenticationService, JWTStrategy } = require('@feathersjs/authentication');
const { LocalStrategy } = require('@feathersjs/authentication-local');
const { expressOauth } = require('@feathersjs/authentication-oauth');

module.exports = app => {
  const config = app.get('authentication');
  const authService = new AuthenticationService(app, config);

  authService.register('jwt', new JWTStrategy());
  authService.register('local', new LocalStrategy());

  app.use('/authentication', authService);
  app.configure(expressOauth());
};

and my authentication config like this:

"authentication": {
    "local": {
      "usernameField": "email",
      "passwordField": "password"
    },
  },
daffl commented 4 years ago

It should be:

const { AuthenticationService, JWTStrategy } = require('@feathersjs/authentication');
const { LocalStrategy } = require('@feathersjs/authentication-local');
const { expressOauth } = require('@feathersjs/authentication-oauth');

module.exports = app => {
  const authService = new AuthenticationService(app);

  authService.register('jwt', new JWTStrategy());
  authService.register('local', new LocalStrategy());

  app.use('/authentication', authService);
  app.configure(expressOauth());
};

Also see API docs

ericuldall commented 4 years ago

I'm implementing this the way you suggest @daffl, yet I'm still getting an error saying I haven't set the usernameField config. Any thoughts?

Auth.js

const authentication = require('@feathersjs/authentication')
const authenticationLocal = require('@feathersjs/authentication-local')
const authenticationOauth = require('@feathersjs/authentication-oauth')

const { AuthenticationService, JWTStrategy } = authentication
const { LocalStrategy } = authenticationLocal
const { express: oauth, OAuthStrategy } = authenticationOauth

module.exports = function auth(app) {
  const authService = new AuthenticationService(app)

  // register all of the strategies with authentication service
  authService.register('local', new LocalStrategy())
  authService.register('jwt', new JWTStrategy())
  authService.register('google', new OAuthStrategy())

  // register the authentication service with your app
  app.use('/authentication', authService)

  // register the oauth middleware with your app
  app.configure(oauth())
}

config/default.json

{
  "app": {
    "port": 3030
  },
  "authentication": {
    "local": {
      "usernameField": "email",
      "passwordField": "password"
    }
  }
}

app.js

const util = require('util');
const config = require('config');
const feathers = require('@feathersjs/feathers');
const express = require('@feathersjs/express');
const socketio = require('@feathersjs/socketio');

const MongoClient = require('mongodb').MongoClient;
const service = require('feathers-mongodb');
//const StorageService = require('./services/storage');

const auth = require('./modules/auth');

// Creates an ExpressJS compatible Feathers application
const app = express(feathers());

// Parse HTTP JSON bodies
app.use(express.json());
// Parse URL-encoded params
app.use(express.urlencoded({ extended: true }));
app.configure(auth);
// Add REST API support
app.configure(express.rest());
// Configure Socket.io real-time APIs
app.configure(socketio());
// Register an in-memory messages service
//app.use('/storage', new StorageService());
// Register a nicer error handler than the default Express one
app.use(express.errorHandler());

MongoClient.connect('mongodb://localhost:27017/amino').then(client => {
  app.use('/users', service({
    Model: client.db('feathers').collections('users')
  }));
  app.use('/storage', service({
    Model: client.db('feathers').collection('storage')
  }));
  //app.use('/storage', service({ Model, id, events, paginate }));
});

// Add any new real-time connection to the `everybody` channel
app.on('connection', connection =>
  app.channel('everybody').join(connection)
);
// Publish all events to the `everybody` channel
app.publish(data => app.channel('everybody'));

// Start the server
app.listen(config.app.port).on('listening', () =>
  console.log(util.format('Feathers server listening on localhost:%s', config.app.port))
);