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

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-6",
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,22 +73,52 @@ export default class AvatarButton extends LightningElement {
59
73
  this.isLoading = false;
60
74
  this.updateAvatarWithUserInfo({}); // clear old info
61
75
 
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
62
85
  fetch(`${TBID_API_LOGOUT_URL}?isSsoLogout=${isSsoLogout}`); // no need to await this
63
86
  };
64
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
+
65
93
  // This will only be called for "seamless SSO" login by the embedded login widget (SFIDWidget) on the website, if it exists.
66
94
  private handleSsoLogin = (event: Event) => {
67
95
  const userInfo = (event as CustomEvent).detail;
68
96
  if (userInfo) {
69
97
  this.updateAvatarWithUserInfo(userInfo);
70
98
  this._didReceiveUserInfo = true;
99
+ // userInfo.access_token should always exist if an SSO login occurs, but we just check
100
+ // for extra safety:
101
+ if (userInfo.access_token) {
102
+ // If an SSO login occurred and we have an SSO access token, we want to start using
103
+ // it rather than some other non-linked access token, for the "seamless SSO"
104
+ // experience.
105
+ fetch(TBID_API_TOKEN_URL, {
106
+ method: "POST",
107
+ body: userInfo.access_token
108
+ });
109
+ }
71
110
  }
72
111
  };
73
112
 
74
- // This will only be called for "seamless SSO" login by the embedded login widget (SFIDWidget) on the website, if it exists.
75
- private handleSsoLogout = this.handleLogout.bind(this, true);
76
-
77
- private handleComponentLogout = this.handleLogout.bind(this, false);
113
+ private handleComponentLogin = () => {
114
+ if (window.SFIDWidget) {
115
+ // If the SFIDWidget is around, defer to it for login. This will ensure that the SSO
116
+ // session is used if possible.
117
+ window.SFIDWidget.login();
118
+ } else {
119
+ window.location.href = this.loginUrl;
120
+ }
121
+ };
78
122
 
79
123
  private updateAvatarWithUserInfo = (userInfo: any) => {
80
124
  if (!userInfo) {
@@ -85,7 +129,7 @@ export default class AvatarButton extends LightningElement {
85
129
  this.userInfo.firstName = userInfo.given_name;
86
130
  this.userInfo.lastName = userInfo.family_name;
87
131
  this.userInfo.username = userInfo.preferred_username;
88
- // TODO: Consider displaying initials if no photo. Is there always a photo?
132
+ // TODO: Consider displaying initials if no photo. Is there always a photo even if just a default one?
89
133
  };
90
134
 
91
135
  private requestUserInfo = async () => {