Skip to content

Commit

Permalink
feat: refactor auth redirect into router as route guard
Browse files Browse the repository at this point in the history
  • Loading branch information
thraizz committed Jan 30, 2024
1 parent 83610bc commit dbd5fa3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
10 changes: 10 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createApp } from "vue";
import { createRouter, createWebHistory } from "vue-router";

import App from "./App.vue";
import { useUser } from "./components/user";
import HomeVue from "./views/Home.vue";

const app = createApp(App);
Expand Down Expand Up @@ -52,5 +53,14 @@ const router = createRouter({
},
],
});
router.beforeEach((to, from, next) => {
const { isLoggedIn } = useUser();
if (to.path == "/projects/new" && !isLoggedIn) {
next("/login");
} else {
next();
}
});

const pinia = createPinia();
app.use(router).use(pinia).mount("#app");
14 changes: 1 addition & 13 deletions src/views/NewProject.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { getStorage, ref as storageRef, uploadBytes } from "firebase/storage";
import { useField, useForm } from "vee-validate";
import { ref, watch } from "vue";
import { ref } from "vue";
import { useRouter } from "vue-router";
import { string } from "yup";
Expand Down Expand Up @@ -100,18 +100,6 @@ const router = useRouter();
const projectStore = useProjectStore();
const userStore = useUser();
if (!userStore.isLoggedIn) {
router.push("/");
}
watch(
() => userStore.isLoggedIn,
(isLoggedIn) => {
if (!isLoggedIn) {
router.push("/");
}
},
);
const onSubmit = handleSubmit(
// Success
async (values: ProjectCreationFormData) => {
Expand Down

0 comments on commit dbd5fa3

Please sign in to comment.