@sumor/token-middleware
Advanced tools
Comparing version 1.0.1 to 1.1.0
{ | ||
"name": "@sumor/token-middleware", | ||
"description": "A token middleware for ExpressJS.", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"license": "MIT", | ||
@@ -17,3 +17,3 @@ "repository": "sumor-cloud/token-middleware", | ||
"devDependencies": { | ||
"@sumor/ssl-server": "^1.0.23", | ||
"@sumor/ssl-server": "^1.1.1", | ||
"axios": "^1.7.2", | ||
@@ -20,0 +20,0 @@ "@jest/globals": "^29.7.0", |
import Token from './Token.js' | ||
import parseCookie from './parseCookie.js' | ||
export default (req, res, next) => { | ||
const cookie = parseCookie(req.headers.cookie) | ||
req.token = new Token(id => { | ||
let existingCookie = res.getHeader('Set-Cookie') || [] | ||
if (typeof existingCookie === 'string') { | ||
existingCookie = [existingCookie] | ||
const authorization = req.headers.authorization | ||
if (authorization) { | ||
req.token = new Token() | ||
if (authorization.startsWith('Bearer ')) { | ||
req.token._id = authorization.substring(7) | ||
} | ||
const maxAge = 100 * 24 * 60 * 60 | ||
res.setHeader('Set-Cookie', [...existingCookie, `t=${id}; Path=/; HttpOnly; Max-Age=${maxAge}`]) | ||
}) | ||
req.token._id = cookie.t || null | ||
} else { | ||
const cookie = parseCookie(req.headers.cookie) | ||
req.token = new Token(id => { | ||
let existingCookie = res.getHeader('Set-Cookie') || [] | ||
if (typeof existingCookie === 'string') { | ||
existingCookie = [existingCookie] | ||
} | ||
const maxAge = 100 * 24 * 60 * 60 | ||
res.setHeader('Set-Cookie', [ | ||
...existingCookie, | ||
`t=${id}; Path=/; HttpOnly; Max-Age=${maxAge}` | ||
]) | ||
}) | ||
req.token._id = cookie.t || null | ||
} | ||
next() | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9626
160