-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathauth-service.ts
54 lines (45 loc) · 1.38 KB
/
auth-service.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
48
49
50
51
52
53
54
import { User, UserManager, WebStorageStateStore } from "oidc-client-ts";
export default class AuthService {
private userManager!: UserManager;
private keycloakConfig: any;
constructor() {
const rtConfig = useRuntimeConfig();
this.keycloakConfig = {
...rtConfig.public,
};
this.initializedOidc();
}
private initializedOidc() {
try {
const { realm, keycloakIssuer, clientId } = this.keycloakConfig;
const settings = {
authority: `${keycloakIssuer}/auth/realms/${realm}`,
client_id: clientId,
redirect_uri: `${window.location.origin}/auth`,
silent_redirect_uri: `${window.location.origin}/silent-refresh`,
post_logout_redirect_uri: `${window.location.origin}`,
response_type: "code",
userStore: new WebStorageStateStore(),
loadUserInfo: true,
};
this.userManager = new UserManager(settings);
} catch (error) {
console.error(error);
}
}
public signInRedirect() {
return this.userManager.signinRedirect();
}
public signInCallback() {
return this.userManager.signinCallback();
}
public renewToken(): Promise<void> {
return this.userManager.signinSilentCallback();
}
public logout(): Promise<void> {
return this.userManager.signoutRedirect();
}
public getUser(): Promise<User | null> {
return this.userManager.getUser();
}
}