Skip to content

Commit d67b526

Browse files
committed
add beforeleave
1 parent 9c68d9b commit d67b526

File tree

5 files changed

+115
-23
lines changed

5 files changed

+115
-23
lines changed

vue-noob-router-2/src/App.vue

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
<template>
22
<div>
3-
<router-view :isAuthenticated="isAuthenticated" :handleAuth="handleAuth" />
3+
<div v-if="loading" class="loader p-4">
4+
<div></div>
5+
</div>
6+
<router-view
7+
:createAlert="createAlert"
8+
:clearAlerts="clearAlerts"
9+
:alerts="alerts"
10+
:isAuthenticated="isAuthenticated"
11+
:handleAuth="handleAuth"
12+
/>
413
</div>
514
</template>
615

@@ -11,8 +20,22 @@ export default {
1120
data() {
1221
return {
1322
isAuthenticated: false,
23+
loading: true,
24+
alerts: null,
1425
};
1526
},
27+
watch: {
28+
alerts() {
29+
const vm = this;
30+
if (vm.alerts !== "") {
31+
setTimeout(() => {
32+
vm.alerts = "";
33+
}, 4000);
34+
} else {
35+
clearTimeout();
36+
}
37+
},
38+
},
1639
beforeCreate: async function () {
1740
const vm = this;
1841
await firebase.auth().onAuthStateChanged(function (user) {
@@ -21,9 +44,16 @@ export default {
2144
} else {
2245
console.log("User is not authenticated");
2346
}
47+
vm.loading = false;
2448
});
2549
},
2650
methods: {
51+
createAlert(ev) {
52+
this.alerts = { ...ev };
53+
},
54+
clearAlerts() {
55+
this.alerts = null;
56+
},
2757
handleAuth: async function (ev) {
2858
const vm = this;
2959
if (ev.mode === "login") {
@@ -33,7 +63,7 @@ export default {
3363
.signInWithEmailAndPassword(ev.email, ev.password);
3464
vm.isAuthenticated = true;
3565
} catch (e) {
36-
return e.message;
66+
return e;
3767
}
3868
} else if (ev.mode === "register") {
3969
try {
@@ -42,12 +72,16 @@ export default {
4272
.createUserWithEmailAndPassword(ev.email, ev.password);
4373
vm.isAuthenticated = true;
4474
} catch (e) {
45-
return e.message;
75+
return e;
4676
}
4777
} else {
4878
try {
4979
await firebase.auth().signOut();
5080
vm.isAuthenticated = false;
81+
this.alerts = {
82+
code: "Notification",
83+
message: "Successfully Signed out",
84+
};
5185
} catch (e) {
5286
return e;
5387
}
@@ -58,4 +92,31 @@ export default {
5892
</script>
5993

6094
<style>
95+
.loader {
96+
height: 100vh;
97+
width: 100vw;
98+
position: absolute;
99+
z-index: 99;
100+
display: flex;
101+
align-items: center;
102+
background: rgb(31, 29, 29);
103+
}
104+
.loader div {
105+
height: 100px;
106+
width: 100px;
107+
border: 3px solid rgb(241, 211, 211);
108+
border-bottom: 0;
109+
border-top: 0;
110+
border-radius: 50%;
111+
margin: auto;
112+
animation: loader 1s infinite linear;
113+
}
114+
@keyframes loader {
115+
from {
116+
transform: rotate(0deg);
117+
}
118+
to {
119+
transform: rotate(360deg);
120+
}
121+
}
61122
</style>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<template>
2+
<div class="container text-center">
3+
<div class="display-4 text-info">Page Not Found</div>
4+
<p class="lead">The page you are looking for does not exist</p>
5+
<hr class="my-2" />
6+
<router-link to="/" class="btn btn-sm btn-warning">Go Back</router-link>
7+
</div>
8+
</template>
9+
10+
<script>
11+
export default {
12+
name: "app404",
13+
};
14+
</script>
15+
16+
<style>
17+
</style>

vue-noob-router-2/src/components/pages/Auth.vue

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<template>
22
<div class="container p-4">
33
<h3 class="text-center">{{ mode | toUpperCase }}</h3>
4-
<div class="toast" :class="{show: error }" role="alert">
4+
<div class="toast" :class="{show: $attrs.alerts }" role="alert">
55
<div class="toast-header">
6-
<strong class="mr-auto text-danger">Error</strong>
7-
<button @click="error = ''" type="button" class="ml-2 mb-1 close">
6+
<strong class="mr-auto text-danger">{{ $attrs.alerts && $attrs.alerts.code }}</strong>
7+
<button @click="$attrs.clearAlerts()" type="button" class="ml-2 mb-1 close">
88
<span aria-hidden="true">&times;</span>
99
</button>
1010
</div>
11-
<div class="toast-body">{{ error }}</div>
11+
<div class="toast-body">{{ $attrs.alerts && $attrs.alerts.message }}</div>
1212
</div>
1313
<form @submit.prevent="handleSubmit">
1414
<div class="form-group">
@@ -52,7 +52,6 @@ export default {
5252
email: "",
5353
password: "",
5454
mode: "login",
55-
error: "",
5655
submitted: false,
5756
};
5857
},
@@ -62,16 +61,6 @@ export default {
6261
},
6362
},
6463
watch: {
65-
error() {
66-
const vm = this;
67-
if (vm.error !== "") {
68-
setTimeout(() => {
69-
vm.error = "";
70-
}, 4000);
71-
} else {
72-
clearTimeout();
73-
}
74-
},
7564
"$attrs.isAuthenticated"() {
7665
if (this.$attrs.isAuthenticated) {
7766
this.$router.push({
@@ -80,23 +69,32 @@ export default {
8069
}
8170
},
8271
},
72+
beforeMount() {
73+
if (this.$attrs.isAuthenticated) {
74+
this.$router.push({
75+
name: "Home",
76+
});
77+
}
78+
},
8379
methods: {
8480
handleSubmit: async function () {
8581
this.submitted = true;
8682
if (!this.validateEmail(this.email)) {
8783
this.submitted = false;
88-
return (this.error = "Please enter a valid Email Address");
84+
return this.$attrs.createAlert({
85+
code: "Validation Error",
86+
message: "Please enter a valid Email Address",
87+
});
8988
}
90-
const res = await this.$attrs.handleAuth({
89+
const err = await this.$attrs.handleAuth({
9190
email: this.email,
9291
password: this.password,
9392
mode: this.mode,
9493
});
95-
if (res) {
94+
if (err) {
9695
this.submitted = false;
97-
return (this.error = res);
96+
return this.$attrs.createAlert(err);
9897
}
99-
this.submitted = false;
10098
this.email = "";
10199
this.password = "";
102100
},

vue-noob-router-2/src/components/pages/Home.vue

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
@click="$attrs.handleAuth({mode: 'signout'})"
1111
class="btn btn-sm btn-warning text-dark"
1212
>Sign out</button>
13+
<button @click="$router.push('/something')" class="btn btn-sm btn-danger mx-2">Go Somewhere</button>
1314
</div>
1415
</template>
1516

@@ -26,6 +27,15 @@ export default {
2627
this.email =
2728
firebase.auth().currentUser && firebase.auth().currentUser.email;
2829
},
30+
beforeRouteLeave(to, from, next) {
31+
if (!this.$attrs.isAuthenticated) {
32+
next();
33+
} else {
34+
if (confirm("Are you sure you want to leave ?")) {
35+
next();
36+
}
37+
}
38+
},
2939
watch: {
3040
"$attrs.isAuthenticated"() {
3141
if (!this.$attrs.isAuthenticated) {

vue-noob-router-2/src/routes.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Home from '@/components/pages/Home';
22
import Auth from '@/components/pages/Auth';
3+
import NotFound from '@/components/pages/404';
34
export const routes = [
45
{
56
path: '/',
@@ -16,4 +17,9 @@ export const routes = [
1617
name: 'Auth',
1718
// redirect: '/',
1819
},
20+
{
21+
path: '*',
22+
component: NotFound,
23+
name: '404',
24+
},
1925
];

0 commit comments

Comments
 (0)