Re: [loopbackjs] Loopback Api + Proxy 401 Unauthorized on login

320 views
Skip to first unread message

Raymond Feng

unread,
Jul 16, 2015, 11:05:07 AM7/16/15
to loopb...@googlegroups.com
Did you set up bodyParser.json() middleware and make sure the login request has ‘Content-Type: application/json’ header? You should print out req.body to see if it contains the correct credentials.

Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

On Jul 16, 2015, at 5:48 AM, Loïc LE PENN <loic....@gmail.com> wrote:


Hi,

I have an api loopback which work well with Strongloop Api Explorer.

I try to make it work with a node proxy using module request. But when i try to login by the proxy i get the following error :

{
  "error": {
    "name": "Error",
    "status": 401,
    "message": "Authorization Required",
    "statusCode": 401,
    "code": "AUTHORIZATION_REQUIRED",
    "stack": "Error: Authorization Required\n    at c:\\Users\\WFSZ8297\\Desktop\\VlanManager\\TEASeT-server\\node_modules\\loopback\\lib\\application.js:348:21\n    at c:\\Users\\WFSZ8297\\Desktop\\VlanManager\\TEASeT-server\\node_modules\\loopback\\lib\\model.js:313:7\n    at c:\\Users\\WFSZ8297\\Desktop\\VlanManager\\TEASeT-server\\node_modules\\loopback\\common\\models\\acl.js:465:23\n    at c:\\Users\\WFSZ8297\\Desktop\\VlanManager\\TEASeT-server\\node_modules\\loopback\\node_modules\\async\\lib\\async.js:251:17\n    at done (c:\\Users\\WFSZ8297\\Desktop\\VlanManager\\TEASeT-server\\node_modules\\loopback\\node_modules\\async\\lib\\async.js:132:19)\n    at c:\\Users\\WFSZ8297\\Desktop\\VlanManager\\TEASeT-server\\node_modules\\loopback\\node_modules\\async\\lib\\async.js:32:16\n    at c:\\Users\\WFSZ8297\\Desktop\\VlanManager\\TEASeT-server\\node_modules\\loopback\\node_modules\\async\\lib\\async.js:248:21\n    at c:\\Users\\WFSZ8297\\Desktop\\VlanManager\\TEASeT-server\\node_modules\\loopback\\node_modules\\async\\lib\\async.js:572:34\n    at c:\\Users\\WFSZ8297\\Desktop\\VlanManager\\TEASeT-server\\node_modules\\loopback\\common\\models\\acl.js:447:17\n    at c:\\Users\\WFSZ8297\\Desktop\\VlanManager\\TEASeT-server\\node_modules\\loopback\\common\\models\\role.js:215:21"
  }
}

I use a model which override User model and use exactly the same acl as the build in model on login. 

proxy code  :

// Dépendances
var http = require('http'),
    express = require('express'),
    app = express(),
    request = require('request');

    var host = 'localhost';    
    var port = 3001;

//app.use(express.cookieParser());

app.post('/api/users/login', function (req, res) {
    console.log(req.headers);
    request({
        //headers:req.header
        body: req.body
    }, function(error, response, body){
        if (!error && response.statusCode == 200) {
            res.status(200).send();
        }
        else if(error){
            console.log(error);
            res.status(503).send();
        }
        else{
            console.log(response.body)
            res.status(response.statusCode).send(response.body);
        }
    });
});

/*app.post('/logout', function (req, res, next) {
});

app.all('/api/*', function (req, res, next) {
    
});*/

    
var server = app.listen(port,host, function () {
  console.log('Example app listening at http://%s:%s', host, port);
});

Savez vous comment résoudre ce problème ?

Cordialement,

Loïc Le PENN


--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/loopbackjs/922839f1-0812-4d91-8f4a-34b4e210ccac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Loïc LE PENN

unread,
Jul 17, 2015, 3:38:45 AM7/17/15
to loopb...@googlegroups.com
Hi,

I have solved this issue. i haven't set up bodyParser but i also forgot to specify method in module request so he just can't login because request work with get by default i think.

Thank you for this quick anwser.
Reply all
Reply to author
Forward
0 new messages