@a-type/auth
Advanced tools
Comparing version
@@ -0,1 +1,2 @@ | ||
import { parse } from 'cookie'; | ||
import { SignJWT, jwtVerify } from 'jose'; | ||
@@ -13,10 +14,13 @@ export const defaultShortNames = { | ||
var _a; | ||
const cookie = (_a = req.headers.get('cookie')) !== null && _a !== void 0 ? _a : ''; | ||
const match = cookie.match(new RegExp(`${this.options.cookieName}=([^;]+)`)); | ||
if (!match) { | ||
const cookieHeader = (_a = req.headers.get('cookie')) !== null && _a !== void 0 ? _a : ''; | ||
const cookies = parse(cookieHeader); | ||
const cookieValue = cookies[this.options.cookieName]; | ||
if (!cookieValue) { | ||
return null; | ||
} | ||
const cookieValue = match[1]; | ||
// read the JWT from the cookie | ||
const jwt = await jwtVerify(cookieValue, this.secret); | ||
const jwt = await jwtVerify(cookieValue, this.secret, { | ||
issuer: this.options.issuer, | ||
audience: this.options.audience, | ||
}); | ||
// convert the JWT claims to a session object | ||
@@ -53,3 +57,3 @@ const session = Object.fromEntries(Object.entries(jwt).map(([key, value]) => [this.getLongName(key), value])); | ||
} | ||
const jwt = builder.sign(this.secret); | ||
const jwt = await builder.sign(this.secret); | ||
return { | ||
@@ -56,0 +60,0 @@ 'Set-Cookie': `${this.options.cookieName}=${jwt}; Path=/; HttpOnly; SameSite=Strict`, |
{ | ||
"name": "@a-type/auth", | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"description": "My personal auth request handlers", | ||
@@ -5,0 +5,0 @@ "module": "dist/esm/index.js", |
@@ -0,1 +1,2 @@ | ||
import { parse } from 'cookie'; | ||
import { SignJWT, jwtVerify } from 'jose'; | ||
@@ -46,12 +47,13 @@ | ||
getSession = async (req: Request) => { | ||
const cookie = req.headers.get('cookie') ?? ''; | ||
const match = cookie.match( | ||
new RegExp(`${this.options.cookieName}=([^;]+)`), | ||
); | ||
if (!match) { | ||
const cookieHeader = req.headers.get('cookie') ?? ''; | ||
const cookies = parse(cookieHeader); | ||
const cookieValue = cookies[this.options.cookieName]; | ||
if (!cookieValue) { | ||
return null; | ||
} | ||
const cookieValue = match[1]; | ||
// read the JWT from the cookie | ||
const jwt = await jwtVerify(cookieValue, this.secret); | ||
const jwt = await jwtVerify(cookieValue, this.secret, { | ||
issuer: this.options.issuer, | ||
audience: this.options.audience, | ||
}); | ||
// convert the JWT claims to a session object | ||
@@ -97,3 +99,3 @@ const session: Session = Object.fromEntries( | ||
const jwt = builder.sign(this.secret); | ||
const jwt = await builder.sign(this.secret); | ||
return { | ||
@@ -100,0 +102,0 @@ 'Set-Cookie': `${this.options.cookieName}=${jwt}; Path=/; HttpOnly; SameSite=Strict`, |
Sorry, the diff of this file is not supported yet
72583
0.48%1496
0.4%