Skip to content

Commit 11eb36a

Browse files
committed
Added route guard
1 parent 9f00774 commit 11eb36a

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

src/App.vue

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222

2323
<div class="nav navbar-nav navbar-right">
2424
<div class="stats">{{ cart.items.length }} <template v-if="cart.items.length == 1">item</template><template v-else>items</template> in cart, totalling {{ cartTotal | currency }}</div>
25+
26+
<div style="margin-top: 8px;">
27+
<router-link :to="{ name: 'viewProfile' }">My Profile</router-link>
28+
&nbsp;
29+
<button v-if="auth.isLoggedIn" class="btn btn-primary" @click="auth.isLoggedIn = false">Log out</button>
30+
<button v-else class="btn btn-primary" @click="auth.isLoggedIn = true">Log in</button>
31+
</div>
2532
</div>
2633
</div>
2734
</div>
@@ -40,14 +47,16 @@
4047
<script>
4148
import { eventBus } from './main';
4249
import CartMixin from './mixins/cart';
50+
import { authService } from './main';
4351
4452
export default {
4553
mixins: [ CartMixin ],
4654
data() {
4755
return {
4856
cart: {
4957
items: []
50-
}
58+
},
59+
auth: authService
5160
};
5261
},
5362
created() {

src/ViewProfile.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
<h1>My Profile</h1>
3+
</template>

src/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Vue.filter('currency', function(value) {
3434
});
3535

3636
export const eventBus = new Vue();
37+
export const authService = { isLoggedIn: false };
3738

3839
new Vue({
3940
el: '#app',

src/routes.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import Cart from './Cart.vue';
44
import Product from './Product.vue';
55
import ProductReviews from './ProductReviews.vue';
66
import SpecialOffer from './SpecialOffer.vue';
7+
import ViewProfile from './ViewProfile.vue';
8+
import { authService } from './main';
79

810
export const routes = [
911
{ path: '', components: {
@@ -14,6 +16,19 @@ export const routes = [
1416
{ path: 'details', name: 'viewProduct', props: true, component: ViewProduct },
1517
{ path: 'reviews', name: 'productReviews', props: true, component: ProductReviews }
1618
] },
19+
{
20+
path: '/user/profile',
21+
name: 'viewProfile',
22+
component: ViewProfile,
23+
beforeEnter(to, from, next) {
24+
if (!authService.isLoggedIn) {
25+
alert("You must be logged in!");
26+
return next(false);
27+
}
28+
29+
next();
30+
}
31+
},
1732
{ path: '/cart', component: Cart },
1833
{ path: '*', component: { template: '<h1>Page Not Found!</h1>' } }
1934
];

0 commit comments

Comments
 (0)