Closed ghost closed 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.
@marshallswain I can't to develop feathers app like express ? only create an server.js and package.json for our modules ?
You most certainly can. I'm only suggesting you compare because I don't see the problem right away.
I can add the default config of an project generated to my project ?
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}`)
})
I must create an folder and inside it i paste the default file before add this code in my server.js ?
I recommend taking inspiration from the generated application. There are lots os possible options, but the generator will guide you.
Thank you very much for your help.
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>
Thank you , thank you , thank you very much :+1: :+1: :+1:
I have an file server.js
and when i click my submit buutton i have this error
Authentication strategy 'local' is not registered