<commit_before>const Client             = require('./client');
const GTM                = require('./gtm');
const getLanguage        = require('./language').get;
const urlLang            = require('./language').urlLang;
const defaultRedirectUrl = require('./url').defaultRedirectUrl;
const urlFor             = require('./url').urlFor;
const paramsHash         = require('./url').paramsHash;
const isEmptyObject      = require('./utility').isEmptyObject;
const Cookies            = require('../../lib/js-cookie');

const LoggedInHandler = (() => {
    'use strict';

    const onLoad = () => {
        parent.window.is_logging_in = 1; // this flag is used in base.js to prevent auto-reloading this page
        let redirect_url;
        try {
            const tokens  = storeTokens();
            let loginid = Cookies.get('loginid');

            if (!loginid) { // redirected to another domain (e.g. github.io) so those cookie are not accessible here
                const loginids = Object.keys(tokens);
                let loginid_list = '';
                loginids.map((id) => {
                    loginid_list += `${(loginid_list ? '+' : '')}${id}:${(/^V/i.test(id) ? 'V' : 'R')}:E`; // since there is not any data source to check, so assume all are enabled, disabled accounts will be handled on authorize
                });
                loginid = loginids[0];
                // set cookies
                Client.setCookie('loginid',      loginid);
                Client.setCookie('loginid_list', loginid_list);
            }
            Client.setCookie('login', tokens[loginid].token);

            // set flags
            GTM.setLoginFlag();

            // redirect url
            redirect_url = sessionStorage.getItem('redirect_url');
            sessionStorage.removeItem('redirect_url');
        } catch (e) { console.log('storage is not supported'); }

        // redirect back
        let set_default = true;
        if (redirect_url) {
            const do_not_redirect = ['reset_passwordws', 'lost_passwordws', 'change_passwordws', 'home', 'home-jp'];
            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 = 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 storeTokens = () => {
        // Parse url for loginids, tokens, and currencies returned by OAuth<commit_msg>Handle no account returned by OAuth<commit_after>const Client             = require('./client');
const GTM                = require('./gtm');
const getLanguage        = require('./language').get;
const urlLang            = require('./language').urlLang;
const defaultRedirectUrl = require('./url').defaultRedirectUrl;
const urlFor             = require('./url').urlFor;
const paramsHash         = require('./url').paramsHash;
const isEmptyObject      = require('./utility').isEmptyObject;
const Cookies            = require('../../lib/js-cookie');

const LoggedInHandler = (() => {
    'use strict';

    const onLoad = () => {
        parent.window.is_logging_in = 1; // this flag is used in base.js to prevent auto-reloading this page
        let redirect_url;
        try {
            const tokens  = storeTokens();
            if (!isEmptyObject(tokens)) {
                let loginid = Cookies.get('loginid');
                // Need to set cookies if OAuth hasn't set them or redirected to another domain (e.g. github.io)
                if (!loginid) {
                    const loginids   = Object.keys(tokens);
                    let loginid_list = '';
                    loginids.map((id) => {
                        loginid_list += `${(loginid_list ? '+' : '')}${id}:${(/^V/i.test(id) ? 'V' : 'R')}:E`; // Assume all are enabled since there is no data source to check. Disabled accounts will be handled on authorize
                    });
                    loginid = loginids[0];
                    // set cookies
                    Client.setCookie('loginid',      loginid);
                    Client.setCookie('loginid_list', loginid_list);
                }
                Client.setCookie('login', tokens[loginid].token);

                // set flags
                GTM.setLoginFlag();
            }
            // redirect url
            redirect_url = sessionStorage.getItem('redirect_url');
            sessionStorage.removeItem('redirect_url');
        } catch (e) { console.log('storage is not supported'); }

        // redirect back
        let set_default = true;
        if (redirect_url) {
            const do_not_redirect = ['reset_passwordws', 'lost_passwordws', 'change_passwordws', 'home', 'home-jp'];
            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 = 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 storeTokens = () => {
        // Parse url for loginids, tokens, and currencies returned by OAuth
{'fraction_non_alphanumeric': 0.13545751633986927, 'fraction_numerical': 0.00065359477124183, 'mean_word_length': 1.9670382937469704, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '7544191', 'n_tokens_mistral': 1615, 'n_tokens_neox': 1571, 'n_words': 393}