-1

I've found alot of text explaining this but i haven't quite found what i'm looking for. I'm hoping someone can point me in the right direction. I am building an Angular App that communicates with a RoR back-end. On the back-end there is a basic authentication with username and password. For the moment i've hardcoded the username and password into my app.js. I'm using Restangular so i set it up like so:

 var authenticationToken = btoa('username,password');

.config(function (RestangularProvider) {
  RestangularProvider.setDefaultHeaders({Authorization: 'Basic ' + authenticationToken});
}

But what I would like is for users to have to login when they open the app with username and password. So that my app can use those to send requests to the backend. I have no real idea how to set this up properly.

update I figured out my problem. I'm using default headers, while I should be using individual headers. Which i can just set in my service call.

Mischa
  • 2,069
  • 4
  • 23
  • 32

1 Answers1

1

All you need to do is create a text input in Angular. Follow the example in the Angular documentation. Then in your controller/directive (whatever scope you store the username and password) just make the authentication call like you did in your question.

Mike S
  • 11,329
  • 6
  • 41
  • 76
  • I'm not quite sure i understand. I make a basic login page with text input. Store the inputted username and password into 2 scope variables and then i make a call to the RestangularProvider. I've tried this but the problem is I can't inject the provider into a controller. So i cant set the default headers to a new value. – Mischa Jun 19 '15 at 14:51
  • @Mischa Ah you should have said that in your question. So actually your question is: "How do I access values in a controller from app.js?" Have you seen this? http://stackoverflow.com/questions/28847987/get-controller-value-in-app-js-from-controller-js – Mike S Jun 19 '15 at 15:19
  • i've looked at it, and this is not exactly what i'm looking for. I can't inject $scope or $rootscope into app.js so i can't change the restangulaprovider config. i'll update my question a little. – Mischa Jun 22 '15 at 09:15