Skip to content

Commit af72f00

Browse files
committed
Added global guard and meta field
1 parent e796dc7 commit af72f00

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

src/ViewProfile.vue

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
11
<template>
22
<h1>My Profile</h1>
3-
</template>
4-
5-
<script>
6-
import { authService } from './main';
7-
8-
export default {
9-
beforeRouteEnter(to, from, next) {
10-
if (!authService.isLoggedIn) {
11-
alert("You must be logged in!");
12-
return next(false);
13-
}
14-
15-
next();
16-
}
17-
}
18-
</script>
3+
</template>

src/main.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ const router = new VueRouter({
2323
}
2424
});
2525

26+
router.beforeEach((to, from, next) => {
27+
if (to.matched.some(record => record.meta.isAuthRequired)) {
28+
if (!authService.isLoggedIn) {
29+
alert("You must be logged in!");
30+
return next(false);
31+
}
32+
}
33+
34+
next();
35+
});
36+
2637
Vue.filter('currency', function(value) {
2738
let formatter = new Intl.NumberFormat('en-US', {
2839
style: 'currency',

src/routes.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ export const routes = [
1818
{
1919
path: '/user/profile',
2020
name: 'viewProfile',
21-
component: ViewProfile
21+
component: ViewProfile,
22+
meta: {
23+
isAuthRequired: true
24+
}
2225
},
2326
{ path: '/cart', component: Cart },
2427
{ path: '*', component: { template: '<h1>Page Not Found!</h1>' } }

0 commit comments

Comments
 (0)