diff --git a/games/1v1-lol/login.js b/games/1v1-lol/login.js new file mode 100644 index 0000000..b61f7f0 --- /dev/null +++ b/games/1v1-lol/login.js @@ -0,0 +1,149 @@ +var tempErrorCreds; +var tempProviderName; + +function retrieveIdToken(successCallback, errorCallback) { + if(firebase.auth().currentUser === null){ + if(errorCallback !== null) + errorCallback("User is null"); + return; + } + + firebase.auth().currentUser.getIdToken().then(function (idToken) { + var resultObj = { + token: idToken, + displayName: firebase.auth().currentUser.displayName + }; + + if (successCallback !== undefined) { + + successCallback(resultObj); + } + }) + .catch(function (error) { + console.log(error); + if (errorCallback !== undefined) + errorCallback(error.message); + }); +} + +function anonymousLogin(successCallback, errorCallback) { + var resultObj = { + token: "", + displayName: "guest" + }; + + if (successCallback !== undefined) { + + successCallback(resultObj); + } +} + +function firebaseLogin(providerName, successCallback, errorCallback) { + if (providerName === "anonymous") { + anonymousLogin(successCallback, errorCallback); + return; + } + + var user = firebase.auth().currentUser; + + if(user != null && !user.isAnonymous){ + retrieveIdToken(successCallback, errorCallback); + return; + } + + var provider = getProvider(providerName); + firebase.auth().useDeviceLanguage(); + + //var task = firebase.auth().currentUser.isAnonymous ? firebase.auth().signInWithPopup(provider) : firebase.auth().linkWithPopup(provider); + + firebase.auth().signInWithPopup(provider).then(function (result) { + console.log("Successful sign in"); + retrieveIdToken(successCallback, errorCallback); + }) + .catch(function (error) { + // Handle Errors here. + var errorCode = error.code; + var errorMessage = error.message; + // The email of the user's account used. + var email = error.email; + // The firebase.auth.AuthCredential type that was used. + tempErrorCreds = error.credential; + console.log(error); + + if (errorCallback !== undefined) + errorCallback(error.message); + + if (errorCode === 'auth/account-exists-with-different-credential') { + // User's email already exists. + // Get sign-in methods for this email. + firebase.auth().fetchSignInMethodsForEmail(email).then(function (methods) { + // the first method in the list will be the "recommended" method to use. + if (methods.length == 0) + return; + // Sign in to provider. + tempProviderName = methods[0].trim(); + setModalContent("generalModalContent", + "
Please press the button to login: