How can i set guards for all my routes? How are the best practicies? #14067
Answered
by
TobyMosque
wilsntn
asked this question in
General - Components / Directives / etc
-
I have a pinia store to handle authentication see bellow when i set the Router.beforeeach function to handle if the user is authenticated or no i always get the error in line 32 of my auth store.. i think the router guard function "Router.beforeeach" runs before my router injection in my pinia store.. because of that i cant redirect my new authentications .. is there another better way to do this auth? |
Beta Was this translation helpful? Give feedback.
Answered by
TobyMosque
Jul 27, 2022
Replies: 1 comment 3 replies
-
move the guard to a boot: https://github.com/TobyMosque/ws-auth-samples-frontend/blob/persist-after/src/boot/guard.ts#L6 import { boot } from 'quasar/wrappers';
import { useAuthStore } from 'src/stores/auth';
export default boot(({ store, router }) => {
// pass the pinia intance to your store is VERY IMPORTANT
const authStore = useAuthStore(store)
router.beforeEach((to) => {
if (!authStore.isLoggedIn && to.name !== "Login") {
return { name: "Login" }
}
})
}); |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
wilsntn
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
move the guard to a boot:
https://github.com/TobyMosque/ws-auth-samples-frontend/blob/persist-after/src/boot/guard.ts#L6