Commit 0eeb1d52 by Nguyễn Đức Huy

Set time out for token and hold user login when refesh page

parent 4911380d
Pipeline #1172 failed with stages
in 0 seconds
......@@ -4,11 +4,13 @@
<div id="logo">
<Logo />
</div>
<ul id="navbar-child">
<div class="text-warning hi h3 align-middle" v-if="$store.getters.isAuth == true"> Hello {{this.$store.getters.getName}}</div>
<ul id="navbar-child" class="pb-3">
<li><nuxt-link to="/">Home</nuxt-link></li>
<li><nuxt-link to="/chat">Chat Room</nuxt-link></li>
<li><nuxt-link to="/my_page">About Me</nuxt-link></li>
<li v-if="$store.getters.isAuth == true"><nuxt-link to="/login">Logout</nuxt-link></li>
<li v-if="$store.getters.isAuth == true"><a href="#" @click="onLogout">Logout</a></li>
<li v-else><nuxt-link to="/login">Login</nuxt-link></li>
<!-- <li> -->
......@@ -25,7 +27,7 @@
<style lang="scss">
#navbar {
height: 50px;
height: 60px;
width: 100%;
background-color: black;
#navbar-child {
......@@ -59,6 +61,12 @@
height: 60px;
float: left;
}
.hi {
padding-left: 50px;
padding-top: 20px;
width: auto;
float: left;
}
}
</style>
......@@ -111,3 +119,13 @@ html {
background-color: #35495e;
}
</style>
<script>
export default {
methods: {
onLogout() {
this.$store.dispatch('logout')
this.$router.push('/')
}
}
}
</script>
export default function (context) {
if (process.client) context.store.dispatch('initAuth')
context.store.dispatch('initAuth', context.req)
}
......@@ -10,8 +10,11 @@ const createStore = () => {
setToken(state, token) {
state.token = token
},
setName(state, name) {
state.name = name
},
clearToken(state) {
stateate.token = null
state.token = null
},
},
actions: {
......@@ -40,7 +43,14 @@ const createStore = () => {
'tokenExpiration',
new Date().getTime() + result.expiresIn * 1000
)
console.log(result.expiresIn)
Cookies.set('name', credentials.email)
// console.log(result.expiresIn)
Cookies.set('token', result.idToken)
Cookies.set(
'tokenExpiration',
new Date().getTime() + result.expiresIn * 1000
)
context.dispatch('setLogoutTimer', result.expiresIn * 1000)
// context.commit('setToken', result.idToken)
this.$router.push('my_page')
......@@ -58,23 +68,59 @@ const createStore = () => {
}, duration)
},
initAuth(context) {
const token = localStorage.getItem('token')
const tokenExpiration = localStorage.getItem('tokenExpiration')
initAuth(context, req) {
let token, tokenExpiration
if (req) {
// Handle first time
if (!req.headers.cookie) return false
const keyToken = req.headers.cookie
.split(';')
.find((c) => c.trim().startsWith('token='))
const keyTokenExpiration = req.headers.cookie
.split(';')
.find((c) => c.trim().startsWith('tokenExpiration='))
if (new Date().getTime() > tokenExpiration || !token) return false
if (!keyToken || !keyTokenExpiration) {
context.dispatch('logout')
return false
}
token = keyToken.split('=')[1]
tokenExpiration = keyTokenExpiration.split('=')[1]
} else {
token = localStorage.getItem('token')
tokenExpiration = localStorage.getItem('tokenExpiration')
if (new Date().getTime() > tokenExpiration || !token) {
context.dispatch('logout')
return false
}
localStorage.setItem(
'tokenExpiration',
tokenExpiration - new Date().getTime()
)
}
localStorage.setItem(
'tokenExpiration',
tokenExpiration - new Date().getTime()
)
context.commit('setToken', token)
},
logout(context) {
context.commit('clearToken')
Cookies.remove('token')
Cookies.remove('tokenExpiration')
localStorage.removeItem('token')
localStorage.removeItem('tokenExpiration')
},
},
getters: {
isAuth(state) {
return state.token != null
},
getName() {
const nameEmail = Cookies.get('name').split('@')[0]
return nameEmail
},
},
})
}
......
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