-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Update NEXTAUTH_SECRET configuration in cypress.config.ts and …
…route.ts
- Loading branch information
1 parent
e2cb2ff
commit 18ac198
Showing
7 changed files
with
414 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
/// <reference types="cypress" /> | ||
// / <reference types="cypress" /> | ||
|
||
declare namespace Cypress { | ||
interface Chainable { | ||
rewriteHeaders(): void | ||
import hkdf from "@panva/hkdf" | ||
import { EncryptJWT, JWTPayload } from "jose" | ||
|
||
declare global { | ||
namespace Cypress { | ||
interface Chainable { | ||
rewriteHeaders(): void | ||
login(): void | ||
} | ||
} | ||
} | ||
|
||
// commands.ts | ||
// Origin: https://www.tomoliver.net/posts/cypress-samesite-problem | ||
Cypress.Commands.add("rewriteHeaders", () => { | ||
cy.intercept("*", (req) => | ||
|
@@ -25,3 +30,49 @@ Cypress.Commands.add("rewriteHeaders", () => { | |
}), | ||
) | ||
}) | ||
|
||
async function getDerivedEncryptionKey(secret: string) { | ||
return await hkdf( | ||
"sha256", | ||
secret, | ||
"", | ||
"NextAuth.js Generated Encryption Key", | ||
32, | ||
) | ||
} | ||
|
||
export async function encode( | ||
token: JWTPayload, | ||
secret: string, | ||
): Promise<string> { | ||
const maxAge = 30 * 24 * 60 * 60 | ||
const encryptionSecret = await getDerivedEncryptionKey(secret) | ||
return await new EncryptJWT(token) | ||
.setProtectedHeader({ alg: "dir", enc: "A256GCM" }) | ||
.setIssuedAt() | ||
.setExpirationTime(Math.round(Date.now() / 1000 + maxAge)) | ||
.setJti("test") | ||
.encrypt(encryptionSecret) | ||
} | ||
|
||
Cypress.Commands.add("login", () => { | ||
const payload = { | ||
name: "Testing", | ||
email: "[email protected]", | ||
picture: "https://avatars.githubusercontent.com/u/65473367?v=", | ||
iat: new Date().getTime(), | ||
exp: new Date().getTime() + 30 * 24 * 60 * 60 * 1000, | ||
} | ||
|
||
cy.wrap(null) | ||
.then(() => { | ||
return encode(payload, Cypress.env("NEXTAUTH_SECRET")) | ||
}) | ||
.then((encryptedToken) => | ||
cy.setCookie("next-auth.session-token", encryptedToken, { | ||
expiry: new Date().setDate(new Date().getDate() + 2), | ||
path: "/", | ||
sameSite: "lax", | ||
}), | ||
) | ||
}) |
Oops, something went wrong.