2

I am trying to implement a custom API authentication method as suggested below.

Password protecting a REST service?

However, I am having some trouble with the authkey. I would like to save my authkey received as a global variable but I can't seem to change it, it always become the same value as what it is defined to be at the start.

I am implement global variable using angular (v0.9) service.

snippet of my code

angular.service('Authkey', function(){
  return {
"authkey": "0000"
 };
});

controller

function LoginCtrl(Login_, Authkey_){
this.login = function(){
Login_.query({"Username": this.email,"Password": this.password}, function(response){
        if (response.success === "true") {
            Authkey_.authkey = response.AuthKey;
            console.log(Authkey_.authkey);
            window.location="/main.html";

        } 
    }); 
}
}

Yup. It always become 0000 after the page is changed.

Appreciate all help I can get. Thank you..

Community
  • 1
  • 1
Wilson
  • 231
  • 1
  • 6
  • 13

2 Answers2

0

try this:

angular.service('Authkey', function(){
  var authkey = "000";
  return {
     getAuthKey: function(){
        return authkey;
     }, 
     setAuthKey: function(key){
        authkey = key;
     }
 };
});

//in controller:

Authkey_.setAuthKey(response.AuthKey);
consol.log(Authkey_.getAuthKey());

the difference here is that the AuthKey service has a closure on the key and you get/set that value.

in your code you always return the same thing. your setting of the key in the service has no effect on your return value

mkoryak
  • 57,086
  • 61
  • 201
  • 257
  • Thanks for your reply. I have tried it. the same problem remains. if i call console.log at the login page, the value changes to the response.authkey but when i change the window location to main.html, the value reverts back to the original value. weird.. – Wilson Oct 22 '12 at 19:04
  • oh, i didnt notice you changed the window location like that. you should use the $location service to change the app location to a route. when you change a location like that you lose all your javascript state. at this point you should be storing the authkey in a cookie. – mkoryak Oct 22 '12 at 19:07
  • oh. no wonder. i can't get $location.path to work. thought I could work around with window.location. Do you have any sample codes on the "use the $location service to change the app location to a route" sorry. real beginner in Angularjs. Thank you. – Wilson Oct 22 '12 at 19:27
  • hmm. i have set up a list of routes for different pages using ng:views with a main.html (headers, menu bar etc) Login.html is just a plain and simple two-textbox input page. How can I set up to use location? – Wilson Oct 22 '12 at 19:38
  • i am a pretty big newb too :). you should read this: http://docs.angularjs.org/tutorial/step_07 and also i can tell you that you should set up some element to be ng-view and change its contents with partials. it your case ng-view should probably go on the body element so you can change out the entire body contents with main.html and login.html – mkoryak Oct 22 '12 at 19:54
  • I did it by the cookies way! so i dont need to change so much for my code! thank you! – Wilson Oct 23 '12 at 04:59
0

I'm not sure if this was available in Angular 0.9... but you might try using Module.value(key, value).... it allows you to set up a globally inject-able value.

var app = angular.module('myApp', []);

app.value('token', '12345');

app.controller('MyCtrl', function($scope, token) {
     $scope.tokenOut = token; //still 12345.
});
Ben Lesh
  • 107,825
  • 47
  • 247
  • 232