@salesforcedevs/dx-components 1.2.2-avatar-button-6 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,347 +0,0 @@
1
- import { api, LightningElement } from "lwc";
2
- import Cookies from 'js-cookie';
3
- import { track } from "dxUtils/analytics";
4
-
5
- // TODO: move to environment variable
6
- const TBID_BASE_URL = "https://dev1-trailblazer-identity.cs192.force.com";
7
- const TBID_SETTINGS_URL = `${TBID_BASE_URL}/settings`;
8
- const TBID_PROFILE_URL = `${TBID_BASE_URL}/id`;
9
- const TBID_IFRAME_URL = `${TBID_BASE_URL}/secur/logout.jsp`;
10
- // TODO: move to environment variable
11
- const TBID_API_BASE_URL = "https://development.developer.salesforce.com/tbid";
12
- const TBID_API_LOGOUT_URL = `${TBID_API_BASE_URL}/logout`;
13
- const TBID_API_USERINFO_URL = `${TBID_API_BASE_URL}/userinfo`;
14
- const TBID_API_LOGIN_URL = `${TBID_API_BASE_URL}/dologin`;
15
- const TBID_API_TOKEN_URL = `${TBID_API_BASE_URL}/token`;
16
- const TBID_API_PLATFORM_EVENTS_URL = `${TBID_API_BASE_URL}/platform-events`;
17
-
18
- declare global {
19
- interface Window {
20
- SFIDWidget?: {
21
- logout(): void;
22
- login(): void;
23
- openid_response: any;
24
- disabled: boolean;
25
- };
26
- }
27
- }
28
-
29
- export interface UserInfo {
30
- avatarImgSrc?: string;
31
- company?: string;
32
- firstName?: string;
33
- id?: string;
34
- lastName?: string;
35
- orgId?: string;
36
- relationship?: string;
37
- role?: string;
38
- username?: string;
39
- readonly fullName: string;
40
- }
41
-
42
- interface EventSourceEvent extends Event {
43
- id: string;
44
- retry?: number;
45
- data: string;
46
- event?: string;
47
- }
48
-
49
- const enum PlatformEventsType {
50
- UserDataChange = "UserDataChange",
51
- UserPhotoChange = "UserPhotoChange",
52
- UserMerge = "UserMerge"
53
- }
54
-
55
- const trackableUserInfo = new Set([
56
- "company",
57
- "id",
58
- "orgId",
59
- "relationship",
60
- "role"
61
- ]);
62
-
63
- export default class AvatarButton extends LightningElement {
64
- @api size: "small" | "medium" | "large" = "medium";
65
-
66
- private userInfo: UserInfo = {
67
- get fullName() {
68
- if (this.firstName && this.lastName) {
69
- return `${this.firstName} ${this.lastName}`;
70
- }
71
- return this.firstName || this.lastName || "";
72
- }
73
- };
74
- private isLoading = false;
75
- private settingsUrl = TBID_SETTINGS_URL;
76
- private profileUrl = TBID_PROFILE_URL;
77
- private eventSource?: EventSource;
78
- private _hasRendered = false;
79
- private _didReceiveUserInfo = false;
80
-
81
- private get loginUrl() {
82
- return `${TBID_API_LOGIN_URL}?startURL=${encodeURIComponent(
83
- window.location.href
84
- )}`;
85
- }
86
-
87
- private get isLoggedIn() {
88
- return this._didReceiveUserInfo;
89
- }
90
-
91
- connectedCallback() {
92
- const isLoginSuccessRedirect = Cookies.get('tbidLoginSuccess') === 'true';
93
-
94
- if (isLoginSuccessRedirect) {
95
- this.isLoading = true;
96
- Cookies.remove('tbidLoginSuccess'); // cleanup
97
- }
98
-
99
- window.addEventListener("tbid-login", this.handleSsoLogin);
100
- window.addEventListener("tbid-logout", this.handleSsoLogout);
101
-
102
- const isWidgetDisabled = window.SFIDWidget?.disabled;
103
- const isWidgetLoggedIn = window.SFIDWidget?.openid_response;
104
-
105
- // If the component loads and (1) we are logging in and (2) the embedded login widget script
106
- // is in the DOM but (3) the widget has either not loaded or not completed login yet, we
107
- // defer the userInfo request in order to allow the SFIDWidget the time to trigger it (via
108
- // it's own login handler) after SSO login. If any of (1) - (3) are false, then we request
109
- // user info immediately. The check prevents duplicate requests.
110
- if (
111
- this.isLoading &&
112
- (!window.SFIDWidget || (!isWidgetDisabled && !isWidgetLoggedIn)) &&
113
- document.querySelector('script[src*="authProviderEmbeddedLogin"]')
114
- ) {
115
- setTimeout(() => {
116
- if (!window.SFIDWidget?.openid_response) {
117
- // this.trackLogin(); TODO: track only clicks
118
- this.requestUserInfo();
119
- }
120
- }, 1000);
121
- } else {
122
- if (this.isLoading) {
123
- // This was a login.
124
- // this.trackLogin(); TODO: track only clicks
125
- }
126
- this.requestUserInfo();
127
- }
128
- }
129
-
130
- disconnectedCallback() {
131
- window.removeEventListener("tbid-login", this.handleSsoLogin);
132
- window.removeEventListener("tbid-logout", this.handleSsoLogout);
133
- this.teardownEventSource();
134
- }
135
-
136
- private handleUserDataChange = ({ data }: EventSourceEvent) => {
137
- let parsedEventData: any;
138
-
139
- try {
140
- parsedEventData = JSON.parse(data);
141
- } catch (ex) {
142
- console.error(
143
- `Unparseable ${PlatformEventsType.UserDataChange} data received`
144
- );
145
- return;
146
- }
147
-
148
- this.updateAvatarWithUserInfo(parsedEventData);
149
- };
150
-
151
- private setupEventSource = () => {
152
- // subscribe to platform events
153
- this.eventSource = new EventSource(TBID_API_PLATFORM_EVENTS_URL);
154
- this.eventSource.addEventListener(
155
- PlatformEventsType.UserDataChange,
156
- // eslint-disable-next-line no-undef
157
- this.handleUserDataChange as EventListener
158
- );
159
- this.eventSource.addEventListener(
160
- PlatformEventsType.UserPhotoChange,
161
- this.requestUserInfo
162
- );
163
- this.eventSource.addEventListener(
164
- PlatformEventsType.UserMerge,
165
- this.handleSsoLogout
166
- );
167
- };
168
-
169
- private teardownEventSource = () => {
170
- if (!this.eventSource) {
171
- return;
172
- }
173
-
174
- this.eventSource.removeEventListener(
175
- PlatformEventsType.UserDataChange,
176
- // eslint-disable-next-line no-undef
177
- this.handleUserDataChange as EventListener
178
- );
179
- this.eventSource.removeEventListener(
180
- PlatformEventsType.UserPhotoChange,
181
- this.requestUserInfo
182
- );
183
- this.eventSource.removeEventListener(
184
- PlatformEventsType.UserMerge,
185
- this.handleSsoLogout
186
- );
187
- this.eventSource.close();
188
- this.eventSource = undefined;
189
- };
190
-
191
- private platformEventsSubscribe = () => {
192
- this.teardownEventSource();
193
- this.setupEventSource();
194
- };
195
-
196
- // This handles logout from within this component, rather than from SSO via the SFIDWidget.
197
- private handleLogout = (isSsoLogout: boolean) => {
198
- this._didReceiveUserInfo = false;
199
- this.isLoading = false;
200
- this.updateAvatarWithUserInfo({}); // clear old info
201
- this.teardownEventSource();
202
-
203
- if (!isSsoLogout) {
204
- this.trackLogout();
205
- }
206
-
207
- if (!isSsoLogout && window.SFIDWidget?.openid_response) {
208
- // If the SFIDWidget is around and has an access token, and if logout was not *already*
209
- // triggered by SSO logout, defer to the SFIDWidget. This will ensure that the SSO
210
- // session is logged out as well.
211
- window.SFIDWidget.logout();
212
- } else {
213
- // Always clear the session token; if not SSO logout, this will also revoke the token with
214
- // TBID; if SSO logout, that step is already taken care of
215
- fetch(`${TBID_API_LOGOUT_URL}?isSsoLogout=${isSsoLogout}`); // no need to await this
216
-
217
- if (!isSsoLogout) {
218
- // Dropping an iframe is required to fully get TBID to destroy the session; this is
219
- // a TBID issue and they have requested that we do this for now.
220
- const ifr = document.createElement("iframe");
221
- ifr.setAttribute("src", TBID_IFRAME_URL);
222
- document.body.appendChild(ifr);
223
- }
224
- }
225
- };
226
-
227
- // This will only be called for "seamless SSO" login by the embedded login widget (SFIDWidget) on the website, if it exists.
228
- private handleSsoLogout = this.handleLogout.bind(this, true);
229
-
230
- private handleComponentLogout = this.handleLogout.bind(this, false);
231
-
232
- // This will only be called for "seamless SSO" login by the embedded login widget (SFIDWidget) on the website, if it exists.
233
- private handleSsoLogin = async (event: Event) => {
234
- const { userInfo } = (event as CustomEvent).detail;
235
- const token = userInfo?.access_token;
236
-
237
- // `token` should always be defined if an SSO login occurs, but we check for extra safety
238
- if (token) {
239
- this.isLoading = true;
240
- // If an SSO login occurred and we have an SSO access token, we want to start using
241
- // it rather than some other non-linked access token, for the "seamless SSO"
242
- // experience
243
- try {
244
- await fetch(TBID_API_TOKEN_URL, {
245
- method: "POST",
246
- headers: {
247
- "Content-Type": "application/json"
248
- },
249
- body: JSON.stringify({
250
- token
251
- })
252
- });
253
- this.platformEventsSubscribe();
254
- } catch (ex) {
255
- console.error(`Attempt to update token failed: ${ex}`);
256
- }
257
- }
258
-
259
- if (userInfo) {
260
- this.updateAvatarWithUserInfo(userInfo);
261
- this._didReceiveUserInfo = true;
262
- }
263
-
264
- // this.trackLogin(); TODO: only track clicks
265
- };
266
-
267
- private handleComponentLogin = () => {
268
- // Either of these methods will trigger a redirect, so we track the successful login after
269
- // the redirect completes, and not here.
270
- if (window.SFIDWidget) {
271
- // If the SFIDWidget is around, defer to it for login. This will ensure that the SSO
272
- // session is used if possible.
273
- window.SFIDWidget.login();
274
- } else {
275
- window.location.href = this.loginUrl;
276
- }
277
- };
278
-
279
- private trackLogin = () => {
280
- track(document, "custEv_userLogin", {
281
- authenticationMethod: "tbid",
282
- id: {
283
- tb: Object.entries(this.userInfo)
284
- .filter(([key]) => trackableUserInfo.has(key))
285
- .reduce(
286
- (infoToTrack, [key, value]) => ({
287
- ...infoToTrack,
288
- [key]: value
289
- }),
290
- {} as Record<string, string>
291
- )
292
- }
293
- });
294
- };
295
-
296
- private trackLogout = () => {
297
- track(document, "custEv_userLogout", {
298
- clickText: "logout",
299
- clickUrl: TBID_API_LOGOUT_URL,
300
- itemTitle: "TBID Logout Link",
301
- elementType: "link"
302
- });
303
- };
304
-
305
- private updateAvatarWithUserInfo = (userInfo: any) => {
306
- if (!userInfo) {
307
- return;
308
- }
309
-
310
- this.userInfo.avatarImgSrc = userInfo.photos?.thumbnail;
311
- this.userInfo.firstName = userInfo.given_name;
312
- this.userInfo.lastName = userInfo.family_name;
313
- this.userInfo.username = userInfo.preferred_username;
314
- this.userInfo.id = userInfo.user_id;
315
- this.userInfo.orgId = userInfo.organization_id;
316
-
317
- if (userInfo.custom_attributes) {
318
- this.userInfo.company = userInfo.custom_attributes.CompanyName;
319
- this.userInfo.relationship =
320
- userInfo.custom_attributes.RelationshipToSalesforce;
321
- this.userInfo.role = userInfo.custom_attributes.Role;
322
- }
323
- // TODO: Consider displaying initials if no photo. Is there always a photo even if just a default one?
324
- };
325
-
326
- private requestUserInfo = async () => {
327
- this.isLoading = true;
328
-
329
- try {
330
- const userInfoRes = await fetch(TBID_API_USERINFO_URL);
331
-
332
- if (userInfoRes.ok) {
333
- if (!this.eventSource) {
334
- this.setupEventSource();
335
- }
336
- const userInfo = await userInfoRes.json();
337
- this.updateAvatarWithUserInfo(userInfo);
338
- this._didReceiveUserInfo = true;
339
- }
340
- } catch (ex) {
341
- // TODO: Something not directly related to auth went wrong. Unsure what to do here.
342
- console.error(`Could not request user info: ${ex}`);
343
- }
344
-
345
- this.isLoading = false;
346
- };
347
- }