@salesforcedevs/dx-components 0.59.0-avatar-button-3 → 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
|
@@ -21,7 +21,11 @@
|
|
|
21
21
|
<a href="#" class="inline-link">Switch account</a>
|
|
22
22
|
|
|
|
23
23
|
-->
|
|
24
|
-
<a
|
|
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 =
|
|
8
|
+
const TBID_API_USERINFO_URL = `${TBID_API_BASE_URL}/userinfo`;
|
|
9
9
|
const TBID_API_LOGIN_URL = `${TBID_API_BASE_URL}/dologin`;
|
|
10
|
-
|
|
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.
|
|
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,63 @@ 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.
|
|
67
|
+
window.removeEventListener("tbid-logout", this.handleSsoLogout);
|
|
54
68
|
}
|
|
55
69
|
|
|
56
|
-
// This
|
|
57
|
-
private
|
|
70
|
+
// This handles logout from within this component, rather than from SSO via the SFIDWidget.
|
|
71
|
+
private handleLogout = async (isSsoLogout: boolean) => {
|
|
72
|
+
this._didReceiveUserInfo = false;
|
|
73
|
+
this.isLoading = false;
|
|
74
|
+
this.updateAvatarWithUserInfo({}); // clear old info
|
|
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
|
|
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 = (event: Event) => {
|
|
58
95
|
const userInfo = (event as CustomEvent).detail;
|
|
59
96
|
if (userInfo) {
|
|
60
97
|
this.updateAvatarWithUserInfo(userInfo);
|
|
61
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
|
+
}
|
|
62
110
|
}
|
|
63
|
-
}
|
|
64
|
-
|
|
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() {
|
|
68
|
-
this._didReceiveUserInfo = false;
|
|
69
|
-
this.isLoading = false;
|
|
70
|
-
this.updateAvatarWithUserInfo({}); // clear old info
|
|
111
|
+
};
|
|
71
112
|
|
|
72
|
-
|
|
73
|
-
|
|
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
|
+
};
|
|
74
122
|
|
|
75
|
-
private updateAvatarWithUserInfo(userInfo: any) {
|
|
123
|
+
private updateAvatarWithUserInfo = (userInfo: any) => {
|
|
76
124
|
if (!userInfo) {
|
|
77
125
|
return;
|
|
78
126
|
}
|
|
@@ -81,10 +129,10 @@ export default class AvatarButton extends LightningElement {
|
|
|
81
129
|
this.userInfo.firstName = userInfo.given_name;
|
|
82
130
|
this.userInfo.lastName = userInfo.family_name;
|
|
83
131
|
this.userInfo.username = userInfo.preferred_username;
|
|
84
|
-
// TODO: Consider displaying initials if no photo. Is there always a photo?
|
|
85
|
-
}
|
|
132
|
+
// TODO: Consider displaying initials if no photo. Is there always a photo even if just a default one?
|
|
133
|
+
};
|
|
86
134
|
|
|
87
|
-
private async
|
|
135
|
+
private requestUserInfo = async () => {
|
|
88
136
|
this.isLoading = true;
|
|
89
137
|
|
|
90
138
|
try {
|
|
@@ -101,5 +149,5 @@ export default class AvatarButton extends LightningElement {
|
|
|
101
149
|
}
|
|
102
150
|
|
|
103
151
|
this.isLoading = false;
|
|
104
|
-
}
|
|
152
|
+
};
|
|
105
153
|
}
|