Skip to content

Commit af7eb5c

Browse files
committed
feat: get current token
1 parent e923a48 commit af7eb5c

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/helpers/Token.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
// eslint-disable-next-line no-unused-vars
2+
import { Request } from 'express'
13
import jwt, {
24
TokenExpiredError,
35
JsonWebTokenError,
46
NotBeforeError,
57
} from 'jsonwebtoken'
8+
import { isEmpty } from 'lodash'
69

710
require('dotenv').config()
811

@@ -20,10 +23,22 @@ function getToken(headers: any) {
2023
return null
2124
}
2225

23-
// Verify Token
24-
function verifyToken(header: any) {
25-
const token = getToken(header)
26+
function currentToken(req: Request) {
27+
const getCookie = req.getCookies()
28+
const getHeaders = req.getHeaders()
29+
30+
let curToken = ''
31+
if (!isEmpty(getCookie.token)) {
32+
curToken = getCookie.token
33+
} else {
34+
curToken = getToken(getHeaders)
35+
}
2636

37+
return curToken
38+
}
39+
40+
// Verify Token
41+
function verifyToken(token: string) {
2742
try {
2843
if (!token) {
2944
return { data: null, message: 'Unauthorized!' }
@@ -46,4 +61,4 @@ function verifyToken(header: any) {
4661
}
4762
}
4863

49-
export { getToken, verifyToken }
64+
export { getToken, currentToken, verifyToken }

0 commit comments

Comments
 (0)