This commit is contained in:
j3d1 2023-05-16 01:10:11 +02:00
parent 06f7c515ef
commit b98c368540
4 changed files with 20 additions and 13 deletions

View file

@ -15,10 +15,10 @@ import store from '@/store';
export default {
name: 'App',
methods: {
...mapMutations(['init'])
...mapMutations(['init']),
},
beforeCreate () {
store.commit('init')
store.commit('load_local')
}
}
</script>

View file

@ -11,9 +11,9 @@ import store from './store';
import _nacl from 'js-nacl';
const app = createApp(App).use(router).use(store).use(BootstrapIconsPlugin);
const app = createApp(App).use(store).use(BootstrapIconsPlugin);
_nacl.instantiate((nacl) => {
window.nacl = nacl
app.mount('#app')
app.use(router).mount('#app')
});

View file

@ -40,6 +40,7 @@ router.beforeEach((to, from) => {
if (to.meta.requiresAuth && !store.getters.isLoggedIn) {
// this route requires auth, check if logged in
// if not, redirect to login page.
console.log("Not logged in, redirecting to login page")
return {
path: '/login',
// save the location we were at to come back later

View file

@ -7,6 +7,7 @@ import {createSignAuth, createTokenAuth, ServerSet} from "@/federation";
export default createStore({
state: {
local_loaded: false,
user: null,
token: null,
keypair: null,
@ -69,7 +70,9 @@ export default createStore({
localStorage.removeItem('keypair');
router.push('/login');
},
init(state) {
load_local(state) {
if (state.local_loaded)
return;
const remember = localStorage.getItem('remember');
const user = localStorage.getItem('user');
const token = localStorage.getItem('token');
@ -79,16 +82,9 @@ export default createStore({
this.commit('setToken', token);
if (keypair) {
this.commit('setKey', keypair)
} else {
}
router.push('/');
/*if (this.$route.query.redirect) {
router.push({path: this.$route.query.redirect});
} else {
router.push({path: '/'});
}*/
}
state.cache_loaded = true;
}
},
actions: {
@ -300,6 +296,16 @@ export default createStore({
},
getters: {
isLoggedIn(state) {
if (!state.local_loaded) {
state.remember = localStorage.getItem('remember') === 'true'
state.user = localStorage.getItem('user')
state.token = localStorage.getItem('token')
const keypair = localStorage.getItem('keypair')
if (keypair)
state.keypair = nacl.crypto_sign_keypair_from_seed(nacl.from_hex(keypair))
state.local_loaded = true
}
return state.user !== null && state.token !== null;
},
signAuth(state) {