-
Notifications
You must be signed in to change notification settings - Fork 731
Expand file tree
/
Copy pathauth.actions.ts
More file actions
176 lines (163 loc) · 4.93 KB
/
auth.actions.ts
File metadata and controls
176 lines (163 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { Auth0Service } from '@/modules/auth/services/auth0.service';
import { AuthApiService } from '@/modules/auth/services/auth.api.service';
import { AuthService } from '@/modules/auth/services/auth.service';
import { User } from '@/modules/auth/types/User.type';
import Errors from '@/shared/error/errors';
import { disconnectSocket, connectSocket, isSocketConnected } from '@/modules/auth/auth.socket';
import identify from '@/shared/modules/monitoring/identify';
import { watch } from 'vue';
import config from '@/config';
import { setRumUser } from '@/utils/datadog/rum';
import useSessionTracking from '@/shared/modules/monitoring/useSessionTracking';
import { boot as bootIntercom, shutdown as shutdownIntercom } from '@/utils/intercom';
export default {
init() {
Auth0Service.isAuthenticated()
.then((isAuthenticated: boolean) => {
if (!isAuthenticated) {
this.handleLocalAuth()
.catch(() => this.silentLogin());
} else {
this.silentLogin();
}
});
},
setLfxHeader() {
const lfxHeader = document.getElementById('lfx-header');
if (!lfxHeader || lfxHeader.authuser) {
return;
}
lfxHeader.docslink = 'https://docs.linuxfoundation.org/lfx/community-management';
lfxHeader.supportlink = 'https://jira.linuxfoundation.org/plugins/servlet/desk/portal/4?requestGroup=54';
Auth0Service.getUser().then((user) => {
if (user) {
setRumUser(user);
lfxHeader.authuser = user;
const intercomJwt = user[config.intercom.auth0IntercomClaim];
const userId = user[config.intercom.auth0UsernameClaim];
if (userId && intercomJwt) {
bootIntercom({
user_id: userId,
name: user.name,
email: user.email,
intercom_user_jwt: intercomJwt,
}).catch((error: any) => {
console.error('Intercom: Boot failed', error);
});
}
}
});
},
silentLogin() {
Auth0Service.getTokenSilently()
.then(() => Auth0Service.authData())
.then((token) => this.authCallback(token))
.catch((error) => {
if (['login_required', 'consent_required', 'missing_refresh_token'].includes(error.error)) {
const appState: any = {};
if (window.location.href) {
appState.returnTo = window.location.href.replace(window.location.origin, '');
}
return this.signin({ appState });
}
return Promise.reject();
});
},
handleLocalAuth() {
if (['production', 'staging'].includes(config.env)) {
return Promise.reject();
}
const storedToken = AuthService.getToken();
const params = new URLSearchParams(window.location.search);
const myJwt = params.get('my-jwt');
const localToken = storedToken || myJwt;
if (localToken) {
return this.getUser(localToken);
}
return Promise.reject();
},
ensureTrackingSession(): Promise<void> {
if (!this.loaded) {
return new Promise((resolve) => {
const stopWatcher = watch(
() => this.loaded,
async (loaded) => {
if (!loaded) {
const { startSession, attachListeners } = useSessionTracking();
this.loaded = true;
await startSession();
attachListeners();
}
resolve();
stopWatcher();
},
{
immediate: true,
},
);
});
}
return Promise.resolve();
},
ensureLoaded(): Promise<void> {
if (!this.user) {
return new Promise((resolve) => {
const stopWatcher = watch(
() => this.user,
async (newUser) => {
if (newUser) {
this.setLfxHeader();
resolve();
stopWatcher();
}
},
{
immediate: true,
},
);
});
}
// Both are already loaded
return Promise.resolve();
},
getUser(token?: string) {
const t = token || AuthService.getToken();
if (!t) {
return Promise.reject();
}
if (!isSocketConnected()) {
connectSocket(t);
}
AuthService.setToken(t);
return AuthApiService.fetchMe()
.then((user) => {
this.user = user;
identify(user);
this.setLfxHeader();
return Promise.resolve(user);
})
.catch((error) => {
AuthService.logout();
Auth0Service.logout();
Errors.handle(error);
return Promise.reject(error);
});
},
authCallback(token: string | null): Promise<User> {
if (!token) {
return Promise.reject();
}
return AuthApiService.ssoGetToken(token)
.then((token) => this.getUser(token))
.catch(() => this.logout());
},
signin(params?: any) {
return Auth0Service.loginWithRedirect(params);
},
logout() {
disconnectSocket();
shutdownIntercom();
this.user = null;
return Auth0Service.logout();
},
};