-
Notifications
You must be signed in to change notification settings - Fork 2
/
authsignal.ts
189 lines (148 loc) · 5.44 KB
/
authsignal.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import {v4 as uuidv4} from "uuid";
import {setCookie, getCookieDomain, getCookie} from "./helpers";
import {
AuthsignalOptions,
AuthsignalWindowMessage,
AuthsignalWindowMessageData,
LaunchOptions,
PopupLaunchOptions,
TokenPayload,
WindowLaunchOptions,
} from "./types";
import {Passkey} from "./passkey";
import {PopupHandler, WindowHandler} from "./handlers";
const DEFAULT_COOKIE_NAME = "__as_aid";
const DEFAULT_PROFILING_COOKIE_NAME = "__as_pid";
const DEFAULT_BASE_URL = "https://api.authsignal.com/v1";
const TMX_ORG_ID = "4a08uqve";
export class Authsignal {
anonymousId = "";
profilingId = "";
cookieDomain = "";
anonymousIdCookieName = "";
passkey: Passkey;
private _token: string | undefined = undefined;
constructor({
cookieDomain,
cookieName = DEFAULT_COOKIE_NAME,
baseUrl = DEFAULT_BASE_URL,
tenantId,
}: AuthsignalOptions) {
this.cookieDomain = cookieDomain || getCookieDomain();
this.anonymousIdCookieName = cookieName;
if (!tenantId) {
throw new Error("tenantId is required");
}
this.passkey = new Passkey({tenantId, baseUrl});
const idCookie = getCookie(this.anonymousIdCookieName);
if (idCookie) {
this.anonymousId = idCookie;
} else {
this.anonymousId = uuidv4();
setCookie({
name: this.anonymousIdCookieName,
value: this.anonymousId,
expire: Infinity,
domain: this.cookieDomain,
secure: document.location.protocol !== "http:",
});
}
}
launch(url: string, options?: {mode?: "redirect"} & LaunchOptions): undefined;
launch(url: string, options?: {mode: "popup"} & LaunchOptions): Promise<TokenPayload>;
launch(url: string, options?: {mode: "window"} & LaunchOptions): Promise<TokenPayload>;
launch(url: string, options?: LaunchOptions) {
switch (options?.mode) {
case "window":
return this.launchWithWindow(url, options);
case "popup":
return this.launchWithPopup(url, options);
case "redirect":
default:
this.launchWithRedirect(url);
}
}
initAdvancedProfiling(baseUrl?: string) {
const profilingId = uuidv4();
this.profilingId = profilingId;
setCookie({
name: DEFAULT_PROFILING_COOKIE_NAME,
value: profilingId,
expire: Infinity,
domain: this.cookieDomain,
secure: document.location.protocol !== "http:",
});
const tmxProfilingScruiptUrl = baseUrl
? `${baseUrl}/fp/tags.js?org_id=${TMX_ORG_ID}&session_id=${profilingId}`
: `https://h.online-metrix.net/fp/tags.js?org_id=${TMX_ORG_ID}&session_id=${profilingId}`;
const script = document.createElement("script");
script.src = tmxProfilingScruiptUrl;
script.async = false;
script.id = "as_adv_profile";
document.head.appendChild(script);
const pixelContainer = document.createElement("noscript");
pixelContainer.setAttribute("id", "as_adv_profile_pixel");
pixelContainer.setAttribute("aria-hidden", "true");
// Instantiate Pixel
const iframe = document.createElement("iframe");
const profilingPixelUrl = baseUrl
? `${baseUrl}/fp/tags?org_id=${TMX_ORG_ID}&session_id=${profilingId}`
: `https://h.online-metrix.net/fp/tags?org_id=${TMX_ORG_ID}&session_id=${profilingId}`;
iframe.setAttribute("id", "as_adv_profile_pixel");
iframe.setAttribute("src", profilingPixelUrl);
iframe.setAttribute("style", "width: 100px; height: 100px; border: 0; position: absolute; top: -5000px;");
if (pixelContainer) {
pixelContainer.appendChild(iframe);
document.body.prepend(pixelContainer);
}
}
private launchWithRedirect(url: string) {
window.location.href = url;
}
private launchWithPopup(url: string, options: PopupLaunchOptions) {
const {popupOptions} = options;
const popupHandler = new PopupHandler({width: popupOptions?.width});
const popupUrl = `${url}&mode=popup`;
popupHandler.show({url: popupUrl});
return new Promise<TokenPayload>((resolve) => {
const onMessage = (event: MessageEvent) => {
let data: AuthsignalWindowMessageData | null = null;
try {
data = JSON.parse(event.data) as AuthsignalWindowMessageData;
} catch {
// Ignore if the event data is not valid JSON
}
if (data?.event === AuthsignalWindowMessage.AUTHSIGNAL_CLOSE_POPUP) {
this._token = data.token;
popupHandler.close();
}
};
popupHandler.on("hide", () => {
resolve({token: this._token});
});
window.addEventListener("message", onMessage, false);
});
}
private launchWithWindow(url: string, options: WindowLaunchOptions) {
const {windowOptions} = options;
const windowHandler = new WindowHandler();
const windowUrl = `${url}&mode=popup`;
windowHandler.show({url: windowUrl, width: windowOptions?.width, height: windowOptions?.height});
return new Promise<TokenPayload>((resolve) => {
const onMessage = (event: MessageEvent) => {
let data: AuthsignalWindowMessageData | null = null;
try {
data = JSON.parse(event.data) as AuthsignalWindowMessageData;
} catch {
// Ignore if the event data is not valid JSON
}
if (data?.event === AuthsignalWindowMessage.AUTHSIGNAL_CLOSE_POPUP) {
this._token = data.token;
windowHandler.close();
resolve({token: this._token});
}
};
window.addEventListener("message", onMessage, false);
});
}
}