@salesforcedevs/dx-components 0.59.0-avatar-button-5 → 0.59.0-avatar-button-8

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-5",
3
+ "version": "0.59.0-avatar-button-8",
4
4
  "description": "DX Lightning web components",
5
5
  "license": "MIT",
6
6
  "engines": {
@@ -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,9 +40,14 @@ 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
  }
@@ -59,16 +73,16 @@ export default class AvatarButton extends LightningElement {
59
73
  this.isLoading = false;
60
74
  this.updateAvatarWithUserInfo({}); // clear old info
61
75
 
62
- fetch(`${TBID_API_LOGOUT_URL}?isSsoLogout=${isSsoLogout}`); // no need to await this
63
- };
64
-
65
- // This will only be called for "seamless SSO" login by the embedded login widget (SFIDWidget) on the website, if it exists.
66
- private handleSsoLogin = (event: Event) => {
67
- const userInfo = (event as CustomEvent).detail;
68
- if (userInfo) {
69
- this.updateAvatarWithUserInfo(userInfo);
70
- this._didReceiveUserInfo = true;
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();
71
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
72
86
  };
73
87
 
74
88
  // This will only be called for "seamless SSO" login by the embedded login widget (SFIDWidget) on the website, if it exists.
@@ -76,6 +90,46 @@ export default class AvatarButton extends LightningElement {
76
90
 
77
91
  private handleComponentLogout = this.handleLogout.bind(this, false);
78
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: JSON.stringify({
107
+ token
108
+ })
109
+ });
110
+ } catch (ex) {
111
+ // If the request fails for some reason, fall back to whatever we've got
112
+ this.updateAvatarWithUserInfo(this.userInfo);
113
+ console.error(ex);
114
+ return;
115
+ }
116
+ }
117
+
118
+ // Unfortunately, the user info provided by the SFIDWidget on SSO login does not contain
119
+ // image URLs, so we also have to request it manually again
120
+ this.requestUserInfo();
121
+ };
122
+
123
+ private handleComponentLogin = () => {
124
+ if (window.SFIDWidget) {
125
+ // If the SFIDWidget is around, defer to it for login. This will ensure that the SSO
126
+ // session is used if possible.
127
+ window.SFIDWidget.login();
128
+ } else {
129
+ window.location.href = this.loginUrl;
130
+ }
131
+ };
132
+
79
133
  private updateAvatarWithUserInfo = (userInfo: any) => {
80
134
  if (!userInfo) {
81
135
  return;
@@ -85,7 +139,7 @@ export default class AvatarButton extends LightningElement {
85
139
  this.userInfo.firstName = userInfo.given_name;
86
140
  this.userInfo.lastName = userInfo.family_name;
87
141
  this.userInfo.username = userInfo.preferred_username;
88
- // TODO: Consider displaying initials if no photo. Is there always a photo?
142
+ // TODO: Consider displaying initials if no photo. Is there always a photo even if just a default one?
89
143
  };
90
144
 
91
145
  private requestUserInfo = async () => {