authentication handling in javascript

Solutions on MaxInterview for authentication handling in javascript by the best coders in the world

showing results for - "authentication handling in javascript"
Franco
07 Jun 2020
1var token_ // variable will store the token
2var userName = "clientID"; // app clientID
3var passWord = "secretKey"; // app clientSecret
4var caspioTokenUrl = "https://xxx123.caspio.com/oauth/token"; // Your application token endpoint  
5var request = new XMLHttpRequest(); 
6
7function getToken(url, clientID, clientSecret) {
8    var key;           
9    request.open("POST", url, true); 
10    request.setRequestHeader("Content-type", "application/json");
11    request.send("grant_type=client_credentials&client_id="+clientID+"&"+"client_secret="+clientSecret); // specify the credentials to receive the token on request
12    request.onreadystatechange = function () {
13        if (request.readyState == request.DONE) {
14            var response = request.responseText;
15            var obj = JSON.parse(response); 
16            key = obj.access_token; //store the value of the accesstoken
17            token_ = key; // store token in your global variable "token_" or you could simply return the value of the access token from the function
18        }
19    }
20}
21// Get the token
22getToken(caspioTokenUrl, userName, passWord);