<commit_before>const Cookies            = require('js-cookie');
const moment             = require('moment');
const Client             = require('./client');
const GTM                = require('./gtm');
const getLanguage        = require('./language').get;
const urlLang            = require('./language').urlLang;
const isStorageSupported = require('./storage').isStorageSupported;
const urlFor             = require('./url').urlFor;
const paramsHash         = require('./url').paramsHash;
const getPropertyValue   = require('./utility').getPropertyValue;
const BinarySocket       = require('../websocket_pages/socket');

const LoggedInHandler = (() => {
    const onLoad = () => {
        parent.window.is_logging_in = 1; // this flag is used in base.js to prevent auto-reloading this page
        let redirect_url;
        const params = paramsHash(window.location.href);
        BinarySocket.send({ authorize: params.token1 }).then((response) => {
            const account_list = getPropertyValue(response, ['authorize', 'account_list']);
            if (isStorageSupported(localStorage) && isStorageSupported(sessionStorage) && account_list) {
                storeClientAccounts(account_list);
                // redirect url
                redirect_url = sessionStorage.getItem('redirect_url');
                sessionStorage.removeItem('redirect_url');
            } else {
                Client.doLogout({ logout: 1 });
            }

            // redirect back
            let set_default = true;
            if (redirect_url) {
                const do_not_redirect = ['reset_passwordws', 'lost_passwordws', '<PASSWORD>_passwordws', 'home', 'home-jp', '404'];
                const reg             = new RegExp(do_not_redirect.join('|'), 'i');
                if (!reg.test(redirect_url) && urlFor('') !== redirect_url) {
                    set_default = false;
                }
            }
            if (set_default) {
                const lang_cookie = urlLang(redirect_url) || Cookies.get('language');
                const language    = getLanguage();
                redirect_url      = Client.defaultRedirectUrl();
                if (lang_cookie && lang_cookie !== language) {
                    redirect_url = redirect_url.replace(new RegExp(`/${language}/`, 'i'), `/${lang_cookie.toLowerCase()}/`);
                }
            }
            document.getElementById('loading_link').setAttribute('href', redirect_url);
            window.location.href = redirect_url; // need to redirect not using pjax
        });
    };

    const storeClientAccounts = (account_list) => {
        // Parse url for loginids, tokens, and currencies returned by OAuth
        const params = paramsHash(window.location.href);

        // Clear all accounts before entering the loop
        Client.clearAllAccounts();

        let is_loginid_set = false;
        account_list.forEach((account) => {
            Object.keys(account).forEach((param) => {
                if (param === 'loginid') {
                    // set the first non-ico account as default loginid
                    if (!is_loginid_set && !account.is_ico_only) {
                        Client.set(param, account[param]);
                        is_loginid_set = true;
                    }
                } else {
                    const param_to_set = param === 'country' ? 'residence' : param;
                    const value_to_set = typeof account[param] === 'undefined' ? '' : account[param];
                    Client.set(param_to_set, value_to_set, account.loginid);
                }
            });
        });

        let i = 1;
        while (params[`acct${i}`]) {
            const loginid = params[`acct${i}`];
            const token   = params[`token${i}`];
            if (loginid && token) {
                Client.set('token', token, loginid);
            }
            i++;<commit_msg>update how default loginid is chosen<commit_after>const Cookies            = require('js-cookie');
const moment             = require('moment');
const Client             = require('./client');
const GTM                = require('./gtm');
const getLanguage        = require('./language').get;
const urlLang            = require('./language').urlLang;
const isStorageSupported = require('./storage').isStorageSupported;
const urlFor             = require('./url').urlFor;
const paramsHash         = require('./url').paramsHash;
const getPropertyValue   = require('./utility').getPropertyValue;
const BinarySocket       = require('../websocket_pages/socket');

const LoggedInHandler = (() => {
    const onLoad = () => {
        parent.window.is_logging_in = 1; // this flag is used in base.js to prevent auto-reloading this page
        let redirect_url;
        const params = paramsHash(window.location.href);
        BinarySocket.send({ authorize: params.token1 }).then((response) => {
            const account_list = getPropertyValue(response, ['authorize', 'account_list']);
            if (isStorageSupported(localStorage) && isStorageSupported(sessionStorage) && account_list) {
                storeClientAccounts(account_list);
                // redirect url
                redirect_url = sessionStorage.getItem('redirect_url');
                sessionStorage.removeItem('redirect_url');
            } else {
                Client.doLogout({ logout: 1 });
            }

            // redirect back
            let set_default = true;
            if (redirect_url) {
                const do_not_redirect = ['<PASSWORD>', '<PASSWORD>', '<PASSWORD>', 'home', 'home-jp', '404'];
                const reg             = new RegExp(do_not_redirect.join('|'), 'i');
                if (!reg.test(redirect_url) && urlFor('') !== redirect_url) {
                    set_default = false;
                }
            }
            if (set_default) {
                const lang_cookie = urlLang(redirect_url) || Cookies.get('language');
                const language    = getLanguage();
                redirect_url      = Client.defaultRedirectUrl();
                if (lang_cookie && lang_cookie !== language) {
                    redirect_url = redirect_url.replace(new RegExp(`/${language}/`, 'i'), `/${lang_cookie.toLowerCase()}/`);
                }
            }
            document.getElementById('loading_link').setAttribute('href', redirect_url);
            window.location.href = redirect_url; // need to redirect not using pjax
        });
    };

    const storeClientAccounts = (account_list) => {
        // Parse url for loginids, tokens, and currencies returned by OAuth
        const params = paramsHash(window.location.href);

        // Clear all accounts before entering the loop
        Client.clearAllAccounts();

        let is_loginid_set = false;
        account_list.forEach((account) => {
            Object.keys(account).forEach((param) => {
                if (param === 'loginid') {
                    // set the first non-ico account as default loginid
                    if (!is_loginid_set && !account.is_ico_only && !account.is_disabled && !account.is_virtual) {
                        Client.set(param, account[param]);
                        is_loginid_set = true;
                    }
                } else {
                    const param_to_set = param === 'country' ? 'residence' : param;
                    const value_to_set = typeof account[param] === 'undefined' ? '' : account[param];
                    Client.set(param_to_set, value_to_set, account.loginid);
                }
            });
        });

        // if didn't find any login ID that matched the above condition, set the first one at the end of the loop
        if (!is_loginid_set) {
            Client.set('loginid', account_list[0].loginid);
            is_loginid_set = true;
        }

        let i = 1;
        while (params[`acct${i}`]) {
            const loginid = params[`acct${i}`];
            const token   = params[`token${i}`];
            if (loginid && token) {
                Client.set('token', token, loginid);
            }
            i++;
{'fraction_non_alphanumeric': 0.12668396984303548, 'fraction_numerical': 0.0018539117538005192, 'mean_word_length': 1.8634111818825194, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '918274', 'n_tokens_mistral': 2101, 'n_tokens_neox': 2026, 'n_words': 463}