<commit_before>// JavaScript source code
'use strict';

config.callback = loggedin;
var authenticationContext = new AuthenticationContext(config);


function displayTodoList() {
    authenticationContext.login();
}

function signOut() {
    authenticationContext.logOut();
}

function loggedin(errorDescription, idToken, error) {
    if (error) {
        showError(errorDescription, error);
    }
    else {
        authenticationContext.getUser(onLogin);
    }
}

function onLogin(error, user) {
    if (user) {
        // Show the information about the user
        var userInfoElement = document.getElementById("userInfo");
        userInfoElement.parentElement.classList.remove("hidden");
        userInfoElement.innerHTML = JSON.stringify(user, null, 4);

        // Show the Sign-Out button
        document.getElementById("signOutButton").classList.remove("hidden");

        // Call the protected API to show the content of the todo list
        acquireTokenAndCallService();
    }
    if (error) {
        showError("login", error);
    }
}


function acquireTokenAndCallService() {
    authenticationContext.acquireToken(webApiConfig.resourceId, function (errorDesc, token, error)
    {
        if (error)
        {
            authenticationContext.acquireTokenPopup(webApiConfig.resourceId, null, null, onAccessToken);
        }
        else
        {
            onAccessToken(errorDesc, token, error);
        }
    })
}

function onAccessToken(errorDesc, token, error) {
    if (error) {
        showError("acquireToken", error);
    }
    if (token) {
        callServiceWithToken(token, webApiConfig.resourceBaseAddress+"api/todolist");
    }
}


function callServiceWithToken(token, endpoint) {
    var headers = new Headers();
    var bearer = "Bearer " + token;
    headers.append("Authorization", bearer);
    var options = {
        method: "GET",
        headers: headers
    };

    // Note that fetch API is not available in all browsers
    fetch(endpoint, options)
        .then(function (response) {
            var contentType = response.headers.get("content-type");
            if (response.status === 200 && contentType && contentType.indexOf("application/json") !== -1) {
                response.json()
                    .then(function (data) {
                        // Display response in the page
                        showAPIResponse(data, token);
                    })
                    .catch(function (error) {
                        showError(endpoint, error);
                    });
            } else {
                response.json()
                    .then(function (data) {
                        // Display response in the page
                        showError(endpoint, data);
                    })
                    .catch(function (error) {
                        showError(endpoint, error);
                    });
            }
        })
        .catch(function (error) {
            showError(endpoint, error);
        });
}


function showAPIResponse(data, token) {
    var responseElement = document.getElementById("apiResponse");
    responseElement.parentElement.classList.remove("hidden");
    console.log(data);
    responseElement.innerHTML = JSON.stringify(data, null, 4);
}<commit_msg>Adding code to demo both the popup experience and the redirect experience. this is only directed by the popUp boolean in the appconfig.js file.<commit_after>'use strict';

config.callback = loggedin;
var authenticationContext = new AuthenticationContext(config);

if (authenticationContext.isCallback(window.location.hash)) {
    authenticationContext.handleWindowCallback();
}
else {
    var user = authenticationContext.getCachedUser();
    if (user && window.parent === window && !window.opener) {

        // Show the user info.
        var userInfoElement = document.getElementById("userInfo");
        userInfoElement.parentElement.classList.remove("hidden");
        userInfoElement.innerHTML = JSON.stringify(user, null, 4);

        // Show the Sign-Out button
        document.getElementById("signOutButton").classList.remove("hidden");

        // Call the protected API to show the content of the todo list
        acquireTokenAndCallService();
    }
}

function displayTodoList() {
    authenticationContext.login();
}

function signOut() {
    authenticationContext.logOut();
}

function loggedin(errorDescription, idToken, error) {
    if (error) {
        showError(errorDescription, error);
    }
    else {
        if (config.popUp) {
            authenticationContext.getUser(onLogin);
        }
    }
}

/**
 * Function called when the user is logged-in
 * @param {string} error - Error from the STS
 * @param {any} user - Data about the logged-in user
 */
function onLogin(error, user) {
    if (user) {
        // Show the information about the user
        var userInfoElement = document.getElementById("userInfo");
        userInfoElement.parentElement.classList.remove("hidden");
        userInfoElement.innerHTML = JSON.stringify(user, null, 4);

        // Show the Sign-Out button
        document.getElementById("signOutButton").classList.remove("hidden");

        // Call the protected API to show the content of the todo list
        acquireTokenAndCallService();
    }
    if (error) {
        showError("login", error);
    }
}

/**
 * Acquire an access token and call the Web API
 */
function acquireTokenAndCallService() {
    authenticationContext.acquireToken(webApiConfig.resourceId, function (errorDesc, token, error) {
        if (error) {
            if (config.popUp) {
                authenticationContext.acquireTokenPopup(webApiConfig.resourceId, null, null, onAccessToken);
            }
            else {
                authenticationContext.acquireTokenRedirect(webApiConfig.resourceId, null, null);
            }
        }
        else {
            onAccessToken(errorDesc, token, error);
        }
    })
}

/**
 * Function called when the access token is available
 * @param {string} errorDesc - Error message
 * @param {string} token - Access token to use in the Web API
 * @param {string} error - Error code from the STS
 */
function onAccessToken(errorDesc, token, error) {
    if (error) {
        showError("acquireToken", error);
    }
    if (token) {
        callServiceWithToken(token, webApiConfig.resourceBaseAddress + "api/todolist");
    }
}


/**
 * Show an error message in the page
 * @param {string} token - Access token for the web API
 * @param {string} endpoint - endpoint to the Web API to call
 */
function callServiceWithToken(token, endpoint) {
    // Header won't work in IE, but you could replace it with a call to AJAX JQuery for instance.
    var headers = new Headers();
    var bearer = "Bearer " + token;
    headers.append("Authorization", bearer);
    var options = {
        method: "GET",
        headers: headers
    };

    // Note that fetch API is not available in all browsers
    fetch(endpoint, options)
        .then(function (response) {
            var contentType = response.headers.get("content-type");
            if (response.status === 200 && contentType && contentType.indexOf("application/json") !== -1) {
                response.json()
                    .then(function (data) {
                        // Display response in the page
                        showAPIResponse(data, token);
                    })
                    .catch(function (error) {
                        showError(endpoint, error);
                    });
            } else {
                response.json()
                    .then(function (data) {
                        // Display response in the page
                        showError(endpoint, data);
                    })
                    .catch(function (error) {
                        showError(endpoint, error);
                    });
            }
        })
        .catch(function (error) {
            showError(endpoint, error);
        });
}

/**
 * Display the response from the Web API (as JSON)
 * @param {any} data - the JSon data
 * @param {any} token - the access token
 */
function showAPIResponse(data, token) {
    var responseElement = document.getElementById("apiResponse");
    responseElement.parentElement.classList.remove("hidden");
    console.log(data);
    responseElement.innerHTML = JSON.stringify(data, null, 4);
}
{'fraction_non_alphanumeric': 0.10638297872340426, 'fraction_numerical': 0.0015539086779823093, 'mean_word_length': 2.210667689946278, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '304897', 'n_tokens_mistral': 2155, 'n_tokens_neox': 2114, 'n_words': 568}