-
Notifications
You must be signed in to change notification settings - Fork 59
/
store.ts
85 lines (81 loc) · 2.66 KB
/
store.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
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
import { BnetOAuthRegion, OauthState, TwitchToken } from "@/store/oauth/types";
import { defineStore } from "pinia";
import AuthorizationService from "@/services/AuthorizationService";
export const useOauthStore = defineStore("oauth", {
state: (): OauthState => ({
code: "",
blizzardVerifiedBtag: "",
token: "",
twitch_token: {} as TwitchToken,
isAdmin: false,
permissions: [],
isLoadingBlizzardBtag: false,
}),
actions: {
async authorizeWithCode(code: string) {
const region = await AuthorizationService.loadAuthRegionCookie();
const bearer = await AuthorizationService.authorize(code, region);
this.SET_BEARER(bearer.jwt);
const profile = await AuthorizationService.getProfile(bearer.jwt);
if (profile) {
this.SET_PROFILE_NAME(profile.battleTag);
this.SET_IS_ADMIN(profile.isAdmin);
if (profile.isAdmin) {
this.SET_PERMISSIONS(profile.permissions);
}
await AuthorizationService.saveAuthToken(bearer);
}
},
async authorizeWithTwitch() {
const token = await AuthorizationService.authorizeWithTwitch();
this.SET_TWITCH_TOKEN(token);
},
async loadAuthCodeToState() {
const bearer = await AuthorizationService.loadAuthCookie();
this.SET_BEARER(bearer);
},
async loadBlizzardBtag(bearerToken: string) {
if (this.isLoadingBlizzardBtag) return;
this.SET_IS_LOADING_BLIZZARD_BTAG(true);
const profile = await AuthorizationService.getProfile(bearerToken);
if (profile) {
this.SET_PROFILE_NAME(profile.battleTag);
this.SET_IS_ADMIN(profile.isAdmin);
if (profile.isAdmin) {
this.SET_PERMISSIONS(profile.permissions);
}
await AuthorizationService.saveAuthToken(profile);
} else {
this.logout();
}
this.SET_IS_LOADING_BLIZZARD_BTAG(false);
},
async saveLoginRegion(region: BnetOAuthRegion) {
await AuthorizationService.saveAuthRegion(region);
},
logout() {
AuthorizationService.deleteAuthCookie();
this.SET_PROFILE_NAME("");
this.SET_IS_ADMIN(false);
this.SET_BEARER("");
},
SET_BEARER(token: string): void {
this.token = token;
},
SET_TWITCH_TOKEN(token: TwitchToken): void {
this.twitch_token = token;
},
SET_PROFILE_NAME(btag: string): void {
this.blizzardVerifiedBtag = btag;
},
SET_IS_ADMIN(isAdmin: boolean): void {
this.isAdmin = isAdmin;
},
SET_PERMISSIONS(permissions: string[]): void {
this.permissions = permissions;
},
SET_IS_LOADING_BLIZZARD_BTAG(val: boolean): void {
this.isLoadingBlizzardBtag = val;
},
},
});