let config = { domain: 'https://auth.oasislabs.com', clientId: 'CAH9i26ppoHpPRz7ktLpVES', }; if (window.location.origin.toLowerCase() === 'https://personalai.webflow.io') { config.clientId = 'CWcWwFsqtkQb9k37aaXwVPk'; } const appURL = "https://app.personal.ai/"; const n8nURL = "https://prod-api-workflow.luther.ai/"; const lisURL = "https://p4stage-lis.mystack.ai/"; if (window.location.href.indexOf("error=access_denied") > -1) { window.location.href = '/signin-failed'; } const attachListeners = () => { document.querySelectorAll(".request-invitation").forEach((item) => { sessionStorage.loginClicked = true; item.addEventListener("click", login); }); document.querySelectorAll(".request-invitation-main").forEach((item) => { sessionStorage.loginClicked = true; item.addEventListener("click", login); }); document.querySelectorAll(".log-in").forEach((item) => { sessionStorage.loginClicked = true; item.addEventListener("click", login); }); const logoutButton = document.querySelector("[oasis-logout]"); if (logoutButton) { logoutButton.addEventListener("click", () => logout()); } }; function parseBool(value) { return value.toLowerCase() === "true"; } function captureInfoFromURL() { try { const params = new URL(window.location).searchParams; const token = params.get("token") || localStorage.handShakeToken; const email = params.get("email") || localStorage.login; const expiresOn = params.get("expiresOn") || localStorage.expiresOn; let user = params.get("userState") || localStorage.userState; if (!user) { user = "new"; } if (params.get("token") && params.get("token").length > 0) { if (token) window.localStorage.handShakeToken = token; if (email) window.localStorage.login = email; if (expiresOn) window.localStorage.expiresOn = expiresOn; if (user) window.localStorage.user = user; } if (window.localStorage.handShakeToken && window.localStorage.handShakeToken.length > 1) { window.localStorage.isAuthenticated = true; window.localStorage.isHandShakeTokenAvailable = true; window.localStorage.firstName = "TBD"; } else { window.localStorage.isHandShakeTokenAvailable = false; } } catch (err) { console.error("captureInfoFromURL", err.message); } } let oasis = null; const configureClient = async () => { attachListeners(); const oidcConfig = { authority: config.domain, client_id: config.clientId, redirect_uri: window.location.origin + '/', response_type: 'code', scope: 'openid profile email', filterProtocolClaims: false, loadUserInfo: false, extraQueryParams: { audience: 'https://api.oasislabs.com/parcel', }, extraTokenParams: { audience: 'https://api.oasislabs.com/parcel', }, }; oasis = new Oidc.UserManager(oidcConfig); }; const login = async () => { await oasis.signinRedirect({ redirect_uri: window.location.origin + '/', }); }; // If the user was not on-boarded earlier, then continue with on-boarding if login btn is clicked // else show webflow update with AIP address and memory count if login btn is not clicked // Otherwise, route to /user-journey in app.personal.ai const routeBasedOnTokenInfo = (currentUserJourneyStep, userAIPAddress, handShakeToken, email, expiresOn, memCount) => { if (currentUserJourneyStep === "USER_PROFILE") { window.location.href = window.location.origin + "/begin-user-journey"; } else if (currentUserJourneyStep === "WAITING_FOR_ONBOARDING" || currentUserJourneyStep === "ONBOARDING_STARTED" || currentUserJourneyStep === "ONBOARDING_COMPLETED") { const href = userAIPAddress + "?token=" + handShakeToken + "&email=" + email + "&expiresOn=" + expiresOn; if (sessionStorage.loginClicked) { delete sessionStorage.loginClicked; window.location.href = href; return; } $('#get-started').hide(); $('.create-my-ai-btn').hide(); $('#launch-app').show(); $('.loading-animation').hide(); // update memory count const memCntEl = document.getElementById('memory-count'); if (memCntEl) { memCntEl.innerText = `${memCount} Memories`; } // update AIP field const aipEl = document.getElementById('aip-address'); if (aipEl) { aipEl.text = userAIPAddress.replace('https://', ''); aipEl.href = href; } $(".page-content").show(); } else { window.location.href = appURL + "user-journey" + "?token=" + handShakeToken + "&email=" + email + "&expiresOn=" + expiresOn; } } const routeBasedOnUserInfo = (response, currentUserJourneyStep, userAIPAddress, email, memCount) => { if (response && response.token) { const handShakeToken = response.token; window.localStorage.handShakeToken = handShakeToken; window.localStorage.isHandShakeTokenAvailable = true; const date = new Date(); const expiresOn = date.setFullYear(date.getFullYear() + 1); routeBasedOnTokenInfo(currentUserJourneyStep, userAIPAddress, handShakeToken, email, expiresOn, memCount); } }; const getUserInfoSettings = (email, token) => { const payload = { token: token, login: email, authProvider: "OASIS", signupFrom: "LFE", domainName: "Luther", inviteCode: "", }; // Replace the below URL with the Production URL return { url: lisURL + "signup/user", method: "POST", timeout: 0, dataType: "json", contentType: "application/json", data: JSON.stringify(payload), }; }; const getUserJourneySettings = (email) => { // Replace the n8n API before deploying to the production return { url: n8nURL + "get-active-status?email=" + email, method: "GET", timeout: 0, }; } const getUserFirstName = (email) => { // Replace the n8n API before deploying to the production return { url: n8nURL + "get-name?email=" + email, method: "GET", timeout: 0, }; } const startJourneyWhenNoResponse = (response) => { // If there is no response, then assume that the user did not complete the on-boarding journey. if (!response) window.location.href = window.location.origin + "/begin-user-journey"; } const handleAuthRedirection = async () => { const query = window.location.search; if (query.includes("code=") && query.includes("state=")) { // Process the login state await oasis.signinRedirectCallback(); window.history.replaceState({}, document.title, window.location.pathname); } } const getCurrentUserJourneyStep = (response) => { // if the CURRENT_USER_JOURNEY_STEP is empty string, then set it to "USER_PROFILE" return response["CURRENT_USER_JOURNEY_STEP"] !== "" ? response["CURRENT_USER_JOURNEY_STEP"] : "USER_PROFILE"; } const getAIPAddress = (response) => { return response["domain"]; } const getMemCount = (response) => { return response["memblockcount"] || 0; } const captureUserGoal = (response) => { window.localStorage.goal = response && response.goal ? response.goal : ""; } const captureUserFirstName = (response, firstName) => { if (response && response.first_name && response.first_name.length > 0) { window.localStorage.firstName = response.first_name; } else { window.localStorage.firstName = firstName ? firstName : ""; } } const handleAuth = async () => { captureInfoFromURL(); if (!parseBool(window.localStorage.isHandShakeTokenAvailable)) { await configureClient(); await oasis.removeUser(); await handleAuthRedirection(); } let isAuthenticated = false; let email = null; let token = null; let handShakeToken = null; let expiresOn = null; let firstName = null; if (window.localStorage.enableDebugging === "true") debugger; if (parseBool(window.localStorage.isHandShakeTokenAvailable)) { isAuthenticated = true; email = window.localStorage.login; handShakeToken = window.localStorage.handShakeToken; expiresOn = window.localStorage.expiresOn; } else { let oasisUser = await oasis.getUser(); isAuthenticated = oasisUser && !oasisUser.expired; window.localStorage.isAuthenticated = isAuthenticated; if (isAuthenticated) { const user = oasisUser; let name = user.profile.name; email = user.profile.email ? user.profile.email : name; firstName = user.profile.given_name; token = user.id_token; const date = new Date(); expiresOn = date.setFullYear(date.getFullYear() + 1); // set in local storage window.localStorage.login = email; window.localStorage.expiresOn = expiresOn; } } if (isAuthenticated) { $.ajax(getUserFirstName(email)).done(function (response) { captureUserFirstName(response, firstName); $.ajax(getUserJourneySettings(email)).done(function (response) { startJourneyWhenNoResponse(response); const currentUserJourneyStep = getCurrentUserJourneyStep(response); const userAIPAddress = getAIPAddress(response); const memCount = getMemCount(response) captureUserGoal(response); if (parseBool(window.localStorage.isHandShakeTokenAvailable)) { routeBasedOnTokenInfo(currentUserJourneyStep, userAIPAddress, handShakeToken, email, expiresOn, memCount); } else { $.ajax(getUserInfoSettings(email, token)).done(function (response) { routeBasedOnUserInfo(response, currentUserJourneyStep, userAIPAddress, email, memCount); }); } }); }); } else { // Do nothing. Stay on this page $(".loading-animation").hide(); $(".page-content").show(); } }; window.onload = handleAuth; // Login Check function function loginEventGtm(){ // setting the variables var user_email = window.localStorage.login || ''; var user_firstname = window.localStorage.firstName || ''; window.dataLayer = window.dataLayer || []; dataLayer.push({ 'event':'login', 'first_name': user_firstname, 'last_name': '', 'email': user_email, 'user_id' : user_email, 'properties': { 'email': user_email, 'user_id': user_email, 'first_name': user_firstname, 'last_name':'', 'date': new Date().toISOString().split('.')[0], 'first_login_date':'', 'login_method':'', } }); }