{"version":3,"file":"session.DIYbw0n9.js","sources":["../../../src/lib/session.ts"],"sourcesContent":["import Cookies from 'js-cookie'\nimport type { CookieAttributes } from 'node_modules/@types/js-cookie'\n\nconst SFC_PRIMARY_COOKIE_NAME = 'sfc_primary'\n\ninterface UserSubscriptions {\n subscription_order_id: string\n}\n\nclass LoginError extends Error {\n constructor(message: string) {\n super(message)\n Object.setPrototypeOf(this, LoginError.prototype)\n }\n}\n\nlet fetchUserSubscriptionsPromise!: Promise\nconst fetchUserSubscriptions = (): Promise => {\n if (fetchUserSubscriptionsPromise === undefined) {\n fetchUserSubscriptionsPromise = new Promise(\n async (resolve, reject) => {\n const sfcCookie = getSfcPrimaryCookie()\n if (sfcCookie === undefined) {\n resolve(undefined)\n } else {\n try {\n const response = await fetch(new URL(import.meta.env.PUBLIC_GRAPHQL_URL), {\n method: 'post',\n body: JSON.stringify({\n query: `query {\n no_graphql_cache()\n user_subscriptions(access_token: \"${sfcCookie}\", app_id: 1) {\n subscription_order_id\n }\n }`\n }),\n headers: {\n 'Content-Type': 'application/json'\n }\n })\n\n if (!response.ok) {\n resolve(undefined)\n } else {\n const result = await response.json()\n resolve(result?.data?.user_subscriptions)\n }\n } catch (error) {\n reject(error)\n }\n }\n }\n )\n }\n\n return fetchUserSubscriptionsPromise\n}\n\ntype AuthUserInfo = {\n authenticated: boolean\n email?: string\n username?: string\n}\n\nlet fetchAuthUserInfoPromise!: Promise\nconst fetchAuthUserInfo = (): Promise => {\n if (fetchAuthUserInfoPromise !== undefined) {\n return fetchAuthUserInfoPromise\n }\n\n fetchAuthUserInfoPromise = new Promise(async (resolve, _reject) => {\n const sfcCookie = getSfcPrimaryCookie()\n\n if (sfcCookie === undefined) {\n resolve({ authenticated: false })\n } else {\n const url = new URL(import.meta.env.PUBLIC_USER_MANAGEMENT_HOST + '/securelogins/api/v1/me')\n const params = {\n access_token: sfcCookie,\n app_id: '1',\n requested_resource: '/'\n }\n url.search = new URLSearchParams(params).toString()\n\n try {\n const response = await fetch(url)\n\n if (!response.ok) {\n resolve({ authenticated: false })\n } else {\n const result = await response.json()\n const content = result.content\n resolve({\n authenticated: result.success,\n email: content?.email,\n username: content?.username\n })\n }\n } catch (error) {\n console.error('Error while authenticating user:', error)\n resolve({ authenticated: false })\n }\n }\n })\n\n return fetchAuthUserInfoPromise\n}\n\nconst getUserInfo = async (): Promise => {\n return await fetchAuthUserInfo()\n}\n\nconst isLoggedIn = async (): Promise => {\n const userInfo = await fetchAuthUserInfo()\n return userInfo.authenticated\n}\n\nconst hasSubscription = async (): Promise => {\n const userSubscriptions = await fetchUserSubscriptions()\n return Boolean(userSubscriptions && userSubscriptions.subscription_order_id)\n}\n\nconst getSfcPrimaryCookie = () => {\n return Cookies.get(SFC_PRIMARY_COOKIE_NAME)\n}\n\nconst storeSfcPrimaryCookie = (loginToken: string) => {\n const cookieOptions: CookieAttributes = {\n expires: 360\n }\n\n if (window.location.hostname.includes('savingforcollege.com')) {\n cookieOptions.domain = 'savingforcollege.com'\n cookieOptions.path = '/'\n }\n\n Cookies.set(SFC_PRIMARY_COOKIE_NAME, encodeURIComponent(loginToken), cookieOptions)\n}\n\nconst storeEmailCookie = ({\n cookieName,\n cookieValue\n}: {\n cookieName: string\n cookieValue: string\n}) => {\n const cookieOptions: CookieAttributes = {\n expires: 7 * 4\n }\n\n if (window.location.hostname.includes('savingforcollege.com')) {\n cookieOptions.domain = 'savingforcollege.com'\n cookieOptions.path = '/'\n }\n\n Cookies.set(encodeURIComponent(cookieName), encodeURIComponent(cookieValue), cookieOptions)\n}\n\nconst GENERIC_LOGIN_ERROR_MESSAGE = 'Failed to log in. Please try again.'\n\nconst logIn = async ({ email, password }: { email: string; password: string }) => {\n const params = {\n username_email: email,\n password: password,\n mode: 'login',\n via: 'ajax',\n from_app: 'astro'\n }\n const response = await fetch(new URL(`${import.meta.env.PUBLIC_SFC_HOST}/login/index.php`), {\n method: 'post',\n body: JSON.stringify(params),\n headers: {\n 'Content-Type': 'application/json'\n }\n })\n\n if (!response.ok) {\n throw new LoginError(GENERIC_LOGIN_ERROR_MESSAGE)\n } else {\n const responseData = await response.json()\n\n if (!responseData.success) {\n if (responseData.error) {\n throw new LoginError(responseData.error)\n } else {\n throw new LoginError(GENERIC_LOGIN_ERROR_MESSAGE)\n }\n } else {\n storeSfcPrimaryCookie(responseData.login_token)\n\n if (\n responseData.email_cookie &&\n responseData.email_cookie.cookie_name &&\n responseData.email_cookie.cookie_value\n ) {\n storeEmailCookie({\n cookieName: responseData.email_cookie.cookie_name,\n cookieValue: responseData.email_cookie.cookie_value\n })\n }\n }\n }\n\n if (window.heap) {\n window.heap.addUserProperties({ email: email })\n }\n}\n\nexport {\n getSfcPrimaryCookie,\n getUserInfo,\n isLoggedIn,\n hasSubscription,\n logIn,\n LoginError,\n GENERIC_LOGIN_ERROR_MESSAGE,\n type AuthUserInfo\n}\n"],"names":["SFC_PRIMARY_COOKIE_NAME","LoginError","message","fetchUserSubscriptionsPromise","fetchUserSubscriptions","resolve","reject","sfcCookie","getSfcPrimaryCookie","response","result","error","fetchAuthUserInfoPromise","fetchAuthUserInfo","_reject","url","params","content","getUserInfo","isLoggedIn","hasSubscription","userSubscriptions","Cookies","storeSfcPrimaryCookie","loginToken","cookieOptions","storeEmailCookie","cookieName","cookieValue","GENERIC_LOGIN_ERROR_MESSAGE","logIn","email","password","responseData"],"mappings":"gZAGA,MAAMA,EAA0B,cAMhC,MAAMC,UAAmB,KAAM,CAC7B,YAAYC,EAAiB,CAC3B,MAAMA,CAAO,EACN,OAAA,eAAe,KAAMD,EAAW,SAAS,CAClD,CACF,CAEA,IAAIE,EACJ,MAAMC,EAAyB,KACzBD,IAAkC,SACpCA,EAAgC,IAAI,QAClC,MAAOE,EAASC,IAAW,CACzB,MAAMC,EAAYC,IAClB,GAAID,IAAc,OAChBF,EAAQ,MAAS,MAEb,IAAA,CACI,MAAAI,EAAW,MAAM,MAAM,IAAI,IAAI,0CAAkC,EAAG,CACxE,OAAQ,OACR,KAAM,KAAK,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