forked from binary-com/binary-static
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogged_in.js
More file actions
92 lines (82 loc) · 3.78 KB
/
logged_in.js
File metadata and controls
92 lines (82 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const objectNotEmpty = require('./utility').objectNotEmpty;
const Cookies = require('../../lib/js-cookie');
const getLanguage = require('./language').getLanguage;
const GTM = require('./gtm').GTM;
const Client = require('./client').Client;
const url_for = require('./url').url_for;
const default_redirect_url = require('./url').default_redirect_url;
const LoggedInHandler = (function() {
'use strict';
const init = function() {
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(function(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.set_cookie('loginid', loginid);
Client.set_cookie('loginid_list', loginid_list);
}
Client.set_cookie('login', tokens[loginid]);
// set flags
if (!$('body').hasClass('BlueTopBack')) localStorage.setItem('risk_classification', 'check');
Client.set_check_tnc();
GTM.set_login_flag();
// 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'];
const reg = new RegExp(do_not_redirect.join('|'), 'i');
if (!reg.test(redirect_url) && url_for('') !== redirect_url) {
set_default = false;
}
}
if (set_default) {
redirect_url = default_redirect_url();
const lang_cookie = Cookies.get('language'),
language = getLanguage();
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;
};
const storeTokens = function() {
// Parse hash for loginids and tokens returned by OAuth
const hash = (/acct1/i.test(window.location.hash) ? window.location.hash : window.location.search).substr(1).split('&'); // to maintain compatibility till backend change released
const tokens = {};
for (let i = 0; i < hash.length; i += 2) {
const loginid = getHashValue(hash[i], 'acct'),
token = getHashValue(hash[i + 1], 'token');
if (loginid && token) {
tokens[loginid] = token;
}
}
if (objectNotEmpty(tokens)) {
Client.set_value('tokens', JSON.stringify(tokens));
}
return tokens;
};
const getHashValue = function(source, key) {
const match = new RegExp('^' + key);
return source && source.length > 0 ? (match.test(source.split('=')[0]) ? source.split('=')[1] : '') : '';
};
return {
init: init,
};
})();
module.exports = {
LoggedInHandler: LoggedInHandler,
};