2021-08-20 03:38:16 -07:00
|
|
|
import { get, set } from '@client/scripts/idb-proxy';
|
2020-12-18 17:55:52 -08:00
|
|
|
import { reactive } from 'vue';
|
2021-03-23 01:30:14 -07:00
|
|
|
import { apiUrl } from '@client/config';
|
|
|
|
import { waiting } from '@client/os';
|
2021-08-20 03:38:16 -07:00
|
|
|
import { unisonReload, reloadChannel } from '@client/scripts/unison-reload';
|
2020-12-18 17:55:52 -08:00
|
|
|
|
|
|
|
// TODO: 他のタブと永続化されたstateを同期
|
|
|
|
|
|
|
|
type Account = {
|
|
|
|
id: string;
|
|
|
|
token: string;
|
2021-07-16 23:43:07 -07:00
|
|
|
isModerator: boolean;
|
|
|
|
isAdmin: boolean;
|
2020-12-18 17:55:52 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
const data = localStorage.getItem('account');
|
|
|
|
|
|
|
|
// TODO: 外部からはreadonlyに
|
|
|
|
export const $i = data ? reactive(JSON.parse(data) as Account) : null;
|
|
|
|
|
2021-08-20 03:38:16 -07:00
|
|
|
export async function signout() {
|
|
|
|
waiting();
|
2020-12-18 17:55:52 -08:00
|
|
|
localStorage.removeItem('account');
|
2021-08-20 03:38:16 -07:00
|
|
|
|
|
|
|
//#region Remove account
|
|
|
|
const accounts = await getAccounts();
|
|
|
|
accounts.splice(accounts.findIndex(x => x.id === $i.id), 1);
|
|
|
|
set('accounts', accounts);
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region Remove push notification registration
|
2021-08-20 19:51:46 -07:00
|
|
|
try {
|
|
|
|
const registration = await navigator.serviceWorker.ready;
|
|
|
|
const push = await registration.pushManager.getSubscription();
|
|
|
|
if (!push) return;
|
|
|
|
await fetch(`${apiUrl}/sw/unregister`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
i: $i.token,
|
|
|
|
endpoint: push.endpoint,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
} catch (e) {}
|
2021-08-20 03:38:16 -07:00
|
|
|
//#endregion
|
|
|
|
|
2020-12-18 17:55:52 -08:00
|
|
|
document.cookie = `igi=; path=/`;
|
2021-08-20 03:38:16 -07:00
|
|
|
|
|
|
|
if (accounts.length > 0) login(accounts[0].token);
|
|
|
|
else unisonReload();
|
2020-12-18 17:55:52 -08:00
|
|
|
}
|
|
|
|
|
2021-08-20 03:38:16 -07:00
|
|
|
export async function getAccounts(): Promise<{ id: Account['id'], token: Account['token'] }[]> {
|
|
|
|
return (await get('accounts')) || [];
|
2020-12-18 17:55:52 -08:00
|
|
|
}
|
|
|
|
|
2021-08-20 03:38:16 -07:00
|
|
|
export async function addAccount(id: Account['id'], token: Account['token']) {
|
|
|
|
const accounts = await getAccounts();
|
2020-12-18 17:55:52 -08:00
|
|
|
if (!accounts.some(x => x.id === id)) {
|
2021-08-20 03:38:16 -07:00
|
|
|
await set('accounts', accounts.concat([{ id, token }]));
|
2020-12-18 17:55:52 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function fetchAccount(token): Promise<Account> {
|
|
|
|
return new Promise((done, fail) => {
|
|
|
|
// Fetch user
|
|
|
|
fetch(`${apiUrl}/i`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
i: token
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
// When failed to authenticate user
|
2021-08-20 03:38:16 -07:00
|
|
|
if (res.status !== 200 && res.status < 500) {
|
2020-12-18 17:55:52 -08:00
|
|
|
return signout();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse response
|
|
|
|
res.json().then(i => {
|
|
|
|
i.token = token;
|
|
|
|
done(i);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(fail);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function updateAccount(data) {
|
|
|
|
for (const [key, value] of Object.entries(data)) {
|
|
|
|
$i[key] = value;
|
|
|
|
}
|
2021-02-10 23:06:19 -08:00
|
|
|
localStorage.setItem('account', JSON.stringify($i));
|
2020-12-18 17:55:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function refreshAccount() {
|
2021-08-20 03:38:16 -07:00
|
|
|
return fetchAccount($i.token).then(updateAccount);
|
2020-12-18 17:55:52 -08:00
|
|
|
}
|
|
|
|
|
2021-08-20 03:38:16 -07:00
|
|
|
export async function login(token: Account['token'], redirect?: string) {
|
2020-12-18 17:55:52 -08:00
|
|
|
waiting();
|
|
|
|
if (_DEV_) console.log('logging as token ', token);
|
|
|
|
const me = await fetchAccount(token);
|
|
|
|
localStorage.setItem('account', JSON.stringify(me));
|
2021-08-20 03:38:16 -07:00
|
|
|
await addAccount(me.id, token);
|
|
|
|
|
|
|
|
if (redirect) {
|
|
|
|
reloadChannel.postMessage('reload');
|
|
|
|
location.href = redirect;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-17 04:36:56 -08:00
|
|
|
unisonReload();
|
2020-12-18 17:55:52 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// このファイルに書きたくないけどここに書かないと何故かVeturが認識しない
|
|
|
|
declare module '@vue/runtime-core' {
|
|
|
|
interface ComponentCustomProperties {
|
|
|
|
$i: typeof $i;
|
|
|
|
}
|
|
|
|
}
|