本地验证¶
$ npm install @feathersjs/authentication-local --save
@feathersjs/authentication-local is a server side module that wraps the passport-local authentication strategy, which lets you authenticate with your Feathers application using a username and password.
This module contains 3 core pieces:
The main initialization function
The
hashPasswordhookThe
Verifierclass
Configuration¶
In most cases initializing the module is as simple as doing this:
const feathers = require('@feathersjs/feathers');
const authentication = require('@feathersjs/authentication');
const local = require('@feathersjs/authentication-local');
const app = feathers();
// Setup authentication
app.configure(authentication(settings));
app.configure(local());
// Setup a hook to only allow valid JWTs or successful
// local auth to authenticate and get new JWT access tokens
app.service('authentication').hooks({
before: {
create: [
authentication.hooks.authenticate(['local', 'jwt'])
]
}
});
This will pull from your global authentication object in your config file. It will also mix in the following defaults, which can be customized.
Options¶
{
name: 'local', // the name to use when invoking the authentication Strategy
entity: 'user', // the entity that you're comparing username/password against
service: 'users', // the service to look up the entity
usernameField: 'email', // key name of username field
passwordField: 'password', // key name of password field
entityUsernameField: 'email', // key name of the username field on the entity (defaults to `usernameField`)
entityPasswordField: 'password', // key name of the password on the entity (defaults to `passwordField`)
passReqToCallback: true, // whether the request object should be passed to `verify`
session: false // whether to use sessions,
Verifier: Verifier // A Verifier class. Defaults to the built-in one but can be a custom one. See below for details.
}
重要
When setting the usernameField to username in
the 配置 the value must be
escaped as \\username otherwise it will use the value of the
username environment variable on Windows systems.
hooks¶
hashPassword¶
This hook is used to hash plain text passwords before they are saved to
the database. It uses the bcrypt algorithm by default but can be
customized by passing your own options.hash function.
重要
@feathersjs/authentication-local does not allow to store clear text passwords.
This means the hashPassword hook must be used when using the Verifier.
Available options are
passwordField(default:'password') - key name of password field to look on context.datahash(default: bcrypt hash function) - Takes in a password and returns a hash.
const local = require('@feathersjs/authentication-local');
app.service('users').hooks({
before: {
create: [
local.hooks.hashPassword()
]
}
});
protect¶
The protect hook makes sure that protected fields don’t get sent to a client.
const local = require('@feathersjs/authentication-local');
app.service('users').hooks({
after: local.hooks.protect('password')
});
Verifier¶
这是验证类, 它通过``usernameField``在给定服务上查找实体(通常是``user``)并使用bcrypt比较散列密码来进行实际的用户名和密码验证.它具有以下可以覆盖的方法.除了``verify``之外, 所有方法都返回一个promise, 它与`passport-local <https://github.com/jaredhanson/passport-local>`_具有完全相同的签名.
{
constructor(app, options) // the class constructor
_comparePassword(entity, password) // compares password using bcrypt
_normalizeResult(result) // normalizes result from service to account for pagination
verify(req, username, password, done) // queries the service and calls the other internal functions.
}
可以扩展``Verifier``类, 以便您可以自定义它的行为, 而无需重写和测试完全自定义的本地Passport实现.虽然如果您不想使用此插件, 这始终是一个选项.
An example of customizing the Verifier:
const { Verifier } = require('@feathersjs/authentication-local');
class CustomVerifier extends Verifier {
// The verify function has the exact same inputs and
// return values as a vanilla passport strategy
verify(req, username, password, done) {
// do your custom stuff. You can call internal Verifier methods
// and reference this.app and this.options. This method must be implemented.
// the 'user' variable can be any truthy value
// the 'payload' is the payload for the JWT access token that is generated after successful authentication
done(null, user, payload);
}
}
app.configure(local({ Verifier: CustomVerifier }));
Client Usage¶
authentication-client¶
When this module is registered server side, using the default config values this is how you can authenticate using 验证客户端.
app.authenticate({
strategy: 'local',
email: 'your email',
password: 'your password'
}).then(response => {
// You are now authenticated
});
HTTP Request¶
If you are not using the @feathersjs/authentication-client and you
have registered this module server side, make a POST request to
/authentication with the following payload:
// POST /authentication the Content-Type header set to application/json
{
"strategy": "local",
"email": "your email",
"password": "your password"
}
Here is what that looks like with curl:
curl -H "Content-Type: application/json" -X POST -d '{"strategy":"local","email":"your email","password":"your password"}' http://localhost:3030/authentication
Sockets¶
Authenticating using a local strategy via sockets is done by emitting the following message:
const io = require('socket.io-client');
const socket = io('http://localhost:3030');
socket.emit('authenticate', {
strategy: 'local',
email: 'your email',
password: 'your password'
}, function(message, data) {
console.log(message); // message will be null
console.log(data); // data will be {"accessToken": "your token"}
// You can now send authenticated messages to the server
});

