-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyAuthProvider.ts
47 lines (39 loc) · 1.3 KB
/
MyAuthProvider.ts
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
import type { AuthProvider } from '@interopio/manager-admin-ui';
export class MyAuthProvider implements AuthProvider {
public isLoading = false;
public isAuthenticated = false;
public addTokenToRequest = true;
public addCredentialsToRequest = false;
public addUsernameToRequest = false;
public error: any = undefined;
public token: string = '';
/**
* Set to `true` as fields are only updated during the execution of the methods defined in `AuthProvider`.
*/
public disablePeriodicChangeDetection: boolean = true;
public async loginIfNeeded(): Promise<void> {
// get token query parameter
const urlParams = new URLSearchParams(window.location.search);
const token = urlParams.get('token');
if (token) {
this.token = token;
this.isAuthenticated = true;
return Promise.resolve();
}
if (!this.isAuthenticated) {
document.location.href = `http://localhost:9123/?callback=${document.location.href}`;
}
return Promise.resolve();
}
public async getAccessToken(): Promise<string | undefined> {
return this.token;
}
public async getUserInfo(): Promise<{ id?: string | undefined } | undefined> {
return {
id: this.token.substring('user:'.length),
};
}
public async logOut(): Promise<void> {
this.token = '';
}
}