-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
22 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,3 @@ | ||
import { serverEnv } from "@/utils/env/server"; | ||
import { PrismaClient } from "@prisma/client"; | ||
|
||
const prismaClientSingleton = () => new PrismaClient(); | ||
|
||
type PrismaClientSingleton = ReturnType<typeof prismaClientSingleton>; | ||
|
||
const globalForPrisma = globalThis as unknown as { | ||
prisma: PrismaClientSingleton | undefined; | ||
}; | ||
|
||
const prisma = globalForPrisma.prisma ?? prismaClientSingleton(); | ||
|
||
export default prisma; | ||
|
||
if (serverEnv.NODE_ENV !== "production") globalForPrisma.prisma = prisma; | ||
export const prisma = new PrismaClient(); |
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,32 +1,39 @@ | ||
import { serverEnv } from "@/utils/env/server"; | ||
import { createCipheriv, createDecipheriv, pbkdf2Sync } from "crypto"; | ||
import jose from "jose"; | ||
|
||
const derivedKey = pbkdf2Sync(serverEnv.SECRET, "salt", 1000, 32, "sha256"); | ||
// Create a key from the server environment secret | ||
const key = new TextEncoder().encode(serverEnv.SECRET); | ||
|
||
export const encrypt = (value: any): string | null => { | ||
export const encrypt = async (value: any): Promise<string | null> => { | ||
try { | ||
const text = typeof value === "object" ? JSON.stringify(value) : value; | ||
const cipher = createCipheriv("aes-256-ecb", derivedKey, null); | ||
let encrypted = cipher.update(text, "utf8", "base64"); | ||
encrypted += cipher.final("base64"); | ||
return encrypted; | ||
|
||
const jwe = await new jose.CompactEncrypt(new TextEncoder().encode(text)) | ||
.setProtectedHeader({ alg: "dir", enc: "A256GCM" }) | ||
.encrypt(key); | ||
|
||
return jwe; | ||
} catch (error) { | ||
console.error("Encryption error:", error); | ||
return null; | ||
} | ||
}; | ||
|
||
export const decrypt = <T = string>(encryptedText: string): T | null => { | ||
export const decrypt = async <T = string>( | ||
encryptedText: string, | ||
): Promise<T | null> => { | ||
if (typeof encryptedText !== "string") return null; | ||
try { | ||
const decipher = createDecipheriv("aes-256-ecb", derivedKey, null); | ||
let decrypted = decipher.update(encryptedText, "base64", "utf8"); | ||
decrypted += decipher.final("utf8"); | ||
const { plaintext } = await jose.compactDecrypt(encryptedText, key); | ||
const decrypted = new TextDecoder().decode(plaintext); | ||
|
||
try { | ||
return JSON.parse(decrypted) as T; | ||
} catch (error) { | ||
return decrypted as any; | ||
return decrypted as T; | ||
} | ||
} catch (error) { | ||
console.error("Decryption error:", error); | ||
return null; | ||
} | ||
}; |
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