@salesforcedevs/dx-components 0.59.0-avatar-button-10 → 0.59.0-avatar-button-13

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforcedevs/dx-components",
3
- "version": "0.59.0-avatar-button-10",
3
+ "version": "0.59.0-avatar-button-13",
4
4
  "description": "DX Lightning web components",
5
5
  "license": "MIT",
6
6
  "engines": {
@@ -15,7 +15,6 @@
15
15
  "@sfdocs-internal/wires": "^0.6.3",
16
16
  "@vimeo/player": "^2.16.4",
17
17
  "classnames": "^2.2.6",
18
- "cometd": "^7.0.7",
19
18
  "debounce": "^1.2.0",
20
19
  "lodash.get": "^4.4.2",
21
20
  "microtip": "0.2.2",
@@ -1,5 +1,4 @@
1
1
  import { api, LightningElement } from "lwc";
2
- import { CometD } from "cometd";
3
2
 
4
3
  const TBID_BASE_URL = "https://dev1-trailblazer-identity.cs192.force.com";
5
4
  const TBID_SETTINGS_URL = `${TBID_BASE_URL}/settings`;
@@ -15,6 +14,8 @@ declare global {
15
14
  SFIDWidget?: {
16
15
  logout(): void;
17
16
  login(): void;
17
+ openid_response: any;
18
+ disabled: boolean;
18
19
  };
19
20
  }
20
21
  }
@@ -27,9 +28,6 @@ export interface UserInfo {
27
28
  readonly fullName: string;
28
29
  }
29
30
 
30
- // @ts-ignore
31
- window.COMETD = CometD;
32
-
33
31
  export default class AvatarButton extends LightningElement {
34
32
  @api size: "small" | "medium" | "large" = "medium";
35
33
 
@@ -57,13 +55,35 @@ export default class AvatarButton extends LightningElement {
57
55
  }
58
56
 
59
57
  connectedCallback() {
58
+ if (
59
+ new URLSearchParams(window.location.search).get("loginSuccess") ===
60
+ "true"
61
+ ) {
62
+ this.isLoading = true;
63
+ }
64
+
60
65
  window.addEventListener("tbid-login", this.handleSsoLogin);
61
66
  window.addEventListener("tbid-logout", this.handleSsoLogout);
62
67
 
63
- // Whenever this component is added to the DOM and there is not an SFIDWidget present, immediately request
64
- // user info; if the user is logged in and we receive the info, this component will display the avatar UI;
65
- // otherwise, it will display the login button.
66
- if (!window.SFIDWidget) {
68
+ const isWidgetDisabled = window.SFIDWidget?.disabled;
69
+ const isWidgetLoggedIn = window.SFIDWidget?.openid_response;
70
+
71
+ // If the component loads and (1) we are logging in and (2) the embedded login widget script
72
+ // is in the DOM but (3) the widget has either not loaded or not completed login yet, we
73
+ // defer the userInfo request in order to allow the SFIDWidget the time to trigger it (via
74
+ // it's own login handler) after SSO login. If any of (1) - (3) are false, then we request
75
+ // user info immediately. The check prevents duplicate requests.
76
+ if (
77
+ this.isLoading &&
78
+ (!window.SFIDWidget || (!isWidgetDisabled && !isWidgetLoggedIn)) &&
79
+ document.querySelector('script[src*="authProviderEmbeddedLogin"]')
80
+ ) {
81
+ setTimeout(() => {
82
+ if (!window.SFIDWidget?.openid_response) {
83
+ this.requestUserInfo();
84
+ }
85
+ }, 1000);
86
+ } else {
67
87
  this.requestUserInfo();
68
88
  }
69
89
  }
@@ -74,21 +94,21 @@ export default class AvatarButton extends LightningElement {
74
94
  }
75
95
 
76
96
  // This handles logout from within this component, rather than from SSO via the SFIDWidget.
77
- private handleLogout = async (isSsoLogout: boolean) => {
97
+ private handleLogout = (isSsoLogout: boolean) => {
78
98
  this._didReceiveUserInfo = false;
79
99
  this.isLoading = false;
80
100
  this.updateAvatarWithUserInfo({}); // clear old info
81
101
 
82
- if (!isSsoLogout && window.SFIDWidget) {
83
- // If the SFIDWidget is around and logout was not *already* triggered by SSO logout,
84
- // defer to the SFIDWidget. This will ensure that the SSO session is logged out as
85
- // well.
102
+ if (!isSsoLogout && window.SFIDWidget?.openid_response) {
103
+ // If the SFIDWidget is around and has an access token, and if logout was not *already*
104
+ // triggered by SSO logout, defer to the SFIDWidget. This will ensure that the SSO
105
+ // session is logged out as well.
86
106
  window.SFIDWidget.logout();
107
+ } else {
108
+ // Always clear the session token; if not SSO logout, this will also revoke the token with
109
+ // TBID; if SSO logout, that step is already taken care of
110
+ fetch(`${TBID_API_LOGOUT_URL}?isSsoLogout=${isSsoLogout}`); // no need to await this
87
111
  }
88
-
89
- // Always clear the session token; if not SSO logout, this will also revoke the token with
90
- // TBID; if SSO logout, that step is already taken care of
91
- fetch(`${TBID_API_LOGOUT_URL}?isSsoLogout=${isSsoLogout}`); // no need to await this
92
112
  };
93
113
 
94
114
  // This will only be called for "seamless SSO" login by the embedded login widget (SFIDWidget) on the website, if it exists.
@@ -98,7 +118,8 @@ export default class AvatarButton extends LightningElement {
98
118
 
99
119
  // This will only be called for "seamless SSO" login by the embedded login widget (SFIDWidget) on the website, if it exists.
100
120
  private handleSsoLogin = async (event: Event) => {
101
- const token = (event as CustomEvent).detail.userInfo?.access_token;
121
+ const { userInfo } = (event as CustomEvent).detail;
122
+ const token = userInfo?.access_token;
102
123
 
103
124
  // `token` should always be defined if an SSO login occurs, but we check for extra safety
104
125
  if (token) {
@@ -117,16 +138,13 @@ export default class AvatarButton extends LightningElement {
117
138
  })
118
139
  });
119
140
  } catch (ex) {
120
- // If the request fails for some reason, fall back to whatever we've got
121
- this.updateAvatarWithUserInfo(this.userInfo);
122
- console.error(ex);
123
- return;
141
+ console.error(`Attempt to update token failed: ${ex}`);
124
142
  }
125
143
  }
126
144
 
127
- // Unfortunately, the user info provided by the SFIDWidget on SSO login does not contain
128
- // image URLs, so we also have to request it manually again
129
- this.requestUserInfo();
145
+ if (userInfo) {
146
+ this.updateAvatarWithUserInfo(userInfo);
147
+ }
130
148
  };
131
149
 
132
150
  private handleComponentLogin = () => {
@@ -164,7 +182,7 @@ export default class AvatarButton extends LightningElement {
164
182
  }
165
183
  } catch (ex) {
166
184
  // TODO: Something not directly related to auth went wrong. Unsure what to do here.
167
- console.error("Invalid response received from TBID API, aborting.");
185
+ console.error(`Could not request user info: ${ex}`);
168
186
  }
169
187
 
170
188
  this.isLoading = false;