18.06 Add Logout Button - Show only when User Logged In

This commit is contained in:
Fredrick W. Warren 2021-02-13 16:57:24 -08:00
parent 92c0c1486b
commit e127886523
3 changed files with 32 additions and 3 deletions

View File

@ -9,10 +9,12 @@ import { mapActions } from 'vuex'
export default { export default {
name: 'App', name: 'App',
methods: { methods: {
...mapActions('settings', ['getSettings']) ...mapActions('settings', ['getSettings']),
...mapActions('auth', ['handleAuthStateChange'])
}, },
mounted () { mounted () {
this.getSettings() this.getSettings()
this.handleAuthStateChange()
} }
} }
</script> </script>

View File

@ -6,12 +6,20 @@
Awesome Todo Awesome Todo
</q-toolbar-title> </q-toolbar-title>
<q-btn <q-btn
v-if="!loggedIn"
to="/auth" to="/auth"
flat flat
icon-right="account_circle" icon-right="account_circle"
label="Login" label="Login"
class="absolute-right" class="absolute-right"
/> />
<q-btn
v-else
flat
icon-right="account_circle"
label="Logout"
class="absolute-right"
/>
</q-toolbar> </q-toolbar>
</q-header> </q-header>
@ -57,6 +65,7 @@
</template> </template>
<script> <script>
import { mapState } from 'vuex'
export default { export default {
name: 'MainLayout', name: 'MainLayout',
@ -79,6 +88,11 @@ export default {
} }
] ]
} }
},
computed: {
...mapState('auth', ['loggedIn'])
},
methods: {
} }
} }
</script> </script>

View File

@ -1,13 +1,17 @@
import { firebaseAuth } from 'boot/firebase' import { firebaseAuth } from 'boot/firebase'
const state = { const state = {
loggedIn: false
} }
const mutations = { const mutations = {
setLoggedIn (state, value) {
state.loggedIn = value
}
} }
const actions = { const actions = {
registerUser ({ state }, payload) { registerUser ({ commit }, payload) {
firebaseAuth.createUserWithEmailAndPassword(payload.email, payload.password) firebaseAuth.createUserWithEmailAndPassword(payload.email, payload.password)
.then(response => { .then(response => {
console.log('response: ', response) console.log('response: ', response)
@ -16,7 +20,7 @@ const actions = {
console.log('error.message: ', error) console.log('error.message: ', error)
}) })
}, },
loginUser ({ state }, payload) { loginUser ({ commit }, payload) {
firebaseAuth.signInWithEmailAndPassword(payload.email, payload.password) firebaseAuth.signInWithEmailAndPassword(payload.email, payload.password)
.then(response => { .then(response => {
console.log('response: ', response) console.log('response: ', response)
@ -24,6 +28,15 @@ const actions = {
.catch(error => { .catch(error => {
console.log('error.message: ', error) console.log('error.message: ', error)
}) })
},
handleAuthStateChange ({ commit }) {
firebaseAuth.onAuthStateChanged(function (user) {
if (user) {
commit('setLoggedIn', true)
} else {
commit('setLoggedIn', false)
}
})
} }
} }