Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nuxtjschat
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nguyễn Đức Huy
nuxtjschat
Commits
0eeb1d52
Commit
0eeb1d52
authored
Jan 29, 2021
by
Nguyễn Đức Huy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Set time out for token and hold user login when refesh page
parent
4911380d
Pipeline
#1172
failed with stages
in 0 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
78 additions
and
14 deletions
+78
-14
layouts/default.vue
+21
-3
middleware/check_auth.js
+1
-1
store/index.js
+56
-10
No files found.
layouts/default.vue
View file @
0eeb1d52
...
...
@@ -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
:
5
0px
;
height
:
6
0px
;
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
>
middleware/check_auth.js
View file @
0eeb1d52
export
default
function
(
context
)
{
if
(
process
.
client
)
context
.
store
.
dispatch
(
'initAuth'
)
context
.
store
.
dispatch
(
'initAuth'
,
context
.
req
)
}
store/index.js
View file @
0eeb1d52
...
...
@@ -10,8 +10,11 @@ const createStore = () => {
setToken
(
state
,
token
)
{
state
.
token
=
token
},
setName
(
state
,
name
)
{
state
.
name
=
name
},
clearToken
(
state
)
{
state
ate
.
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
},
},
})
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment