diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 00000000000..fa1188cb259 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,17 @@ +{ + "singleQuote": true, + "tabWidth": 4, + "semi": true, + "trailingComma": "es5", + "bracketSpacing": true, + "jsxBracketSameLine": false, + "arrowParens": "avoid", + "printWidth": 100, + "endOfLine": "lf", + "object-property-newline": "always", + "key-spacing": { + "beforeColon": false, + "afterColon": true, + "mode": "strict" + } +} \ No newline at end of file diff --git a/src/javascript/app/pages/callback/callback.jsx b/src/javascript/app/pages/callback/callback.jsx index 514f1f0f1e4..7c47b26641e 100644 --- a/src/javascript/app/pages/callback/callback.jsx +++ b/src/javascript/app/pages/callback/callback.jsx @@ -1,156 +1,137 @@ -/* eslint-disable */ -import React from "react"; -import ReactDOM from "react-dom"; +import React from 'react'; +import ReactDOM from 'react-dom'; -import { Callback } from "@deriv-com/auth-client"; +import { Callback } from '@deriv-com/auth-client'; -import Cookies from "js-cookie"; -import moment from "moment"; -import Client from "../../base/client"; -import BinarySocket from "../../base/socket"; -import GTM from "../../../_common/base/gtm"; -import { get as getLanguage } from "../../../_common/language"; -import { urlLang } from "../../../_common/language"; -import { isStorageSupported } from "../../../_common/storage"; -import { removeCookies } from "../../../_common/storage"; -import { urlFor } from "../../../_common/url"; -import { getPropertyValue } from "../../../_common/utility"; -import { getElementById } from "../../../_common/common_functions"; +import Cookies from 'js-cookie'; +import moment from 'moment'; +import Client from '../../base/client'; +import BinarySocket from '../../base/socket'; +import GTM from '../../../_common/base/gtm'; +import { get as getLanguage, urlLang } from '../../../_common/language'; +import { isStorageSupported, removeCookies } from '../../../_common/storage'; +import { urlFor } from '../../../_common/url'; +import { getPropertyValue } from '../../../_common/utility'; +import { getElementById } from '../../../_common/common_functions'; const map_names = { - country: "residence", - landing_company_name: "landing_company_shortcode", + country : 'residence', + landing_company_name: 'landing_company_shortcode', }; const storeClientAccounts = (tokens, account_list) => { - // Parse url for loginids, tokens, and currencies returned by OAuth + // Parse url for loginids, tokens, and currencies returned by OAuth - // Clear all accounts before entering the loop - Client.clearAllAccounts(); + // Clear all accounts before entering the loop + Client.clearAllAccounts(); - account_list.forEach((account) => { - Object.keys(account).forEach((param) => { - if (param === "loginid") { - if ( - !Client.get("loginid") && - !account.is_disabled && - !account.is_virtual - ) { - Client.set(param, account[param]); - } - } else { - const param_to_set = map_names[param] || param; - const value_to_set = - typeof account[param] === "undefined" ? "" : account[param]; - Client.set(param_to_set, value_to_set, account.loginid); - } + account_list.forEach(account => { + Object.keys(account).forEach(param => { + if (param === 'loginid') { + if (!Client.get('loginid') && !account.is_disabled && !account.is_virtual) { + Client.set(param, account[param]); + } + } else { + const param_to_set = map_names[param] || 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 (tokens[`acct${i}`]) { - const loginid = tokens[`acct${i}`]; - const token = tokens[`token${i}`]; - if (loginid && token) { - Client.set("token", token, loginid); + let i = 1; + while (tokens[`acct${i}`]) { + const loginid = tokens[`acct${i}`]; + const token = tokens[`token${i}`]; + if (loginid && token) { + Client.set('token', token, loginid); + } + i++; } - i++; - } - // if didn't find any login ID that matched the above condition - // or the selected one doesn't have a token, set the first one - if (!Client.get("loginid") || !Client.get("token")) { - Client.set("loginid", tokens.acct1 || account_list[0].loginid); - } + // if didn't find any login ID that matched the above condition + // or the selected one doesn't have a token, set the first one + if (!Client.get('loginid') || !Client.get('token')) { + Client.set('loginid', tokens.acct1 || account_list[0].loginid); + } - if (Client.isLoggedIn()) { - GTM.setLoginFlag("log_in"); - Client.set("session_start", parseInt(moment().valueOf() / 1000)); - // Remove cookies that were set by the old code - removeCookies("email", "login", "loginid", "loginid_list", "residence"); - } + if (Client.isLoggedIn()) { + GTM.setLoginFlag('log_in'); + Client.set('session_start', parseInt(moment().valueOf() / 1000)); + // Remove cookies that were set by the old code + removeCookies('email', 'login', 'loginid', 'loginid_list', 'residence'); + } }; const CallbackContainer = () => { - const onLoginSuccess = async (tokens) => { - let redirect_url; - BinarySocket.send({ authorize: tokens.token1 }).then((response) => { - const account_list = getPropertyValue(response, [ - "authorize", - "account_list", - ]); - if ( - isStorageSupported(localStorage) && - isStorageSupported(sessionStorage) && - account_list - ) { - // redirect url - redirect_url = sessionStorage.getItem("redirect_url"); - sessionStorage.removeItem("redirect_url"); + const onLoginSuccess = async tokens => { + let redirect_url; + BinarySocket.send({ authorize: tokens.token1 }).then(response => { + const account_list = getPropertyValue(response, ['authorize', 'account_list']); + if ( + isStorageSupported(localStorage) && + isStorageSupported(sessionStorage) && + account_list + ) { + // redirect url + redirect_url = sessionStorage.getItem('redirect_url'); + sessionStorage.removeItem('redirect_url'); - storeClientAccounts(tokens, account_list); - } else { - Client.doLogout({ logout: 1 }); - } + storeClientAccounts(tokens, account_list); + } else { + Client.doLogout({ logout: 1 }); + } - // redirect back - let set_default = true; - if (redirect_url) { - const do_not_redirect = [ - "reset_passwordws", - "lost_passwordws", - "change_passwordws", - "home", - "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.isAccountOfType("financial") || Client.isOptionsBlocked() - ? urlFor("user/metatrader") - : Client.defaultRedirectUrl(); - if (lang_cookie && lang_cookie !== language) { - redirect_url = redirect_url.replace( - new RegExp(`/${language}/`, "i"), - `/${lang_cookie.toLowerCase()}/` - ); - } - } - getElementById("loading_link").setAttribute("href", redirect_url); - console.log("redirect url", redirect_url); - const domains = ["deriv.com", "binary.sx", "pages.dev", "localhost"]; - const currentDomain = window.location.hostname - .split(".") - .slice(-2) - .join("."); - if (domains.includes(currentDomain)) { - Cookies.set("logged_state", "false", { - expires: 30, - path: "/", - domain: currentDomain, - secure: true, + // redirect back + let set_default = true; + if (redirect_url) { + const do_not_redirect = [ + 'reset_passwordws', + 'lost_passwordws', + 'change_passwordws', + 'home', + '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.isAccountOfType('financial') || Client.isOptionsBlocked() + ? urlFor('user/metatrader') + : Client.defaultRedirectUrl(); + if (lang_cookie && lang_cookie !== language) { + redirect_url = redirect_url.replace( + new RegExp(`/${language}/`, 'i'), + `/${lang_cookie.toLowerCase()}/` + ); + } + } + getElementById('loading_link').setAttribute('href', redirect_url); + const domains = ['deriv.com', 'binary.sx', 'pages.dev', 'localhost']; + const currentDomain = window.location.hostname.split('.').slice(-2).join('.'); + if (domains.includes(currentDomain)) { + Cookies.set('logged_state', 'false', { + expires: 30, + path : '/', + domain : currentDomain, + secure : true, + }); + } + window.location.href = redirect_url; // need to redirect not using pjax }); - } - window.location.href = redirect_url; // need to redirect not using pjax - }); - }; + }; - return ( - - ); + // eslint-disable-next-line no-console + return ; }; export const init = () => { - ReactDOM.render( - , - document.getElementById("callback_container") - ); + ReactDOM.render(, document.getElementById('callback_container')); }; export default init;