2019-06-22 18:52:24 +03:00
|
|
|
import { NavigationGuard } from "vue-router";
|
|
|
|
import { RootStore } from "./store";
|
|
|
|
|
2019-06-23 19:18:41 +03:00
|
|
|
const isAuthenticated = async () => {
|
|
|
|
if (RootStore.state.auth.apiKey) return true; // We have an API key -> authenticated
|
|
|
|
if (RootStore.state.auth.loadedInitialAuth) return false; // No API key and initial auth data was already loaded -> not authenticated
|
2019-06-22 18:52:24 +03:00
|
|
|
await RootStore.dispatch("auth/loadInitialAuth"); // Initial auth data wasn't loaded yet (per above check) -> load it now
|
2019-06-23 19:18:41 +03:00
|
|
|
if (RootStore.state.auth.apiKey) return true;
|
|
|
|
return false; // Still no API key -> not authenticated
|
|
|
|
};
|
|
|
|
|
|
|
|
export const authGuard: NavigationGuard = async (to, from, next) => {
|
|
|
|
if (await isAuthenticated()) return next();
|
|
|
|
next("/");
|
2019-06-22 18:52:24 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export const loginCallbackGuard: NavigationGuard = async (to, from, next) => {
|
2019-07-22 00:11:24 +03:00
|
|
|
if (to.query.apiKey) {
|
|
|
|
await RootStore.dispatch("auth/setApiKey", to.query.apiKey);
|
|
|
|
next("/dashboard");
|
|
|
|
} else {
|
|
|
|
next({
|
|
|
|
path: "/",
|
|
|
|
query: {
|
|
|
|
error: "noaccess",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2019-06-22 18:52:24 +03:00
|
|
|
};
|
2019-06-23 19:18:41 +03:00
|
|
|
|
|
|
|
export const authRedirectGuard: NavigationGuard = async (to, form, next) => {
|
|
|
|
if (await isAuthenticated()) return next("/dashboard");
|
|
|
|
window.location.href = `${process.env.API_URL}/auth/login`;
|
|
|
|
};
|