waffle/frontend/src/auth.js

28 lines
869 B
JavaScript

import gateway, { GatewayEventType } from "./gateway";
import { removeItem, setItem } from "./storage";
import { overlayStore, OverlayType } from "./stores";
import { timeline } from "./timeline";
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) {
timeline.addCheckpoint("Authentication started (authWithToken)");
if (shouldUpdate)
setItem("auth:token", token);
gateway.init(token);
}
export function logOut() {
removeItem("auth:token");
gateway.close();
gateway.dispatch(GatewayEventType.BadAuth, -1);
}