1

I have some simple javascript functions to interact with an API like this one to login:

    login: function(username, password) {
    var calledUrl = baseapi + "user/login/" + credentials;
    calledUrl.post(
        function (content) {
            /*console.log("success" + JSON.stringify(content, null, 4));*/
        },
        function (e) {
            console.log("it failed! -> " + e);
        },
        {
            "username": username,
            "password": password

        },
        {"Accept" : "application/json"}
    );
},

The problem is, in the URL I must pass some credentials and they look like that:

var credentials = "?api_username=" + api_username + "&api_key=" + api_key;

Right now this variable is hardcoded to make some tests but of course it should change for each person using the function. I don't want to ask for it with each request, in this case I only want to ask for username and password. I would like to ask for it once and for all during an initializing process or whatever it is called and then remember it when executing the various functions.

2
  • 1
    I don't understand, what exactly is the problem with your code? Commented Oct 20, 2013 at 22:18
  • Sorry if it's not very clear, the code works but there is a parameter that is hardcoded in there, the credentials of the API. I have more of these functions and each of them use this parameter. I would like to be able to make a call that remembers this parameter in further calls so I do not have to enter it all the time. Commented Oct 21, 2013 at 6:44

2 Answers 2

1

If .login() is the first method that typically needs the credentials, then you can just make it a required argument for that method and then store the credentials in the object:

login: function(username, password, credentials) {
    // save credentials for use in other methods    
    this.credentials = credentials;
    var calledUrl = baseapi + "user/login/" + credentials;
    calledUrl.post(
        function (content) {
            /*console.log("success" + JSON.stringify(content, null, 4));*/
        },
        function (e) {
            console.log("it failed! -> " + e);
        },
        {
            "username": username,
            "password": password

        },
        {"Accept" : "application/json"}
    );
},

Then, in other methods, you can access the credentials for this user with this.credentials.

If there are other methods that could also be called first and need the credentials for them, then you can either make the credentials an argument for those also or you can create a .init() method that just establishes the credentials or you can make it an argument in the constructor for this object.


You will probably also have to fix this line:

 calledUrl.post(...)

because calledUrl is a string and strings don't have a .post() method unless you're using some sort of 3rd party library that adds one.

Sign up to request clarification or add additional context in comments.

5 Comments

calledUrl.post( ? $ ?
@jacouh - I don't know about that - that's just the OP's code and I don't think an operative part of the question though it does look odd and probably wrong.
Yes the calledUrl.post is indeed odd, it comes from a 3rd party library called abaaso.
good ideas, the .init() looks cleaner as the users are not imposed what functions they should use first.
I have created an 'authenticate' function that asks for credentials and then use them in the other functions. What I needed was the this that I did not understand. Your answer showed me the way.
1

I recommend that you read about scope in JavaScript. Without more explanation of what you are trying to do, I would try something like this pattern...

var app = {
  baseapi: 'http://some.url.com'

  /* assuming the api user/pass are different form the account trying to log in */
  ,api_username: ''
  ,api_key: ''

  ,username: ''
  ,userpass: ''

  ,get_creditialString: function() {
    return '?api_username=' + this.api_username + '&api_key=' + this.api_key;
  }
  ,init: function(){    
    // do something to prompt for username and password
    this.username = 'myUserName';
    this.userpass = 'supersecretpassword';

    this.login();
  }
  ,login: function() {
    var calledUrl = this.baseapi + "user/login/" + this.get_credentialString();
    calledUrl.post(
        function (content) {
            /*console.log("success" + JSON.stringify(content, null, 4));*/
        },
        function (e) {
            console.log("it failed! -> " + e);
        },
        {
            "username": this.username,
            "password": this.userpass
        },
        {"Accept" : "application/json"}
    );
  }
}
app.init();

1 Comment

Yes sorry my explanation is pretty poor indeed. Your idea comes very close to what I need, the only thing is I would like the user to call a specific function to set up the credentials and then remember them to use them further down the code in other functions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.