añadido refresh y rutas protegidas

This commit is contained in:
2023-02-25 16:49:26 +01:00
parent 27add5f2bc
commit 97c17d3672
3 changed files with 35 additions and 16 deletions

View File

@@ -7,16 +7,11 @@
<div class="row">{{ listaStore.pb.authStore.token }}</div>
<div class="row">{{ listaStore.pb.authStore.model.id }}</div>
</div>
<q-btn @click="logout()">Cerrar</q-btn>
<q-btn @click="listaStore.pb.authStore.clear()">Cerrar</q-btn>
</q-page>
</template>
<script setup>
import { ref } from "vue";
import { useListaStore } from "../stores/lista.js";
const listaStore = useListaStore();
const logout = () => {
listaStore.pb.authStore.clear();
console.log(listaStore.pb.authStore.isValid);
};
</script>

View File

@@ -1,6 +1,12 @@
import { route } from 'quasar/wrappers'
import { createRouter, createMemoryHistory, createWebHistory, createWebHashHistory } from 'vue-router'
import routes from './routes'
import { route } from "quasar/wrappers";
import {
createRouter,
createMemoryHistory,
createWebHistory,
createWebHashHistory,
} from "vue-router";
import routes from "./routes";
import { useListaStore } from "../stores/lista.js";
/*
* If not building with SSR mode, you can
@@ -14,7 +20,9 @@ import routes from './routes'
export default route(function (/* { store, ssrContext } */) {
const createHistory = process.env.SERVER
? createMemoryHistory
: (process.env.VUE_ROUTER_MODE === 'history' ? createWebHistory : createWebHashHistory)
: process.env.VUE_ROUTER_MODE === "history"
? createWebHistory
: createWebHashHistory;
const Router = createRouter({
scrollBehavior: () => ({ left: 0, top: 0 }),
@@ -23,8 +31,21 @@ export default route(function (/* { store, ssrContext } */) {
// Leave this as is and make changes in quasar.conf.js instead!
// quasar.conf.js -> build -> vueRouterMode
// quasar.conf.js -> build -> publicPath
history: createHistory(process.env.VUE_ROUTER_BASE)
})
history: createHistory(process.env.VUE_ROUTER_BASE),
});
return Router
})
Router.beforeEach(async (to, from, next) => {
const listaStore = useListaStore();
if (to.meta.auth) {
//Si es una ruta protegida
if (listaStore.pb.authStore.isValid) {
return next(); //Si es protegida y el token es válido
}
return next("/login"); //Si es protegida y el token no es válido
}
next();
});
return Router;
});

View File

@@ -5,7 +5,7 @@ import PocketBase from "pocketbase";
export const useListaStore = defineStore("lista", () => {
const pb = new PocketBase("https://pocketbase.clonbg.es");
const authData = ref("");
async function login(email, password) {
function login(email, password) {
authData.value = pb
.collection("users")
.authWithPassword(email, password)
@@ -17,8 +17,11 @@ export const useListaStore = defineStore("lista", () => {
console.log("error");
});
}
async function refresh() {
const authData = await pb.collection("users").authRefresh();
}
return { pb, authData, login };
return { pb, authData, login, refresh };
});
/*