26 lines
760 B
JavaScript
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);
|
|
}
|