2022-04-26 22:45:40 +03:00
|
|
|
import gateway, { GatewayEventType } from "./gateway";
|
2022-05-05 16:31:10 +03:00
|
|
|
import { removeItem, setItem } from "./storage";
|
2022-04-26 22:45:40 +03:00
|
|
|
import { overlayStore } from "./stores";
|
|
|
|
|
2022-05-05 20:39:24 +03:00
|
|
|
export function useAuthHandlers() {
|
2022-04-26 22:45:40 +03:00
|
|
|
gateway.subscribe(GatewayEventType.Ready, () => {
|
|
|
|
overlayStore.close("login");
|
|
|
|
overlayStore.close("createAccount");
|
|
|
|
});
|
|
|
|
|
|
|
|
gateway.subscribe(GatewayEventType.BadAuth, () => {
|
|
|
|
overlayStore.open("login", {});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function authWithToken(token, shouldUpdate=false) {
|
|
|
|
if (shouldUpdate)
|
2022-05-05 16:31:10 +03:00
|
|
|
setItem("token", token);
|
2022-04-26 22:45:40 +03:00
|
|
|
gateway.init(token);
|
|
|
|
}
|
|
|
|
|
2022-04-27 22:03:51 +03:00
|
|
|
export function logOut() {
|
2022-05-05 16:31:10 +03:00
|
|
|
removeItem("token");
|
2022-04-27 22:03:51 +03:00
|
|
|
gateway.close();
|
|
|
|
gateway.dispatch(GatewayEventType.BadAuth, -1);
|
|
|
|
}
|