Toggle navigation
Toggle navigation
This project
Loading...
Sign in
2021-1-capstone-design1
/
RIT_Project1
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
1
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
박권수
2021-05-09 03:20:42 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
55f9e222275bb7ea12184867bcc7df782252e250
55f9e222
1 parent
2373c08d
feat. jwt Middleware
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
0 deletions
server/src/lib/jwtMiddleWare.js
server/src/lib/jwtMiddleWare.js
View file @
55f9e22
const
jwt
=
require
(
"jsonwebtoken"
);
const
User
=
require
(
'../models/user'
);
const
jwtMiddleware
=
async
(
ctx
,
next
)
=>
{
const
token
=
ctx
.
cookies
.
get
(
"access_token"
);
if
(
!
token
)
{
return
next
();
}
try
{
const
decoded
=
jwt
.
verify
(
token
,
process
.
env
.
JWT_SECRET
);
ctx
.
state
.
user
=
{
_id
:
decoded
.
_id
,
userId
:
decoded
.
userId
};
const
now
=
Math
.
floor
(
Date
.
now
()
/
1000
);
if
(
decoded
.
exp
-
now
<
60
*
60
*
24
*
3.5
)
{
const
user
=
await
User
.
findById
(
decoded
.
_id
);
const
token
=
user
.
generateToken
();
ctx
.
cookies
.
set
(
'access_token'
,
token
,
{
httpOnly
:
true
,
maxAge
:
1000
*
60
*
60
*
24
*
7
})
}
}
catch
(
e
)
{
ctx
.
state
.
user
=
null
;
}
return
next
();
};
module
.
exports
=
jwtMiddleware
;
...
...
Please
register
or
login
to post a comment