Skip to content

Commit

Permalink
feat: get current token
Browse files Browse the repository at this point in the history
  • Loading branch information
masb0ymas committed Oct 20, 2020
1 parent e923a48 commit af7eb5c
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/helpers/Token.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// eslint-disable-next-line no-unused-vars
import { Request } from 'express'
import jwt, {
TokenExpiredError,
JsonWebTokenError,
NotBeforeError,
} from 'jsonwebtoken'
import { isEmpty } from 'lodash'

require('dotenv').config()

Expand All @@ -20,10 +23,22 @@ function getToken(headers: any) {
return null
}

// Verify Token
function verifyToken(header: any) {
const token = getToken(header)
function currentToken(req: Request) {
const getCookie = req.getCookies()
const getHeaders = req.getHeaders()

let curToken = ''
if (!isEmpty(getCookie.token)) {
curToken = getCookie.token
} else {
curToken = getToken(getHeaders)
}

return curToken
}

// Verify Token
function verifyToken(token: string) {
try {
if (!token) {
return { data: null, message: 'Unauthorized!' }
Expand All @@ -46,4 +61,4 @@ function verifyToken(header: any) {
}
}

export { getToken, verifyToken }
export { getToken, currentToken, verifyToken }

0 comments on commit af7eb5c

Please sign in to comment.