feathersjs-ecosystem / authentication-local

[MOVED] Local authentication plugin for feathers-authentication
https://github.com/feathersjs/feathers
MIT License
26 stars 15 forks source link

How configure local strategy to feathers-authentication? #36

Closed ghost closed 7 years ago

ghost commented 7 years ago

I have an file server.js

const feathers = require('feathers');
const bodyParser = require('body-parser');
const errors = require('feathers-errors');
const errorHandler = require('feathers-errors/handler');
const rest = require('feathers-rest');
const hooks = require('feathers-hooks');
const auth = require('feathers-authentication');
const jwt = require('feathers-authentication-jwt');
const local = require('feathers-authentication-local');
const memory = require('feathers-memory');
var MongoClient = require('mongodb').MongoClient;
const service = require('feathers-mongodb');
const app = feathers();

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }));
var cors = require('cors');
var port= process.env.PORT || 4200;
app.use(cors());
app.configure(rest())
.configure(hooks())
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
.configure(auth({ secret: 'supersecret' }))`
`MongoClient.connect(url).then(db => {
    app.use('/users', service({
      Model: db.collection('Users_collection')
    }));
    //app.use('/users', service({ Model, id, events, paginate }));
    app.configure(jwt())
       .configure(local());

const config=app.get('authentication');
console.log(config);
    app.service('users').hooks({
    before: {

      create: [
        local.hooks.hashPassword({ passwordField: 'password' })
      ]
    }
  });
  app.service('authentication').hooks({
  before: {
    create: [
      auth.hooks.authenticate('local'),

    ],
    remove: [
      auth.hooks.authenticate('jwt')
    ]
  }
  });
});`
`app.listen(port, () => {
    console.log(`Feathers server on port ${port}`);

  });

and when i click my submit buutton i have this error Authentication strategy 'local' is not registered

marshallswain commented 7 years ago

It's not immediately apparent what's wrong in the code you posted. I recommend using the feathers-cli to generate a new project feathers generate app then feathers generate authentication. Then compare the setup.

ghost commented 7 years ago

@marshallswain I can't to develop feathers app like express ? only create an server.js and package.json for our modules ?

marshallswain commented 7 years ago

You most certainly can. I'm only suggesting you compare because I don't see the problem right away.

ghost commented 7 years ago

I can add the default config of an project generated to my project ?

marshallswain commented 7 years ago

Sure, just use feathers-configuration. This is probably a better starting point:

const feathers = require('feathers')
const bodyParser = require('body-parser')
const errors = require('feathers-errors')
const errorHandler = require('feathers-errors/handler')
const rest = require('feathers-rest')
const hooks = require('feathers-hooks')
const auth = require('feathers-authentication')
const jwt = require('feathers-authentication-jwt')
const local = require('feathers-authentication-local')
const memory = require('feathers-memory')
const path = require('path')
const configuration = require('feathers-configuration')

var MongoClient = require('mongodb').MongoClient
const service = require('feathers-mongodb')
var cors = require('cors')
var port = process.env.PORT || 4200

const app = feathers()

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

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cors())
app.configure(rest())
.configure(hooks())
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
.configure(auth({ secret: 'supersecret' }))
.configure(jwt())
.configure(local())

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

MongoClient.connect(url).then(db => {
  app.use('/users', service({ Model: db.collection('Users_collection') }))

  app.service('users').hooks({
    before: {},
    create: [
      local.hooks.hashPassword({ passwordField: 'password' })
    ]
  })
})

app.listen(port, () => {
  console.log(`Feathers server on port ${port}`)
})
ghost commented 7 years ago

I must create an folder and inside it i paste the default file before add this code in my server.js ?

marshallswain commented 7 years ago

I recommend taking inspiration from the generated application. There are lots os possible options, but the generator will guide you.

ghost commented 7 years ago

Thank you very much for your help.

daffl commented 7 years ago

I got it working with the following code:

const feathers = require('feathers')
const bodyParser = require('body-parser')
const errors = require('feathers-errors')
const errorHandler = require('feathers-errors/handler')
const rest = require('feathers-rest')
const hooks = require('feathers-hooks')
const auth = require('feathers-authentication')
const jwt = require('feathers-authentication-jwt')
const local = require('feathers-authentication-local')
const path = require('path')

var MongoClient = require('mongodb').MongoClient
const service = require('feathers-mongodb')
var cors = require('cors')
var port = process.env.PORT || 4200
var url = 'mongodb://localhost:27017/feathers-test';

MongoClient.connect(url).then(db => {
  const app = feathers()

    app.use(bodyParser.json())
    app.use(bodyParser.urlencoded({ extended: true }))
    app.use(feathers.static(__dirname))
    app.use(cors())
    app.configure(rest())
      .configure(hooks())
      .use(bodyParser.json())
      .use(bodyParser.urlencoded({ extended: true }))
      .configure(auth({ secret: 'supersecret' }))
      .configure(jwt())
      .configure(local())

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

  app.use('/users', service({ Model: db.collection('Users_collection') }))

  app.service('users').hooks({
    before: {
      create: [
        local.hooks.hashPassword({ passwordField: 'password' })
      ]
    }
  })

  app.listen(port, () => {
    console.log(`Feathers server on port ${port}`)
  })
}).catch(e => console.error(e));

Now you can create a new user from a Linux or Mac OS command line via

curl 'http://localhost:4200/users/' -H 'Content-Type: application/json' --data-binary '{ "email": "test@example.com", "password": "secret" }'

And then get back an authentication token via

curl 'http://localhost:4200/authentication/' -H 'Content-Type: application/json' --data-binary '{ "strategy": "local", "email": "test@example.com", "password": "secret" }'

I also verified that the following Feathers client setup works and returns a valid token:

<!DOCTYPE html>
<html>
<head>
  <title>Feathers client</title>
</head>
<body>
<script src="//unpkg.com/feathers-client@^2.0.0/dist/feathers.js"></script>
<script>
  // feathers-client is exposed as the `feathers` global.
  var app = feathers()
    .configure(feathers.hooks())
    .configure(feathers.rest().fetch(fetch))
    .configure(feathers.authentication())

  app.authenticate({
    "strategy": "local",
    "email": "test@example.com",
    "password": "secret"
  }).then(result => {
    console.log('Client authenticated', result);
  });
</script>
</body>
</html>
ghost commented 7 years ago

Thank you , thank you , thank you very much :+1: :+1: :+1: