waffle/frontend/src/auth.js
2022-09-03 17:30:39 +03:00

26 lines
760 B
JavaScript

import gateway, { GatewayEventType } from "./gateway";
import { removeItem, setItem } from "./storage";
import { overlayStore, OverlayType } from "./stores";
export function useAuthHandlers() {
gateway.subscribe(GatewayEventType.Ready, () => {
overlayStore.popType(OverlayType.Login);
overlayStore.popType(OverlayType.CreateAccount);
});
gateway.subscribe(GatewayEventType.BadAuth, () => {
overlayStore.push(OverlayType.Login, {});
});
}
export function authWithToken(token, shouldUpdate=false) {
if (shouldUpdate)
setItem("auth:token", token);
gateway.init(token);
}
export function logOut() {
removeItem("auth:token");
gateway.close();
gateway.dispatch(GatewayEventType.BadAuth, -1);
}