-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrpt-test.js
47 lines (43 loc) · 1.26 KB
/
scrpt-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { getAuth, onAuthStateChanged } from "firebase/auth";
import { createRouter,createWebHistory } from "vue-router";
const router = createRouter ({
history: createWebHistory(),
routes: [
{ path:"/",component: () => import("../components/Home.vue")},
{ path:"/Register", component: () => import("../components/Register.vue")},
{ path:"/Sign-in", component: () => import("../components/Signin.vue")},
{
path:"/Feed",
component: () => import("../components/Feed.vue"),
meta: {
requiresAuth: true,
},
},
]
});
const getCurrentUser = () => {
return new Promise((resolve, reject) => {
const removeListener = onAuthStateChanged(
getAuth(),
(user) => {
removeListener();
resolve(user);
},
reject
);
});
};
router.beforeEach((async(to, from, next) => {
if (to.matched.some((record) => record.meta.requiresAuth)) {
const user = await getCurrentUser();
if (user) {
next();
} else {
alert("You dont have access!")
next("/Sign-in")
}
} else {
next();
}
}));
export default router;