Compare commits
2 Commits
e5535f1869
...
88f36575fb
Author | SHA1 | Date | |
---|---|---|---|
88f36575fb | |||
f927cc916b |
@ -39,6 +39,7 @@
|
|||||||
},
|
},
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@odit/lfk-client-js": "^1.0.1",
|
||||||
"bwip-js": "3.4.0"
|
"bwip-js": "3.4.0"
|
||||||
},
|
},
|
||||||
"release-it": {
|
"release-it": {
|
||||||
|
7
pnpm-lock.yaml
generated
7
pnpm-lock.yaml
generated
@ -1,6 +1,9 @@
|
|||||||
lockfileVersion: '6.0'
|
lockfileVersion: '6.0'
|
||||||
|
|
||||||
dependencies:
|
dependencies:
|
||||||
|
'@odit/lfk-client-js':
|
||||||
|
specifier: ^1.0.1
|
||||||
|
version: 1.0.1
|
||||||
bwip-js:
|
bwip-js:
|
||||||
specifier: 3.4.0
|
specifier: 3.4.0
|
||||||
version: 3.4.0
|
version: 3.4.0
|
||||||
@ -492,6 +495,10 @@ packages:
|
|||||||
'@octokit/openapi-types': 16.0.0
|
'@octokit/openapi-types': 16.0.0
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
|
/@odit/lfk-client-js@1.0.1:
|
||||||
|
resolution: {integrity: sha512-eGwUW1MIh7sCzlLNRBLuBvLGGCHoOzmHovHnPqpMDn+fziIIX+fcmt40mdISucZwJDJqnOff4+eHimsHfSQO8A==}
|
||||||
|
dev: false
|
||||||
|
|
||||||
/@odit/license-exporter@0.0.12:
|
/@odit/license-exporter@0.0.12:
|
||||||
resolution: {integrity: sha512-k5KxyTOk3Qz/OzId5VNXKjYOz1C4cMVfRHbq3X0VS4BM2rRuIgabrg/lbmZXDM1ExJkdBXi9sqiQ4h7N5bVbLQ==}
|
resolution: {integrity: sha512-k5KxyTOk3Qz/OzId5VNXKjYOz1C4cMVfRHbq3X0VS4BM2rRuIgabrg/lbmZXDM1ExJkdBXi9sqiQ4h7N5bVbLQ==}
|
||||||
hasBin: true
|
hasBin: true
|
||||||
|
92
src/lib/userstore.ts
Normal file
92
src/lib/userstore.ts
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
import { AuthService, OpenAPI, type ResponseAuth } from '@odit/lfk-client-js';
|
||||||
|
import { writable } from 'svelte/store';
|
||||||
|
|
||||||
|
type UserState = {
|
||||||
|
access_token: string;
|
||||||
|
refresh_token: string;
|
||||||
|
isLoggedIn: boolean;
|
||||||
|
refreshInterval: number
|
||||||
|
};
|
||||||
|
|
||||||
|
const userStore = () => {
|
||||||
|
const state: UserState = {
|
||||||
|
access_token: '',
|
||||||
|
refresh_token: '',
|
||||||
|
isLoggedIn: false,
|
||||||
|
refreshInterval: 0
|
||||||
|
};
|
||||||
|
|
||||||
|
const { subscribe, set, update } = writable(state);
|
||||||
|
|
||||||
|
const methods = {
|
||||||
|
async login(resAuth: ResponseAuth) {
|
||||||
|
update((state: UserState) => {
|
||||||
|
if (!resAuth) {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
state.access_token = resAuth.access_token;
|
||||||
|
state.refresh_token = resAuth.refresh_token;
|
||||||
|
state.isLoggedIn = true;
|
||||||
|
state.refreshInterval = setInterval(() => {
|
||||||
|
this.refreshAuth();
|
||||||
|
}, 2 * 60000)
|
||||||
|
|
||||||
|
localStorage.setItem('userdata', JSON.stringify(state));
|
||||||
|
localStorage.setItem('access_token', state.access_token);
|
||||||
|
localStorage.setItem('refresh_token', state.refresh_token);
|
||||||
|
return state;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
async refreshAuth() {
|
||||||
|
try {
|
||||||
|
const authRes = await AuthService.authControllerRefresh({ token: state.refresh_token }) as ResponseAuth;
|
||||||
|
OpenAPI.TOKEN = authRes.access_token;
|
||||||
|
} catch {
|
||||||
|
this.logout();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
loginFromStorage() {
|
||||||
|
console.log('loginFromStorage');
|
||||||
|
const access_token = localStorage.getItem('token');
|
||||||
|
if (!access_token) {
|
||||||
|
throw new Error('Unauthorized');
|
||||||
|
}
|
||||||
|
|
||||||
|
const storagedata = localStorage.getItem('userdata');
|
||||||
|
const userdata = JSON.parse(storagedata || '{}') as UserState;
|
||||||
|
|
||||||
|
update((state: UserState) => {
|
||||||
|
state.access_token = access_token;
|
||||||
|
state.refresh_token = userdata.refresh_token;
|
||||||
|
state.isLoggedIn = true;
|
||||||
|
state.refreshInterval = setInterval(() => {
|
||||||
|
this.refreshAuth();
|
||||||
|
}, 2 * 60000);
|
||||||
|
return state;
|
||||||
|
});
|
||||||
|
|
||||||
|
this.refreshAuth
|
||||||
|
},
|
||||||
|
async logout() {
|
||||||
|
update((state: UserState) => {
|
||||||
|
state.isLoggedIn = false;
|
||||||
|
state.access_token = '';
|
||||||
|
state.refresh_token = '';
|
||||||
|
state.refreshInterval = 0;
|
||||||
|
localStorage.clear();
|
||||||
|
return state;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
subscribe,
|
||||||
|
set,
|
||||||
|
update,
|
||||||
|
state,
|
||||||
|
...methods
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default userStore();
|
Loading…
x
Reference in New Issue
Block a user