Skip to content

Commit 12ff7a5

Browse files
committed
[CODEBREW] Added video 009 progress
1 parent 4ef858c commit 12ff7a5

File tree

3 files changed

+87
-30
lines changed

3 files changed

+87
-30
lines changed

src/firebase.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import firebase from "firebase/app";
22
import "firebase/firestore";
33
import "firebase/auth";
4+
import "firebase/storage";
45

56
const firebaseConfig = {
67
apiKey: "AIzaSyDVFoOhFYY1zqDcSrgd05IVbonbcSE0AAk",
@@ -16,7 +17,8 @@ firebase.initializeApp(firebaseConfig);
1617

1718
const db = firebase.firestore();
1819
const auth = firebase.auth();
20+
const storage = firebase.storage();
1921

2022
const demoCollection = db.collection("demo");
2123

22-
export { db, auth, demoCollection };
24+
export { db, auth, storage, demoCollection };

src/store/index.js

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,58 @@ import * as fb from "../firebase";
44

55
Vue.use(Vuex);
66
export default new Vuex.Store({
7-
state: {
8-
user: {}
7+
state: {
8+
user: {},
9+
image: "",
10+
},
11+
mutations: {
12+
setUser(state, val) {
13+
state.user = val;
914
},
10-
mutations: {
11-
setUser(state,val) {
12-
state.user = val;
13-
}
15+
setImageURL(state, val) {
16+
state.image = val;
1417
},
15-
actions: {
16-
async setAuthState({commit}, user) {
17-
console.log(user)
18-
commit("setUser",user);
19-
},
20-
async login({commit},payload) {
21-
return fb.auth.signInWithEmailAndPassword(payload.email,payload.password).then((userCredentials) => {
22-
var user = userCredentials.user;
23-
console.log("Logged in user:",user);
24-
commit("setUser",user);
25-
}).catch((error) => {
26-
var errorCode = error.code;
27-
var errorMessage = error.message;
28-
console.log("Failed to login: ",errorCode,errorMessage);
29-
});
30-
},
31-
async logout({commit}) {
32-
fb.auth.signOut();
33-
commit("setUser",{})
34-
}
18+
},
19+
actions: {
20+
async setAuthState({ commit }, user) {
21+
console.log(user);
22+
commit("setUser", user);
3523
},
24+
async login({ commit }, payload) {
25+
return fb.auth
26+
.signInWithEmailAndPassword(payload.email, payload.password)
27+
.then((userCredentials) => {
28+
var user = userCredentials.user;
29+
console.log("Logged in user:", user);
30+
commit("setUser", user);
31+
})
32+
.catch((error) => {
33+
var errorCode = error.code;
34+
var errorMessage = error.message;
35+
console.log("Failed to login: ", errorCode, errorMessage);
36+
});
37+
},
38+
async logout({ commit }) {
39+
fb.auth.signOut();
40+
commit("setUser", {});
41+
},
42+
async uploadProfilePicture({ commit }, payload) {
43+
var storageRef = fb.storage.ref();
44+
var pictureRef = storageRef.child("profilePicture.png");
45+
return pictureRef.put(payload.image).then((snapshot) => {
46+
return snapshot.ref.getDownloadURL().then((res) => {
47+
console.log(res);
48+
commit("setImageURL", res);
49+
});
50+
});
51+
},
52+
async fetchProfilePicture({ commit }) {
53+
var storageRef = fb.storage.ref();
54+
var pictureRef = storageRef.child("profilePicture.png");
55+
return pictureRef.getDownloadURL().then((res) => {
56+
console.log(res);
57+
commit("setImageURL", res);
58+
});
59+
},
60+
},
3661
});
37-

src/views/Dashboard.vue

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
<template>
22
<div id="app">
3-
<img alt="Vue logo" src="@/assets/logo.png" />
3+
<img class="image" alt="Vue logo" :src="image" />
4+
<br />
5+
Profilbild hochladen: <br />
6+
<input
7+
type="file"
8+
accept="image/*"
9+
@change="uploadProfilePicture($event)"
10+
/>
11+
<br />
12+
<br />
13+
<br />
14+
<hr />
415
Sie sind eingeloggt <button @click="logout()">Logout</button>
516
</div>
617
</template>
@@ -14,14 +25,34 @@ export default {
1425
return {};
1526
},
1627
computed: {
17-
...mapState(["user"]),
28+
...mapState(["user", "image"]),
29+
},
30+
mounted() {
31+
this.$store.dispatch("fetchProfilePicture");
1832
},
1933
methods: {
2034
logout() {
2135
this.$store.dispatch("logout").then(() => {
2236
this.$router.push("/");
2337
});
2438
},
39+
uploadProfilePicture(event) {
40+
console.log("Upload image: ", event.target.files[0]);
41+
let payload = {
42+
uid: this.user.uid,
43+
image: event.target.files[0],
44+
};
45+
this.$store.dispatch("uploadProfilePicture", payload).then(() => {
46+
alert("Successfully uploaded profile picture");
47+
});
48+
},
2549
},
2650
};
2751
</script>
52+
53+
<style scoped>
54+
.image {
55+
width: 300px;
56+
height: 300px;
57+
}
58+
</style>

0 commit comments

Comments
 (0)