waffle/frontend/src/auth.js

27 lines
760 B
JavaScript
Raw Normal View History

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
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)
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() {
removeItem("auth:token");
2022-04-27 22:03:51 +03:00
gateway.close();
gateway.dispatch(GatewayEventType.BadAuth, -1);
}