How to use passport-local to authenticate in compo

2019-01-18 12:27发布

问题:

I want to use passport-local to authenticate user to login into composer rest server like other passport strategies ( e.g passport-github,passport-google).

So first I try to set COMPOSER_PROVIDER variable like this

"local": {
    "provider": "local",
    "module": "passport-local",
    "usernameField": "username",
    "passwordField": "password",
    "authPath": "/auth/local",
    "successRedirect": "/",
    "failureRedirect": "/"}

Then i run the server in docker (with mongo as persisted datasource) and add some user in database collection

The question is what's next step that i need to use this passport.Because i run this command and still get response with 401 Unauthorized

curl -H "Content-Type: application/json" -X POST -d '{"username":"{USER_NAME}","password":"{USER_PASSWORD}"}' http://localhost:3000/auth/local

Is it not enough to use this passport? Does i need to start another service to locally authenticate this login (e.g. GitHub oAuth Application )?

回答1:

It is possible to customize your composer-rest-server, based on the loopback framework, according to your requirements.

I suggest to read this tutorial from the official documentation.

https://hyperledger.github.io/composer/latest/integrating/customizing-the-rest-server



回答2:

I wanted to do that also initially, but then I just edit the server.js file on the composer rest server for basic auth at the end.

  1. check where your composer-rest-server is stored

npm root -g

  1. Open where server.js is located
cd /home/ubuntu/.nvm/versions/node/v8.10.0/lib/node_modules/composer-rest-server/server
  1. Add this to the code
const basicauth = require('basicauth-middleware');
app.use(basicauth('username', 'password!');