2022-09-03 09:17:57 +00:00
|
|
|
import { UserApi } from '~~/lib/api/user';
|
|
|
|
import { defineStore } from 'pinia';
|
|
|
|
import { useLocalStorage } from '@vueuse/core';
|
2022-09-01 22:32:03 +00:00
|
|
|
|
2022-09-03 09:17:57 +00:00
|
|
|
export const useAuthStore = defineStore('auth', {
|
2022-09-01 22:32:03 +00:00
|
|
|
state: () => ({
|
2022-09-03 09:17:57 +00:00
|
|
|
token: useLocalStorage('pinia/auth/token', ''),
|
|
|
|
expires: useLocalStorage('pinia/auth/expires', ''),
|
2022-09-01 22:32:03 +00:00
|
|
|
}),
|
|
|
|
getters: {
|
2022-09-03 09:17:57 +00:00
|
|
|
isTokenExpired: state => {
|
2022-09-01 22:32:03 +00:00
|
|
|
if (!state.expires) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-03 09:17:57 +00:00
|
|
|
if (typeof state.expires === 'string') {
|
2022-09-01 22:32:03 +00:00
|
|
|
return new Date(state.expires) < new Date();
|
|
|
|
}
|
|
|
|
|
|
|
|
return state.expires < new Date();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
async logout(api: UserApi) {
|
|
|
|
const result = await api.logout();
|
|
|
|
|
|
|
|
if (result.error) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-09-03 09:17:57 +00:00
|
|
|
this.token = '';
|
|
|
|
this.expires = '';
|
2022-09-01 22:32:03 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|