window.addEventListener("load", function() {
const url = 'https://app.distant-office.ru/api/chat_v3/auth_token.php'
fetch(url,
{credentials: 'include'}
).then(res => res.json())
.then(data => {
console.log(data);
if (data.login) {
var d = new Date();
d.setDate(d.getDate() + 1);
// _sc('android_id_install', data.android_id_install, d, "/");
// _sc('device_token', data.device.token, d, "/");
_sc('device_token', data.device.token, d, "/");
_sc('android_id_install', data.android_id_install, d, "/");
window.location.reload();
} else {
$("head").append($('\
\
\
\
'));
$("body").append($('\
\
'));
// $("#modal_auth_button").append($(''));
loadScriptWithCallback('https://app.distant-office.ru/js/popper.min.js?_='+Date.now(), function(){
loadScriptWithCallback('https://app.distant-office.ru/js/bootstrap431.min.js?_='+Date.now(), function(){
console.log("$('#modal_auth').length",$('#modal_auth').length);
$('#modal_auth').on('show.bs.modal', function (event) {
console.log('show.bs.modal');
});
show_modal_auth();
});
});
}
});
});
function loadScriptWithCallback(scriptName, callback) {
var body = document.getElementsByTagName("body")[0] || document.documentElement;
var script = document.createElement("script");
var done = false;
script.src = scriptName;
script.onload = script.onreadystatechange = function() {
if ( !done && (!this.readyState ||
this.readyState === "loaded" || this.readyState === "complete") ) {
done = true;
if (typeof callback == 'function')
{
callback.call();
}
console.log(scriptName + ' loaded');
// Handle memory leak in IE
script.onload = script.onreadystatechange = null;
if ( body && script.parentNode ) {
body.removeChild( script );
}
}
};
body.appendChild(script);
}
function show_modal_auth(button) {
// var button = $(event.relatedTarget) // Button that triggered the modal
var button = $(button) // Button that triggered the modal
console.log(button);
if (typeof firebase === 'undefined') {
loadScriptWithCallback('https://www.gstatic.com/firebasejs/7.21.0/firebase-app.js', function(){
loadScriptWithCallback('https://www.gstatic.com/firebasejs/7.21.0/firebase-auth.js', function(){
loadScriptWithCallback('https://www.gstatic.com/firebasejs/ui/4.6.1/firebase-ui-auth__ru.js', function(){
firebase_init();
});
});
});
}
$("#modal_auth").modal("show");
};
function firebase_init() {
var json = '{"src":["Google","Apple","Email"]}'; //,"Facebook"
var jsonParse = JSON.parse(json);
var signIn = [
];
// Initialize Firebase
var config = {
apiKey: "AIzaSyBSyJX36Hwkz7qd-lt5sTwO3ueg3yOcZVM",
authDomain: "distant-office.firebaseapp.com",
databaseURL: "https://distant-office.firebaseio.com",
projectId: "distant-office",
storageBucket: "distant-office.appspot.com",
messagingSenderId: "723349202322"
};
jsonParse.src.forEach(function (value) {
switch (value)
{
case "Google":
signIn.push(
{
provider: firebase.auth.GoogleAuthProvider.PROVIDER_ID,
scopes:
[
'https://www.googleapis.com/auth/userinfo.email',
'profile',
'email'
],
}
);
break;
case "Facebook":
signIn.push(
{
provider: firebase.auth.FacebookAuthProvider.PROVIDER_ID,
scopes: [
'public_profile',
'email',
],
},
);
break;
case "Twitter":
signIn.push(firebase.auth.TwitterAuthProvider.PROVIDER_ID);
break;
case "Github":
signIn.push(firebase.auth.GithubAuthProvider.PROVIDER_ID);
break;
case "Email":
signIn.push(firebase.auth.EmailAuthProvider.PROVIDER_ID);
break;
case "Apple":
signIn.push('apple.com');
break;
case "Phone":
signIn.push(
[
{
provider: firebase.auth.PhoneAuthProvider.PROVIDER_ID,
recaptchaParameters: {
type: 'image', // 'audio'
size: 'normal', // 'invisible' or 'compact'
badge: 'bottomleft' //' bottomright' or 'inline' applies to invisible.
},
defaultCountry: 'RU', // Set default country to the United Kingdom (+44).
loginHint: 'phone',
whitelistedCountries: ['RU', '+7']
}
]
);
break;
default:
break;
}
});
// FirebaseUI config.
var uiConfig = {
signInFlow: 'popup',
// signInSuccessUrl: 'https://app.distant-office.ru/v2/home',
callbacks: {
// signInFailure: function(error) {
// if (error.code != 'firebaseui/anonymous-upgrade-merge-conflict') {
// console.log('!=firebaseui/anonymous-upgrade-merge-conflict');
// return Promise.resolve();
// }
// var cred = error.credential;
// console.log('==firebaseui/anonymous-upgrade-merge-conflict');
// return firebase.auth().signInWithCredential(cred);
// },
signInSuccessWithAuthResult: function(authResult, redirectUrl) {
firebase.auth().currentUser.getIdToken(false).then(function(idToken) {
// $.ajax({url: 'https://app.distant-office.ru/api/chat_v3/auth_firebase.php?android_id_install=mac_39F8026BEFDE4D9CA49BB3B1&idToken='+idToken,
$("#modal_auth").modal("hide");
$.ajax({url: 'https://app.distant-office.ru/api/chat_v3/auth_firebase.php?idToken='+idToken,
dataType:'json',
xhrFields: {
withCredentials: true
},
crossDomain: true,
success: function(data){
if (data.login) {
window.location.href = '/login/chat/' + data.device.token + '/' + data.android_id_install;
}
}
});
}).catch(function(error) {
console.log("getIdToken error: "+error);
// Handle error
});
return false;
},
},
signInOptions: signIn,
// Terms of service url.
credentialHelper: firebaseui.auth.CredentialHelper.NONE,
tosUrl: ''
};
firebase.initializeApp(config);
// Initialize the FirebaseUI Widget using Firebase.
var ui = new firebaseui.auth.AuthUI(firebase.auth());
// The start method will wait until the DOM is loaded.
ui.start('#firebaseui-auth-container', uiConfig);
/*
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
var displayName = user.displayName;
var email = user.email;
var emailVerified = user.emailVerified;
var photoURL = user.photoURL;
var uid = user.uid;
var phoneNumber = user.phoneNumber;
var providerData = user.providerData;
user.getIdToken().then(function(accessToken) {
console.log( 'Signed in');
console.log( JSON.stringify({
displayName: displayName,
email: email,
emailVerified: emailVerified,
phoneNumber: phoneNumber,
photoURL: photoURL,
uid: uid,
accessToken: accessToken,
providerData: providerData
}, null, ' '));
});
} else {
// User is signed out.
console.log( 'Signed out');
}
}, function(error) {
console.log(error);
});
*/
}