Fixed login and login-guard, redirect when offline
This commit is contained in:
parent
0922d468d9
commit
bc3c15e3bc
|
@ -6,7 +6,7 @@ import { useMainStore } from 'src/store';
|
||||||
|
|
||||||
const api = axios.create();
|
const api = axios.create();
|
||||||
|
|
||||||
export default boot(({ store, router }) => {
|
export default boot(({ router }) => {
|
||||||
api.defaults.baseURL = LocalStorage.getItem<string>('baseURL') || config.baseURL;
|
api.defaults.baseURL = LocalStorage.getItem<string>('baseURL') || config.baseURL;
|
||||||
|
|
||||||
/***
|
/***
|
||||||
|
@ -28,6 +28,8 @@ export default boot(({ store, router }) => {
|
||||||
api.interceptors.response.use(
|
api.interceptors.response.use(
|
||||||
(response) => response,
|
(response) => response,
|
||||||
(error) => {
|
(error) => {
|
||||||
|
const store = useMainStore();
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
const e = <AxiosError>error;
|
const e = <AxiosError>error;
|
||||||
if (
|
if (
|
||||||
|
@ -43,8 +45,13 @@ export default boot(({ store, router }) => {
|
||||||
query: { redirect: next },
|
query: { redirect: next },
|
||||||
});
|
});
|
||||||
} else if (e.response && e.response.status == 401) {
|
} else if (e.response && e.response.status == 401) {
|
||||||
if (router.currentRoute.value.name !== 'login')
|
if (router.currentRoute.value.name !== 'login') {
|
||||||
return store.dispatch('session/clearCurrent');
|
store.logout();
|
||||||
|
return router.push({
|
||||||
|
name: 'login',
|
||||||
|
query: { logout: '.', redirect: router.currentRoute.value.fullPath },
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
|
|
|
@ -4,21 +4,17 @@ import { hasPermissions } from 'src/utils/permission';
|
||||||
import { RouteRecord } from 'vue-router';
|
import { RouteRecord } from 'vue-router';
|
||||||
|
|
||||||
export default boot(({ router }) => {
|
export default boot(({ router }) => {
|
||||||
router.beforeEach((to, from, next) => {
|
router.beforeResolve((to, from, next) => {
|
||||||
console.log(`from ${from.fullPath} to ${to.fullPath}`);
|
|
||||||
const store = useMainStore();
|
const store = useMainStore();
|
||||||
|
|
||||||
if (to.path == from.path) {
|
if (to.path == from.path) return next();
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (to.path.startsWith('/main')) {
|
if (to.path.startsWith('/main')) {
|
||||||
// Secured area (LOGIN REQUIRED)
|
// Secured area (LOGIN REQUIRED)
|
||||||
// Check login is ok
|
// Check login is ok
|
||||||
if (!store.session || store.session.expires <= new Date()) {
|
if (!store.session || store.session.expires <= new Date()) {
|
||||||
console.log('Nope, logout');
|
|
||||||
void store.logout();
|
void store.logout();
|
||||||
return;
|
return next({ name: 'login', query: { redirect: to.fullPath } });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if special permissions are required
|
// Check if special permissions are required
|
||||||
|
@ -26,28 +22,21 @@ export default boot(({ router }) => {
|
||||||
to.matched.every((record: RouteRecord) => {
|
to.matched.every((record: RouteRecord) => {
|
||||||
if (!('meta' in record) || !('permissions' in record.meta)) return true;
|
if (!('meta' in record) || !('permissions' in record.meta)) return true;
|
||||||
if ((<{ permissions: FG.Permission[] }>record.meta).permissions) {
|
if ((<{ permissions: FG.Permission[] }>record.meta).permissions) {
|
||||||
console.log(record.meta);
|
return hasPermissions((<{ permissions: FG.Permission[] }>record.meta).permissions);
|
||||||
const h = hasPermissions((<{ permissions: FG.Permission[] }>record.meta).permissions);
|
|
||||||
console.log(h);
|
|
||||||
return h;
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
) {
|
) {
|
||||||
console.log('ok next');
|
return next();
|
||||||
next();
|
|
||||||
} else {
|
} else {
|
||||||
console.log('Back loggin');
|
return next({ name: 'login', query: { redirect: to.fullPath } });
|
||||||
next({ name: 'login', query: { redirect: to.fullPath } });
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (to.name == 'login' && store.user && !to.params['logout']) {
|
if (to.name == 'login' && store.user && !to.params['logout']) {
|
||||||
// Called login while already logged in
|
// Called login while already logged in
|
||||||
console.log('Ok next');
|
return next({ name: 'dashboard' });
|
||||||
void next({ name: 'dashboard' });
|
|
||||||
} else {
|
} else {
|
||||||
// We are on the non secured area
|
// We are on the non secured area
|
||||||
console.log('Ok non sec');
|
return next();
|
||||||
next();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -105,7 +105,7 @@ import { Screen } from 'quasar';
|
||||||
import { defineComponent, ref, inject, computed } from 'vue';
|
import { defineComponent, ref, inject, computed } from 'vue';
|
||||||
import { useMainStore } from 'src/store';
|
import { useMainStore } from 'src/store';
|
||||||
import { FG_Plugin } from 'src/plugins';
|
import { FG_Plugin } from 'src/plugins';
|
||||||
import { useRoute } from 'vue-router';
|
import { useRoute, useRouter } from 'vue-router';
|
||||||
|
|
||||||
const links = [
|
const links = [
|
||||||
{
|
{
|
||||||
|
@ -136,6 +136,7 @@ export default defineComponent({
|
||||||
components: { EssentialLink, ShortCutLink },
|
components: { EssentialLink, ShortCutLink },
|
||||||
setup() {
|
setup() {
|
||||||
const route = useRoute();
|
const route = useRoute();
|
||||||
|
const router = useRouter();
|
||||||
const mainStore = useMainStore();
|
const mainStore = useMainStore();
|
||||||
const flaschengeist = inject<FG_Plugin.Flaschengeist>('flaschengeist');
|
const flaschengeist = inject<FG_Plugin.Flaschengeist>('flaschengeist');
|
||||||
const leftDrawer = ref(false);
|
const leftDrawer = ref(false);
|
||||||
|
@ -171,6 +172,7 @@ export default defineComponent({
|
||||||
});
|
});
|
||||||
|
|
||||||
function logout() {
|
function logout() {
|
||||||
|
void router.push({ name: 'login', params: { logout: 'logout' } });
|
||||||
void mainStore.logout();
|
void mainStore.logout();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,8 +45,10 @@ export default defineComponent({
|
||||||
const reload = ref(10);
|
const reload = ref(10);
|
||||||
const ival = setInterval(() => {
|
const ival = setInterval(() => {
|
||||||
reload.value -= 1;
|
reload.value -= 1;
|
||||||
if (reload.value === 0) {
|
if (reload.value <= 0) {
|
||||||
const path = router.currentRoute.value.query.redirect;
|
const path = router.currentRoute.value.query.redirect;
|
||||||
|
console.log('Offline: ');
|
||||||
|
console.log(path);
|
||||||
void router.replace(path ? { path: <string>path } : { name: 'login' });
|
void router.replace(path ? { path: <string>path } : { name: 'login' });
|
||||||
}
|
}
|
||||||
}, 1000);
|
}, 1000);
|
||||||
|
|
|
@ -141,7 +141,6 @@ export default defineComponent({
|
||||||
}
|
}
|
||||||
|
|
||||||
async function save() {
|
async function save() {
|
||||||
console.log(lifetime.value);
|
|
||||||
isEdit.value = false;
|
isEdit.value = false;
|
||||||
await sessionStore.updateSession(lifetime.value, props.modelValue.token);
|
await sessionStore.updateSession(lifetime.value, props.modelValue.token);
|
||||||
emit(
|
emit(
|
||||||
|
|
|
@ -20,7 +20,7 @@ export default store(function (/* { ssrContext } */) {
|
||||||
import { defineStore } from 'pinia';
|
import { defineStore } from 'pinia';
|
||||||
import { api } from 'src/boot/axios';
|
import { api } from 'src/boot/axios';
|
||||||
import { AxiosResponse } from 'axios';
|
import { AxiosResponse } from 'axios';
|
||||||
import { LocalStorage } from 'quasar';
|
import { LocalStorage, SessionStorage } from 'quasar';
|
||||||
import { useUserStore, useSessionStore } from 'src/plugins/user/store';
|
import { useUserStore, useSessionStore } from 'src/plugins/user/store';
|
||||||
|
|
||||||
function loadCurrentSession() {
|
function loadCurrentSession() {
|
||||||
|
@ -29,12 +29,18 @@ function loadCurrentSession() {
|
||||||
return session || undefined;
|
return session || undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function loadUser() {
|
||||||
|
const user = SessionStorage.getItem<FG.User>('user');
|
||||||
|
if (user && user.birthday) user.birthday = new Date(user.birthday);
|
||||||
|
return user || undefined;
|
||||||
|
}
|
||||||
|
|
||||||
export const useMainStore = defineStore({
|
export const useMainStore = defineStore({
|
||||||
id: 'main',
|
id: 'main',
|
||||||
|
|
||||||
state: () => ({
|
state: () => ({
|
||||||
session: loadCurrentSession(),
|
session: loadCurrentSession(),
|
||||||
user: undefined as FG.User | undefined,
|
user: loadUser(),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
getters: {
|
getters: {
|
||||||
|
@ -62,7 +68,10 @@ export const useMainStore = defineStore({
|
||||||
this.session = this.session;
|
this.session = this.session;
|
||||||
const userStore = useUserStore();
|
const userStore = useUserStore();
|
||||||
const user = await userStore.getUser(this.session.userid);
|
const user = await userStore.getUser(this.session.userid);
|
||||||
if (user) this.user = user;
|
if (user) {
|
||||||
|
this.user = user;
|
||||||
|
SessionStorage.set('user', user);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -82,11 +91,13 @@ export const useMainStore = defineStore({
|
||||||
logout() {
|
logout() {
|
||||||
if (!this.session) return false;
|
if (!this.session) return false;
|
||||||
|
|
||||||
void api.delete(`/auth/${this.session.token}`);
|
void api.delete(`/auth/${this.session.token}`).finally(() => {
|
||||||
this.$patch({
|
this.$patch({
|
||||||
session: undefined,
|
session: undefined,
|
||||||
user: undefined,
|
user: undefined,
|
||||||
});
|
});
|
||||||
|
SessionStorage.clear();
|
||||||
|
});
|
||||||
LocalStorage.remove('session');
|
LocalStorage.remove('session');
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue