@salesforcedevs/dx-components 0.59.0-avatar-button-4 → 0.59.0-avatar-button-7

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-4",
3
+ "version": "0.59.0-avatar-button-7",
4
4
  "description": "DX Lightning web components",
5
5
  "license": "MIT",
6
6
  "engines": {
@@ -21,7 +21,11 @@
21
21
  <a href="#" class="inline-link">Switch account</a>
22
22
  &nbsp;&nbsp;|&nbsp;&nbsp;
23
23
  -->
24
- <a href="#" onclick={handleLogout} class="inline-link">
24
+ <a
25
+ href="#"
26
+ onclick={handleComponentLogout}
27
+ class="inline-link"
28
+ >
25
29
  Logout
26
30
  </a>
27
31
  </div>
@@ -5,9 +5,18 @@ const TBID_SETTINGS_URL = `${TBID_BASE_URL}/settings`;
5
5
  const TBID_PROFILE_URL = `${TBID_BASE_URL}/id`;
6
6
  const TBID_API_BASE_URL = "https://development.developer.salesforce.com/tbid";
7
7
  const TBID_API_LOGOUT_URL = `${TBID_API_BASE_URL}/logout`;
8
- const TBID_API_USERINFO_URL = `https://development.developer.salesforce.com/tbid/userinfo`;
8
+ const TBID_API_USERINFO_URL = `${TBID_API_BASE_URL}/userinfo`;
9
9
  const TBID_API_LOGIN_URL = `${TBID_API_BASE_URL}/dologin`;
10
- // const TBID_LOGOUT_URL = `${TBID_BASE_URL}/services/auth/idp/oidc/logout`;
10
+ const TBID_API_TOKEN_URL = `${TBID_API_BASE_URL}/token`;
11
+
12
+ declare global {
13
+ interface Window {
14
+ SFIDWidget?: {
15
+ logout(): void;
16
+ login(): void;
17
+ };
18
+ }
19
+ }
11
20
 
12
21
  export interface UserInfo {
13
22
  avatarImgSrc?: string;
@@ -31,16 +40,21 @@ export default class AvatarButton extends LightningElement {
31
40
  private isLoading = false;
32
41
  private settingsUrl = TBID_SETTINGS_URL;
33
42
  private profileUrl = TBID_PROFILE_URL;
34
- private loginUrl = TBID_API_LOGIN_URL;
35
43
  private _didReceiveUserInfo = false;
36
44
 
45
+ private get loginUrl() {
46
+ return `${TBID_API_LOGIN_URL}?startURL=${window.encodeURIComponent(
47
+ window.location.href
48
+ )}`;
49
+ }
50
+
37
51
  private get isLoggedIn() {
38
52
  return this._didReceiveUserInfo;
39
53
  }
40
54
 
41
55
  connectedCallback() {
42
56
  window.addEventListener("tbid-login", this.handleSsoLogin);
43
- window.addEventListener("tbid-logout", this.handleLogout);
57
+ window.addEventListener("tbid-logout", this.handleSsoLogout);
44
58
 
45
59
  // Whenever this component is added to the DOM, immediately request user info; if the user is logged in
46
60
  // and we receive the info, this component will display the avatar UI; otherwise, it will display the login
@@ -50,29 +64,71 @@ export default class AvatarButton extends LightningElement {
50
64
 
51
65
  disconnectedCallback() {
52
66
  window.removeEventListener("tbid-login", this.handleSsoLogin);
53
- window.removeEventListener("tbid-logout", this.handleLogout);
54
- }
55
-
56
- // This will only be called for "seamless SSO" login by the embedded login widget (SFWidget) on the website, if it exists.
57
- private handleSsoLogin(event: Event) {
58
- const userInfo = (event as CustomEvent).detail;
59
- if (userInfo) {
60
- this.updateAvatarWithUserInfo(userInfo);
61
- this._didReceiveUserInfo = true;
62
- }
67
+ window.removeEventListener("tbid-logout", this.handleSsoLogout);
63
68
  }
64
69
 
65
- // This can be called *either* by the embedded login widget (SFWidget) on the website, if it exists,
66
- // in the event of "seamless SSO" logout, *or* by a user clicking logout on this component.
67
- private async handleLogout() {
70
+ // This handles logout from within this component, rather than from SSO via the SFIDWidget.
71
+ private handleLogout = async (isSsoLogout: boolean) => {
68
72
  this._didReceiveUserInfo = false;
69
73
  this.isLoading = false;
70
74
  this.updateAvatarWithUserInfo({}); // clear old info
71
75
 
72
- fetch(TBID_API_LOGOUT_URL); // no need to await this
73
- }
76
+ if (!isSsoLogout && window.SFIDWidget) {
77
+ // If the SFIDWidget is around and logout was not *already* triggered by SSO logout,
78
+ // defer to the SFIDWidget. This will ensure that the SSO session is logged out as
79
+ // well.
80
+ window.SFIDWidget.logout();
81
+ }
82
+
83
+ // Always clear the session token; if not SSO logout, this will also revoke the token with
84
+ // TBID; if SSO logout, that step is already taken care of
85
+ fetch(`${TBID_API_LOGOUT_URL}?isSsoLogout=${isSsoLogout}`); // no need to await this
86
+ };
87
+
88
+ // This will only be called for "seamless SSO" login by the embedded login widget (SFIDWidget) on the website, if it exists.
89
+ private handleSsoLogout = this.handleLogout.bind(this, true);
90
+
91
+ private handleComponentLogout = this.handleLogout.bind(this, false);
92
+
93
+ // This will only be called for "seamless SSO" login by the embedded login widget (SFIDWidget) on the website, if it exists.
94
+ private handleSsoLogin = async (event: Event) => {
95
+ const token = (event as CustomEvent).detail.userInfo?.access_token;
96
+
97
+ // `token` should always be defined if an SSO login occurs, but we check for extra safety
98
+ if (token) {
99
+ this.isLoading = true;
100
+ // If an SSO login occurred and we have an SSO access token, we want to start using
101
+ // it rather than some other non-linked access token, for the "seamless SSO"
102
+ // experience
103
+ try {
104
+ await fetch(TBID_API_TOKEN_URL, {
105
+ method: "POST",
106
+ body: token
107
+ });
108
+ } catch (ex) {
109
+ // If the request fails for some reason, fall back to whatever we've got
110
+ this.updateAvatarWithUserInfo(this.userInfo);
111
+ console.error(ex);
112
+ return;
113
+ }
114
+ }
115
+
116
+ // Unfortunately, the user info provided by the SFIDWidget on SSO login does not contain
117
+ // image URLs, so we also have to request it manually again
118
+ this.requestUserInfo();
119
+ };
120
+
121
+ private handleComponentLogin = () => {
122
+ if (window.SFIDWidget) {
123
+ // If the SFIDWidget is around, defer to it for login. This will ensure that the SSO
124
+ // session is used if possible.
125
+ window.SFIDWidget.login();
126
+ } else {
127
+ window.location.href = this.loginUrl;
128
+ }
129
+ };
74
130
 
75
- private updateAvatarWithUserInfo(userInfo: any) {
131
+ private updateAvatarWithUserInfo = (userInfo: any) => {
76
132
  if (!userInfo) {
77
133
  return;
78
134
  }
@@ -81,10 +137,10 @@ export default class AvatarButton extends LightningElement {
81
137
  this.userInfo.firstName = userInfo.given_name;
82
138
  this.userInfo.lastName = userInfo.family_name;
83
139
  this.userInfo.username = userInfo.preferred_username;
84
- // TODO: Consider displaying initials if no photo. Is there always a photo?
85
- }
140
+ // TODO: Consider displaying initials if no photo. Is there always a photo even if just a default one?
141
+ };
86
142
 
87
- private async requestUserInfo() {
143
+ private requestUserInfo = async () => {
88
144
  this.isLoading = true;
89
145
 
90
146
  try {
@@ -101,5 +157,5 @@ export default class AvatarButton extends LightningElement {
101
157
  }
102
158
 
103
159
  this.isLoading = false;
104
- }
160
+ };
105
161
  }