Commit a417a8d8 by Nguyễn Đức Huy

store all in vuex

parent 378b97db
Pipeline #1170 failed with stages
in 0 seconds
...@@ -5,18 +5,15 @@ ...@@ -5,18 +5,15 @@
<Logo /> <Logo />
</div> </div>
<ul id="navbar-child"> <ul id="navbar-child">
<li><nuxt-link to="/">Home</nuxt-link></li> <li><nuxt-link to="/ssss">Home</nuxt-link></li>
<li><nuxt-link to="/chat">Chat Room</nuxt-link></li> <li><nuxt-link to="/chat">Chat Room</nuxt-link></li>
<li><nuxt-link to="/about">About Me</nuxt-link></li> <li><nuxt-link to="/my_page">About Me</nuxt-link></li>
<li> <li v-if="$store.getters.isAuth == true"><nuxt-link to="/login">Logout</nuxt-link></li>
<!-- hi username -->
Hi <!-- <li> -->
<!-- log out button -->
</li>
<li>
<!-- log in button --> <!-- log in button -->
<nuxt-link to="/about">{Not login yet}</nuxt-link> <!-- <nuxt-link to="/about">{Not login yet}</nuxt-link> -->
</li> <!-- </li> -->
</ul> </ul>
</div> </div>
<div class="default_nuxt"> <div class="default_nuxt">
......
export default function (context) {
if (context.store.getters.isAuth == false) {
context.redirect('/login')
}
}
...@@ -36,8 +36,8 @@ export default { ...@@ -36,8 +36,8 @@ export default {
// https://go.nuxtjs.dev/pwa // https://go.nuxtjs.dev/pwa
'@nuxtjs/pwa', '@nuxtjs/pwa',
// https://go.nuxtjs.dev/content // https://go.nuxtjs.dev/content
'@nuxt/content', // '@nuxt/content',
'@nuxtjs/auth-next', // '@nuxtjs/auth-next',
], ],
// Axios module configuration (https://go.nuxtjs.dev/config-axios) // Axios module configuration (https://go.nuxtjs.dev/config-axios)
......
...@@ -72,13 +72,11 @@ export default { ...@@ -72,13 +72,11 @@ export default {
// }) // })
// }, // },
onSubmit() { onSubmit() {
this.$store this.$store.dispatch('authenticationUser', {
.dispatch('authenticationUser', { email: this.email,
email: this.email, password: this.password,
password: this.password, checkLogin: this.checkLogin,
checkLogin: this.checkLogin, })
})
// .then(() => this.$router.push('/success_goi_nha'))
}, },
}, },
} }
......
<template>
<div class="container">Hello {{ this.$store.state.token }}</div>
</template>
<script>
export default {
middleware: 'auth',
}
</script>
<style scope>
@import '@/assets/css/custom.scss';
.container {
margin: 0 auto;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
}
</style>
...@@ -9,6 +9,9 @@ const createStore = () => { ...@@ -9,6 +9,9 @@ const createStore = () => {
setToken(state, token) { setToken(state, token) {
state.token = token state.token = token
}, },
clearToken(state) {
stateate.token = null
},
}, },
actions: { actions: {
authenticationUser(context, credentials) { authenticationUser(context, credentials) {
...@@ -29,6 +32,10 @@ const createStore = () => { ...@@ -29,6 +32,10 @@ const createStore = () => {
alert('Register Success !') alert('Register Success !')
} else { } else {
alert('Login Success !') alert('Login Success !')
// console.log(result)
context.commit('setToken', result.idToken)
// context.commit('setToken', result.idToken)
this.$router.push('my_page')
} }
// window.location.href = '/' // window.location.href = '/'
}) })
...@@ -36,6 +43,16 @@ const createStore = () => { ...@@ -36,6 +43,16 @@ const createStore = () => {
alert(e) alert(e)
}) })
}, },
setLogoutTinme(context, duration) {
setTimeout(() => {
contextontext.commit('clearToken')
}, duration)
},
},
getters: {
isAuth(state) {
return state.token != null
},
}, },
}) })
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment