Move all specific cookie logic to cookie-helper
This commit is contained in:
parent
4510241bdd
commit
0ba926d20c
2 changed files with 84 additions and 59 deletions
|
|
@ -78,6 +78,38 @@ export function getDeviceIdValue() {
|
||||||
return "00000000-0000-0000-0000-000000000000";
|
return "00000000-0000-0000-0000-000000000000";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function regenerateDeviceId() {
|
||||||
|
if (!isCookieSet(cookieNames.DXDEV)) {
|
||||||
|
setCookieProperties(
|
||||||
|
{
|
||||||
|
[cookieNames.DXDEV]: `did=${crypto.randomUUID()}`,
|
||||||
|
},
|
||||||
|
{ maxAge: 365 * 24 * 60 * 60 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getUserIdValue() {
|
||||||
|
const cookieValue = getCookieValueByName(cookieNames.FUNNEL_USER_ID);
|
||||||
|
|
||||||
|
if (cookieValue) {
|
||||||
|
return cookieValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
return "00000000-0000-0000-0000-000000000000";
|
||||||
|
}
|
||||||
|
|
||||||
|
export function regenerateUserId() {
|
||||||
|
if (!isCookieSet(cookieNames.FUNNEL_USER_ID)) {
|
||||||
|
setCookieProperties(
|
||||||
|
{
|
||||||
|
[cookieNames.FUNNEL_USER_ID]: crypto.randomUUID(),
|
||||||
|
},
|
||||||
|
{ maxAge: 7 * 24 * 60 * 60 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Gets value of skey cookie, returns 0 if not found.
|
Gets value of skey cookie, returns 0 if not found.
|
||||||
*/
|
*/
|
||||||
|
|
@ -91,6 +123,17 @@ export function getSessionKeyValue() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function setSessionKeyIfUnset(value) {
|
||||||
|
if (!isCookieSet(cookieNames.FUNNEL_SESSION_KEY)) {
|
||||||
|
setCookieProperties(
|
||||||
|
{
|
||||||
|
[cookieNames.FUNNEL_SESSION_KEY]: value,
|
||||||
|
},
|
||||||
|
{ maxAge: 30 * 60 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Gets value of skey cookie, returns 0 if not found.
|
Gets value of skey cookie, returns 0 if not found.
|
||||||
*/
|
*/
|
||||||
|
|
@ -104,6 +147,17 @@ export function getSessionIdValue() {
|
||||||
return "00000000-0000-0000-0000-000000000000";
|
return "00000000-0000-0000-0000-000000000000";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function setSessionIdIfUnset(value) {
|
||||||
|
if (!isCookieSet(cookieNames.SESSION_ID)) {
|
||||||
|
setCookieProperties(
|
||||||
|
{
|
||||||
|
[cookieNames.SESSION_ID]: value,
|
||||||
|
},
|
||||||
|
{ maxAge: 30 * 60 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Updates session ID cookie with new expiration date
|
Updates session ID cookie with new expiration date
|
||||||
*/
|
*/
|
||||||
|
|
@ -111,16 +165,6 @@ export function updateSessionIdCookie() {
|
||||||
createOrUpdateCookie(cookieNames.SESSION_ID, getSessionIdValue(), { maxAge: 60 * 30 });
|
createOrUpdateCookie(cookieNames.SESSION_ID, getSessionIdValue(), { maxAge: 60 * 30 });
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getUserIdValue() {
|
|
||||||
const cookieValue = getCookieValueByName(cookieNames.FUNNEL_USER_ID);
|
|
||||||
|
|
||||||
if (cookieValue) {
|
|
||||||
return cookieValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return "00000000-0000-0000-0000-000000000000";
|
|
||||||
}
|
|
||||||
|
|
||||||
export function setCookieProperties(
|
export function setCookieProperties(
|
||||||
properties,
|
properties,
|
||||||
{ useDefaultFunnelCookieAttributes = true, maxAge, isSecure }
|
{ useDefaultFunnelCookieAttributes = true, maxAge, isSecure }
|
||||||
|
|
@ -142,6 +186,22 @@ export function isCookieSet(name) {
|
||||||
return !!val;
|
return !!val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function areAllSessionCookiesSet() {
|
||||||
|
return (
|
||||||
|
isCookieSet(cookieNames.SESSION_ID) &&
|
||||||
|
isCookieSet(cookieNames.DXDEV) &&
|
||||||
|
isCookieSet(cookieNames.FUNNEL_SESSION_KEY) &&
|
||||||
|
isCookieSet(cookieNames.FUNNEL_USER_ID)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function refreshSessionExpiration() {
|
||||||
|
refreshCookieExpiration(cookieNames.SESSION_ID, 30 * 60);
|
||||||
|
refreshCookieExpiration(cookieNames.DXDEV, 365 * 24 * 60 * 60);
|
||||||
|
refreshCookieExpiration(cookieNames.FUNNEL_USER_ID, 7 * 24 * 60 * 60);
|
||||||
|
refreshCookieExpiration(cookieNames.FUNNEL_SESSION_KEY, 30 * 60);
|
||||||
|
}
|
||||||
|
|
||||||
export function refreshCookieExpiration(name, expirationTime) {
|
export function refreshCookieExpiration(name, expirationTime) {
|
||||||
if (isCookieSet(name)) {
|
if (isCookieSet(name)) {
|
||||||
createOrUpdateCookie(name, getCookieValueByName(name), { maxAge: expirationTime });
|
createOrUpdateCookie(name, getCookieValueByName(name), { maxAge: expirationTime });
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,12 @@ import {
|
||||||
getSessionKeyValue,
|
getSessionKeyValue,
|
||||||
getUserIdValue,
|
getUserIdValue,
|
||||||
isCookieSet,
|
isCookieSet,
|
||||||
refreshCookieExpiration,
|
regenerateDeviceId,
|
||||||
|
regenerateUserId,
|
||||||
|
refreshSessionExpiration,
|
||||||
|
areAllSessionCookiesSet,
|
||||||
|
setSessionIdIfUnset,
|
||||||
|
setSessionKeyIfUnset,
|
||||||
} from "@/helpers/heritage-integration/cookie-helper";
|
} from "@/helpers/heritage-integration/cookie-helper";
|
||||||
import { queryStrings } from "@/constants/query-strings";
|
import { queryStrings } from "@/constants/query-strings";
|
||||||
import { experimentSettings } from "@/constants/experiments";
|
import { experimentSettings } from "@/constants/experiments";
|
||||||
|
|
@ -139,22 +144,8 @@ export default {
|
||||||
},
|
},
|
||||||
|
|
||||||
async initSession() {
|
async initSession() {
|
||||||
if (!isCookieSet(cookieNames.FUNNEL_USER_ID)) {
|
regenerateDeviceId();
|
||||||
setCookieProperties(
|
regenerateUserId();
|
||||||
{
|
|
||||||
[cookieNames.FUNNEL_USER_ID]: crypto.randomUUID(),
|
|
||||||
},
|
|
||||||
{ maxAge: 7 * 24 * 60 * 60 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (!isCookieSet(cookieNames.DXDEV)) {
|
|
||||||
setCookieProperties(
|
|
||||||
{
|
|
||||||
[cookieNames.DXDEV]: crypto.randomUUID(),
|
|
||||||
},
|
|
||||||
{ maxAge: 365 * 24 * 60 * 60 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const userId = getUserIdValue(); // cookieNames.FUNNEL_USER_ID
|
const userId = getUserIdValue(); // cookieNames.FUNNEL_USER_ID
|
||||||
const deviceId = getDeviceIdValue(); // cookieNames.DXDEV
|
const deviceId = getDeviceIdValue(); // cookieNames.DXDEV
|
||||||
|
|
@ -178,37 +169,18 @@ export default {
|
||||||
);
|
);
|
||||||
|
|
||||||
if (response?.data) {
|
if (response?.data) {
|
||||||
if (response.data.sessionKey && !isCookieSet(cookieNames.FUNNEL_SESSION_KEY)) {
|
if (response.data.sessionKey) {
|
||||||
setCookieProperties(
|
setSessionKeyIfUnset(response.data.sessionKey);
|
||||||
{
|
|
||||||
[cookieNames.FUNNEL_SESSION_KEY]: response.data.sessionKey,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
maxAge: 30 * 60,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response.data.sessionId && !isCookieSet(cookieNames.SESSION_ID)) {
|
if (response.data.sessionId) {
|
||||||
setCookieProperties(
|
setSessionIdIfUnset(response.data.sessionId);
|
||||||
{
|
|
||||||
[cookieNames.SESSION_ID]: response.data.sessionId,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
maxAge: 30 * 60,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
noSession() {
|
noSession() {
|
||||||
return !(
|
return !areAllSessionCookiesSet();
|
||||||
isCookieSet(cookieNames.SESSION_ID) &&
|
|
||||||
isCookieSet(cookieNames.DXDEV) &&
|
|
||||||
isCookieSet(cookieNames.FUNNEL_SESSION_KEY) &&
|
|
||||||
isCookieSet(cookieNames.FUNNEL_USER_ID)
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
async validateSession() {
|
async validateSession() {
|
||||||
|
|
@ -216,14 +188,7 @@ export default {
|
||||||
await this.initSession();
|
await this.initSession();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.refreshSessionExpiration();
|
refreshSessionExpiration();
|
||||||
},
|
|
||||||
|
|
||||||
refreshSessionExpiration() {
|
|
||||||
refreshCookieExpiration(cookieNames.SESSION_ID, 30 * 60);
|
|
||||||
refreshCookieExpiration(cookieNames.DXDEV, 365 * 24 * 60 * 60);
|
|
||||||
refreshCookieExpiration(cookieNames.FUNNEL_USER_ID, 7 * 24 * 60 * 60);
|
|
||||||
refreshCookieExpiration(cookieNames.FUNNEL_SESSION_KEY, 30 * 60);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
removeParamsFromEndpoint(endpoint) {
|
removeParamsFromEndpoint(endpoint) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue