项目作者: aa403210842

项目描述 :
Anonymous authentication strategy for feathers-authentication using Passport
高级语言: JavaScript
项目地址: git://github.com/aa403210842/feathers-authentication-anonymous.git


feathers-authentication-anonymous

Anonymous authentication strategy for feathers-authentication using Passport

Installation

  1. npm install feathers-authentication-anonymous --save

API

Main Initialization

In most cases initializing the feathers-authentication-anonymous module is as simple as doing this:

  1. const anonymous = require('feathers-authentication-anonymous')
  2. ...
  3. app.configure(authentication(settings));
  4. app.configure(anonymous());

This will pull from your global auth object in your config file. It will also mix in the following defaults, which can be customized.

Default Options

  1. {
  2. name: 'anonymous', // the name to use when invoking the authentication Strategy
  3. }

just one config

  1. app.configure(anonymous({name: 'anon'}));
  2. // use in hook
  3. auth.hooks.authenticate(['jwt', 'anon'])
  4. // use middleware
  5. app.post('/users', auth.express.authenticate(['jwt', 'anon']), function(req, res) {
  6. if (req.user) {
  7. ...
  8. } else {
  9. ...
  10. }
  11. });

Additional passport-anonymous options can be provided.

Hooks

This usefull for endpoint, return difference results

  1. const hooks = require('feathers-hooks-common')
  2. function afterGetUsers () {
  3. return function(hook) {
  4. const { authenticated } = hook.params
  5. if (!authenticated) {
  6. // delete password field
  7. hook.result = omit(hook.result, ['password'])
  8. }
  9. return hook
  10. }
  11. }
  12. //user remove hook
  13. function isAuthenticated () {
  14. return function(hook) {
  15. const { authenticated } = hook.params
  16. return authenticated
  17. }
  18. }
  19. app.service('users').hooks({
  20. before: {
  21. get: [
  22. // You can chain multiple strategies
  23. // it while pass if jwt auth error
  24. // but params.user is undefined
  25. // and params.authenticated is undefined
  26. auth.hooks.authenticate(['jwt', 'anonymous']),
  27. ],
  28. remove: [
  29. auth.hooks.authenticate('jwt')
  30. ]
  31. }
  32. after: {
  33. get: [
  34. afterGetUsers()
  35. // or
  36. hooks.iff(isAuthenticated(), hooks.remove('password'))
  37. ]
  38. }
  39. });

Complete Example

Here’s a basic example of a Feathers server that uses feathers-authentication-anonymous. You can see a fully working example in the example/ directory.

  1. const feathers = require('feathers');
  2. const rest = require('feathers-rest');
  3. const hooks = require('feathers-hooks');
  4. const memory = require('feathers-memory');
  5. const bodyParser = require('body-parser');
  6. const errorHandler = require('feathers-errors/handler');
  7. const auth = require('feathers-authentication');
  8. const jwt = require('feathers-authentication-jwt');
  9. const anonymous = require('feathers-authentication-anonymous')
  10. // Initialize the application
  11. const app = feathers()
  12. .configure(rest())
  13. .configure(hooks())
  14. // Needed for parsing bodies (login)
  15. .use(bodyParser.json())
  16. .use(bodyParser.urlencoded({ extended: true }))
  17. // Configure feathers-authentication
  18. .configure(auth({ secret: 'super secret' }))
  19. .configure(jwt())
  20. .configure(anonymous())
  21. .use('/users', memory())
  22. .use(errorHandler());
  23. app.listen(3030);
  24. console.log('Feathers app started on 127.0.0.1:3030');

License

Copyright (c) 2016

Licensed under the MIT license.