I have the following code on my factory 

 $http({
    method:'POST',
    url: 'http://myapicall.com/api/login',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization' : 'Basic ' + $window.btoa('Username:MySecret')
    },
transformRequest: function(obj) {
var str = [];
for (var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data:{useraccount:uaccount,userpassword:pwd}
});

I'm using passportJS to check for the username and password to access the 
API route. On my app.js I have the following code.

var express = require('express');
var passport = require('passport');
var BasicStrategy = require('passport-http').BasicStrategy;
var path = require('path');
var mysql = require('mysql');
var bodyParser = require('body-parser');

....

var app = express();
app.use(passport.initialize());

passport.use(new BasicStrategy(function(username,password,done){
   connectionPool.getConnection(function(err,connection){
      if(err){
         console.log('Error: ' + err);
      }

      var sql = 'MY QUERY TO CHECK API ACCESS';

      connection.query(sql,function(err,rows){
         if(err){
            throw err;
         } else {
.....
            if(temp.length > 0){
               done(null,temp[0].apiId);
            } else {
               done(null,null);
            }
         }
         connection.release();
      });
   });
}));

var routes = require('./routes/index');
var routesApi = require('./api/routes.js');

app.use('/api',passport.authenticate('basic',{session:false}),routesApi);

....

When I try accessing the URL on the browser I get a login popup, and once I 
enter the correct credential it works. Also when I make the call using 
POSTMAN it works too.

My issue is when I make the $http call from my factory. For some reason I 
keep getting 401 unauthorized, even passing the username:password. Am I 
missing anything on the $http call?
Thanks for the help

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to