import Modal from "@app/components/modals";
|
|
import jwtDecode from "jwt-decode";
|
|
import { MutationTree } from "vuex";
|
|
import { IState } from "./state";
|
|
|
|
/**
|
|
* All available mutation types
|
|
*/
|
|
export enum Mutation {
|
|
|
|
LockScroll = "LOCK_SCROLL",
|
|
|
|
UserForget = "USER_FORGET",
|
|
UserLoad = "USER_LOAD",
|
|
UserStore = "USER_STORE",
|
|
|
|
}
|
|
|
|
/**
|
|
* Type declarations for each mutation
|
|
*/
|
|
export type MutationsTypes<S = IState> = {
|
|
[Mutation.UserForget]: (state: S) => void,
|
|
[Mutation.UserLoad] : (state: S, token: string) => boolean,
|
|
[Mutation.UserStore] : (state: S, remember: boolean) => void,
|
|
[Mutation.LockScroll]: (state: S, lock: boolean) => void,
|
|
}
|
|
|
|
/**
|
|
* The auth mutations
|
|
*/
|
|
export const mutations: MutationsTypes & MutationTree<IState> = {
|
|
|
|
[Mutation.LockScroll](state: IState, lock: boolean) {
|
|
if (lock) {
|
|
// document.body.style.position = "fixed";
|
|
// document.body.style.top = `-${window.scrollY}px`;
|
|
document.body.style.paddingRight = `${window.innerWidth - document.body.clientWidth}px`;
|
|
document.body.style.overflow = "hidden";
|
|
} else {
|
|
// const scrollY = document.body.style.top;
|
|
// document.body.style.position = "";
|
|
// document.body.style.top = "";
|
|
document.body.style.paddingRight = "";
|
|
document.body.style.overflow = "";
|
|
// window.scrollTo(0, parseInt(scrollY || '0') * -1);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Log the user out and remove the token from storage
|
|
*/
|
|
[Mutation.UserForget](state: IState) {
|
|
state.user = null;
|
|
sessionStorage.removeItem("jwt");
|
|
localStorage.removeItem("jwt");
|
|
},
|
|
|
|
/**
|
|
* Try loading a user from the provided token
|
|
*/
|
|
[Mutation.UserLoad](state: IState, token: string) {
|
|
try {
|
|
let user: any = jwtDecode(token);
|
|
state.user = {
|
|
id : user.id,
|
|
name : user.name,
|
|
isAdmin: user.isAdmin,
|
|
token : token
|
|
}
|
|
} catch(e) {
|
|
return false;
|
|
}
|
|
return true;
|
|
},
|
|
|
|
/**
|
|
* Store the user to remember them
|
|
*/
|
|
[Mutation.UserStore](state: IState, remember: boolean) {
|
|
if (state.user == null) {
|
|
sessionStorage.removeItem("jwt");
|
|
localStorage.removeItem("jwt");
|
|
return;
|
|
}
|
|
if (remember) {
|
|
localStorage.setItem("jwt", state.user.token);
|
|
} else {
|
|
sessionStorage.setItem("jwt", state.user.token);
|
|
}
|
|
}
|
|
}
|