@salesforcedevs/dx-components 0.59.0-avatar-button-11 → 0.59.0-avatar-button-14

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/lwc.config.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "modules": [{ "dir": "src/modules" }],
3
3
  "expose": [
4
+ "dx/avatarButton",
4
5
  "dx/banner",
5
6
  "dx/brandThemeProvider",
6
7
  "dx/breadcrumbs",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforcedevs/dx-components",
3
- "version": "0.59.0-avatar-button-11",
3
+ "version": "0.59.0-avatar-button-14",
4
4
  "description": "DX Lightning web components",
5
5
  "license": "MIT",
6
6
  "engines": {
@@ -22,8 +22,7 @@
22
22
    |  
23
23
  -->
24
24
  <a
25
- href="#"
26
- onclick={handleComponentLogout}
25
+ href="https://development.developer.salesforce.com/tbid/logout"
27
26
  class="inline-link"
28
27
  >
29
28
  Logout
@@ -14,6 +14,8 @@ declare global {
14
14
  SFIDWidget?: {
15
15
  logout(): void;
16
16
  login(): void;
17
+ openid_response: any;
18
+ disabled: boolean;
17
19
  };
18
20
  }
19
21
  }
@@ -53,13 +55,35 @@ export default class AvatarButton extends LightningElement {
53
55
  }
54
56
 
55
57
  connectedCallback() {
58
+ if (
59
+ new URLSearchParams(window.location.search).get("loginSuccess") ===
60
+ "true"
61
+ ) {
62
+ this.isLoading = true;
63
+ }
64
+
56
65
  window.addEventListener("tbid-login", this.handleSsoLogin);
57
66
  window.addEventListener("tbid-logout", this.handleSsoLogout);
58
67
 
59
- // Whenever this component is added to the DOM and there is not an SFIDWidget present, immediately request
60
- // user info; if the user is logged in and we receive the info, this component will display the avatar UI;
61
- // otherwise, it will display the login button.
62
- 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 {
63
87
  this.requestUserInfo();
64
88
  }
65
89
  }
@@ -70,15 +94,15 @@ export default class AvatarButton extends LightningElement {
70
94
  }
71
95
 
72
96
  // This handles logout from within this component, rather than from SSO via the SFIDWidget.
73
- private handleLogout = async (isSsoLogout: boolean) => {
97
+ private handleLogout = (isSsoLogout: boolean) => {
74
98
  this._didReceiveUserInfo = false;
75
99
  this.isLoading = false;
76
100
  this.updateAvatarWithUserInfo({}); // clear old info
77
101
 
78
- if (!isSsoLogout && window.SFIDWidget) {
79
- // If the SFIDWidget is around and logout was not *already* triggered by SSO logout,
80
- // defer to the SFIDWidget. This will ensure that the SSO session is logged out as
81
- // 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.
82
106
  window.SFIDWidget.logout();
83
107
  } else {
84
108
  // Always clear the session token; if not SSO logout, this will also revoke the token with
@@ -94,7 +118,8 @@ export default class AvatarButton extends LightningElement {
94
118
 
95
119
  // This will only be called for "seamless SSO" login by the embedded login widget (SFIDWidget) on the website, if it exists.
96
120
  private handleSsoLogin = async (event: Event) => {
97
- const token = (event as CustomEvent).detail.userInfo?.access_token;
121
+ const { userInfo } = (event as CustomEvent).detail;
122
+ const token = userInfo?.access_token;
98
123
 
99
124
  // `token` should always be defined if an SSO login occurs, but we check for extra safety
100
125
  if (token) {
@@ -113,16 +138,14 @@ export default class AvatarButton extends LightningElement {
113
138
  })
114
139
  });
115
140
  } catch (ex) {
116
- // If the request fails for some reason, fall back to whatever we've got
117
- this.updateAvatarWithUserInfo(this.userInfo);
118
- console.error(ex);
119
- return;
141
+ console.error(`Attempt to update token failed: ${ex}`);
120
142
  }
121
143
  }
122
144
 
123
- // Unfortunately, the user info provided by the SFIDWidget on SSO login does not contain
124
- // image URLs, so we also have to request it manually again
125
- this.requestUserInfo();
145
+ if (userInfo) {
146
+ this.updateAvatarWithUserInfo(userInfo);
147
+ this._didReceiveUserInfo = true;
148
+ }
126
149
  };
127
150
 
128
151
  private handleComponentLogin = () => {
@@ -160,7 +183,7 @@ export default class AvatarButton extends LightningElement {
160
183
  }
161
184
  } catch (ex) {
162
185
  // TODO: Something not directly related to auth went wrong. Unsure what to do here.
163
- console.error("Invalid response received from TBID API, aborting.");
186
+ console.error(`Could not request user info: ${ex}`);
164
187
  }
165
188
 
166
189
  this.isLoading = false;