File tree Expand file tree Collapse file tree 5 files changed +94
-41
lines changed Expand file tree Collapse file tree 5 files changed +94
-41
lines changed Original file line number Diff line number Diff line change 10
10
"dependencies" : {
11
11
"core-js" : " ^3.6.5" ,
12
12
"firebase" : " ^8.6.8" ,
13
- "vue" : " ^2.6.11"
13
+ "vue" : " ^2.6.11" ,
14
+ "vuex" : " ^3.6.2"
14
15
},
15
16
"devDependencies" : {
16
17
"@vue/cli-plugin-babel" : " ~4.5.0" ,
Original file line number Diff line number Diff line change 1
1
<template >
2
2
<div id =" app" >
3
3
<img alt =" Vue logo" src =" ./assets/logo.png" />
4
- <LoginForm @login =" login" v-if =" !loggedIn " msg =" Bitte einloggen" />
4
+ <LoginForm @login =" login" v-if =" !user.uid " msg =" Bitte einloggen" />
5
5
<div v-else >
6
- Sie sind eingeloggt
6
+ Sie sind eingeloggt < button @click = " logout() " >Logout</ button >
7
7
</div >
8
8
</div >
9
9
</template >
10
10
11
11
<script >
12
12
import LoginForm from " ./components/LoginForm.vue" ;
13
- import * as fb from " ./firebase " ;
13
+ import { mapState } from " vuex " ;
14
14
15
15
export default {
16
16
name: " App" ,
17
17
components: {
18
18
LoginForm,
19
19
},
20
20
data () {
21
- return {
22
- loggedIn: false
23
- }
24
- },
21
+ return {}
22
+ },
25
23
24
+ computed: {
25
+ ... mapState ([" user" ])
26
+ },
26
27
methods: {
27
28
login (payload ) {
28
- fb .auth .signInWithEmailAndPassword (payload .email ,payload .password ).then ((userCredentials ) => {
29
- var user = userCredentials .user ;
30
- console .log (" Logged in user:" ,user);
31
- this .loggedIn = true ;
32
- }).catch ((error ) => {
33
- var errorCode = error .code ;
34
- var errorMessage = error .message ;
35
- console .log (" Failed to login: " ,errorCode,errorMessage);
36
- this .loggedIn = false ;
37
- });
38
- }
39
- },
40
-
41
- mounted () {
42
- /*
43
- fb.auth.createUserWithEmailAndPassword("[email protected] ","")
44
- .then((userCredentials) => {
45
- var user = userCredentials.user;
46
- console.log("Newly created user: ",user);
47
- })
48
- */
49
- /*
50
- fb.demoCollection
51
- .doc("pbLh3aRXvxHzNCtp4QKb")
52
- .get()
53
- .then((doc) => {
54
- console.log(doc.data().hello);
55
- });
56
- */
29
+ this .$store .dispatch (" login" ,payload)
30
+ },
31
+ logout (){
32
+ this .$store .dispatch (" logout" );
33
+ },
57
34
},
58
35
};
59
36
</script >
Original file line number Diff line number Diff line change 1
1
import Vue from "vue" ;
2
2
import App from "./App.vue" ;
3
+ import store from "./store" ;
4
+
5
+ import { auth } from "./firebase" ;
3
6
4
7
Vue . config . productionTip = false ;
5
8
6
- new Vue ( {
7
- render : ( h ) => h ( App ) ,
8
- } ) . $mount ( "#app" ) ;
9
+ let app ;
10
+
11
+ auth . onAuthStateChanged ( ( user ) => {
12
+ if ( ! app ) {
13
+ app = new Vue ( {
14
+ store,
15
+ render : ( h ) => h ( App ) ,
16
+ } ) . $mount ( "#app" ) ;
17
+ store . $app = app ;
18
+
19
+ if ( user ) {
20
+ store . dispatch ( "setAuthState" , user ) ;
21
+ }
22
+ else {
23
+ store . dispatch ( "setAuthState" , { } ) ;
24
+ }
25
+ } else {
26
+ store . $app = app ;
27
+ app . $mount ( "#app" ) ;
28
+ }
29
+ console . log ( "Auth state changed" )
30
+
31
+ } )
Original file line number Diff line number Diff line change
1
+ import Vuex from "vuex" ;
2
+ import Vue from "vue" ;
3
+ import * as fb from "../firebase" ;
4
+
5
+ Vue . use ( Vuex ) ;
6
+ export default new Vuex . Store ( {
7
+ state : {
8
+ user : { }
9
+ } ,
10
+ mutations : {
11
+ setUser ( state , val ) {
12
+ state . user = val ;
13
+ }
14
+ } ,
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
+ }
35
+ } ,
36
+ } ) ;
37
+
You can’t perform that action at this time.
0 commit comments