-
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
26 changed files
with
241 additions
and
116 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
SECRET=km12poik3mokpaxnjcojsandfoj1n | ||
SECRET=km12poik3mokpaxnjcojsandfoj1nbjt | ||
DATABASE_URL="file:../demo.db" | ||
NEXT_PUBLIC_URL=http://localhost:3000 |
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
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
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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { serverEnv } from "@/utils/env/server"; | ||
import * as jose from "jose"; | ||
|
||
// Create an encryption key from the server's SECRET environment variable | ||
const key = new TextEncoder().encode(serverEnv.SECRET); | ||
|
||
/** | ||
* Encrypts a value using JSON Web Encryption (JWE) | ||
* @param value - The value to encrypt (can be any type) | ||
* @returns A Promise that resolves to the encrypted string, or null if encryption fails | ||
*/ | ||
export const encrypt = async (value: any): Promise<string | null> => { | ||
try { | ||
// Convert objects to JSON strings, leave other types as-is | ||
const text = typeof value === "object" ? JSON.stringify(value) : value; | ||
|
||
// Create a JWE using direct encryption with AES-GCM | ||
const jwe = await new jose.CompactEncrypt(new TextEncoder().encode(text)) | ||
.setProtectedHeader({ alg: "dir", enc: "A256GCM" }) | ||
.encrypt(key); | ||
|
||
return jwe; | ||
} catch (error) { | ||
return null; | ||
} | ||
}; | ||
|
||
/** | ||
* Decrypts a JWE string | ||
* @param encryptedText - The JWE string to decrypt | ||
* @returns A Promise that resolves to the decrypted value (as type T), or null if decryption fails | ||
*/ | ||
export const decrypt = async <T = string>( | ||
encryptedText: string, | ||
): Promise<T | null> => { | ||
if (typeof encryptedText !== "string") return null; | ||
try { | ||
// Decrypt the JWE string | ||
const { plaintext } = await jose.compactDecrypt(encryptedText, key); | ||
const decrypted = new TextDecoder().decode(plaintext); | ||
|
||
// Attempt to parse the decrypted text as JSON | ||
try { | ||
return JSON.parse(decrypted) as T; | ||
} catch (error) { | ||
// If parsing fails, return the decrypted text as-is | ||
return decrypted as T; | ||
} | ||
} catch (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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
export default prisma; |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { QueryClient } from "@tanstack/react-query"; | ||
import { cache } from "react"; | ||
|
||
/** | ||
* Creates and caches a global QueryClient instance for server components only. | ||
* | ||
* This function uses React's `cache` to ensure that only one QueryClient | ||
* instance is created and reused across all server components. This helps | ||
* prevent duplicate requests and maintains consistent state. | ||
* | ||
* @returns A cached instance of QueryClient | ||
*/ | ||
const getQueryClient = cache(() => new QueryClient()); | ||
|
||
export default getQueryClient; |
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
Oops, something went wrong.