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-09-03 17:30:39 +03:00
|
|
|
import { overlayStore, OverlayType } from "./stores";
|
2022-04-26 22:45:40 +03:00
|
|
|
|
2022-05-05 20:39:24 +03:00
|
|
|
export function useAuthHandlers() {
|
2022-04-26 22:45:40 +03:00
|
|
|
gateway.subscribe(GatewayEventType.Ready, () => {
|
2022-09-03 17:30:39 +03:00
|
|
|
overlayStore.popType(OverlayType.Login);
|
|
|
|
overlayStore.popType(OverlayType.CreateAccount);
|
2022-04-26 22:45:40 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
gateway.subscribe(GatewayEventType.BadAuth, () => {
|
2022-09-03 17:30:39 +03:00
|
|
|
overlayStore.push(OverlayType.Login, {});
|
2022-04-26 22:45:40 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function authWithToken(token, shouldUpdate=false) {
|
|
|
|
if (shouldUpdate)
|
2022-05-05 21:30:21 +03:00
|
|
|
setItem("auth: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 21:30:21 +03:00
|
|
|
removeItem("auth:token");
|
2022-04-27 22:03:51 +03:00
|
|
|
gateway.close();
|
|
|
|
gateway.dispatch(GatewayEventType.BadAuth, -1);
|
|
|
|
}
|