Skip to content

Commit 4ef858c

Browse files
committed
[FIREBASE-DEMO] Added video 008 code
1 parent fc1b209 commit 4ef858c

File tree

8 files changed

+137
-42
lines changed

8 files changed

+137
-42
lines changed

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"core-js": "^3.6.5",
1212
"firebase": "^8.6.8",
1313
"vue": "^2.6.11",
14+
"vue-router": "^3.5.2",
1415
"vuex": "^3.6.2"
1516
},
1617
"devDependencies": {

src/App.vue

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,9 @@
11
<template>
22
<div id="app">
3-
<img alt="Vue logo" src="./assets/logo.png" />
4-
<LoginForm @login="login" v-if="!user.uid" msg="Bitte einloggen" />
5-
<div v-else>
6-
Sie sind eingeloggt <button @click="logout()">Logout</button>
7-
</div>
3+
<router-view></router-view>
84
</div>
95
</template>
106

11-
<script>
12-
import LoginForm from "./components/LoginForm.vue";
13-
import {mapState} from "vuex";
14-
15-
export default {
16-
name: "App",
17-
components: {
18-
LoginForm,
19-
},
20-
data() {
21-
return{}
22-
},
23-
24-
computed: {
25-
...mapState(["user"])
26-
},
27-
methods: {
28-
login(payload) {
29-
this.$store.dispatch("login",payload)
30-
},
31-
logout(){
32-
this.$store.dispatch("logout");
33-
},
34-
},
35-
};
36-
</script>
37-
387
<style>
398
#app {
409
font-family: Avenir, Helvetica, Arial, sans-serif;

src/main.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
import Vue from "vue";
22
import App from "./App.vue";
33
import store from "./store";
4-
4+
import router from "./router";
55
import { auth } from "./firebase";
66

77
Vue.config.productionTip = false;
88

99
let app;
1010

1111
auth.onAuthStateChanged((user) => {
12-
if(!app) {
12+
if (!app) {
1313
app = new Vue({
1414
store,
15+
router,
1516
render: (h) => h(App),
1617
}).$mount("#app");
1718
store.$app = app;
1819

19-
if(user) {
20-
store.dispatch("setAuthState",user);
21-
}
22-
else {
23-
store.dispatch("setAuthState",{});
20+
if (user) {
21+
store.dispatch("setAuthState", user);
22+
} else {
23+
store.dispatch("setAuthState", {});
2424
}
2525
} else {
2626
store.$app = app;
2727
app.$mount("#app");
2828
}
29-
console.log("Auth state changed")
30-
31-
})
29+
console.log("Auth state changed");
30+
});

src/router/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Vue from "vue";
2+
import VueRouter from "vue-router";
3+
import routes from "./routes";
4+
import { auth } from "../firebase";
5+
6+
Vue.use(VueRouter);
7+
8+
const router = new VueRouter({
9+
mode: "history",
10+
linkActiveClass: "active",
11+
routes,
12+
});
13+
14+
router.beforeEach((to, from, next) => {
15+
const requiresAuth = to.matched.some((x) => x.meta.requiresAuthentication);
16+
if (requiresAuth && !auth.currentUser) {
17+
next("/");
18+
}
19+
next();
20+
});
21+
22+
export default router;

src/router/routes.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import Index from "@/views/Index";
2+
import Dashboard from "@/views/Dashboard";
3+
4+
const routes = [
5+
{
6+
path: "/",
7+
component: Index,
8+
meta: {
9+
requiresAuthentication: false,
10+
},
11+
children: [
12+
{
13+
path: "",
14+
component: Index,
15+
},
16+
],
17+
},
18+
{
19+
path: "/dashboard",
20+
component: Dashboard,
21+
meta: {
22+
requiresAuthentication: true,
23+
},
24+
children: [
25+
{
26+
path: "",
27+
component: Dashboard,
28+
},
29+
],
30+
},
31+
];
32+
33+
export default routes;

src/views/Dashboard.vue

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<template>
2+
<div id="app">
3+
<img alt="Vue logo" src="@/assets/logo.png" />
4+
Sie sind eingeloggt <button @click="logout()">Logout</button>
5+
</div>
6+
</template>
7+
8+
<script>
9+
import { mapState } from "vuex";
10+
export default {
11+
name: "Dashboard",
12+
13+
data() {
14+
return {};
15+
},
16+
computed: {
17+
...mapState(["user"]),
18+
},
19+
methods: {
20+
logout() {
21+
this.$store.dispatch("logout").then(() => {
22+
this.$router.push("/");
23+
});
24+
},
25+
},
26+
};
27+
</script>

src/views/Index.vue

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<template>
2+
<div id="app">
3+
<img alt="Vue logo" src="@/assets/logo.png" />
4+
<LoginForm @login="login" v-if="!user.uid" msg="Bitte einloggen" />
5+
</div>
6+
</template>
7+
8+
<script>
9+
import { mapState } from "vuex";
10+
import LoginForm from "@/components/LoginForm.vue";
11+
12+
export default {
13+
name: "Dashboard",
14+
15+
components: {
16+
LoginForm,
17+
},
18+
19+
data() {
20+
return {};
21+
},
22+
computed: {
23+
...mapState(["user"]),
24+
},
25+
methods: {
26+
login(payload) {
27+
this.$store.dispatch("login", payload).then(() => {
28+
this.$router.push("/dashboard");
29+
});
30+
},
31+
},
32+
};
33+
</script>

0 commit comments

Comments
 (0)