Skip to content

Commit

Permalink
easy
Browse files Browse the repository at this point in the history
  • Loading branch information
0-don committed Jul 4, 2024
1 parent cb51423 commit d0692ea
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@elysiajs/eden": "^1.0.14",
"@prisma/client": "^5.14.0",
"@tanstack/react-query": "^5.40.0",
"jose": "^5.6.3",
"next": "14.2.3",
"react": "^18",
"react-dom": "^18"
Expand Down
15 changes: 1 addition & 14 deletions src/lib/db.ts
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();
31 changes: 19 additions & 12 deletions src/lib/encrypt.ts
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;
}
};
2 changes: 1 addition & 1 deletion src/server/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import prisma from "@/lib/db";
import { encrypt } from "@/lib/encrypt";
import { serverEnv } from "@/utils/env/server";
import { Elysia, ParseError } from "elysia";
import { authUser } from "./typebox";
import { prisma } from "@/lib/db";

export const authRoute = new Elysia({ prefix: "/auth" })
.post(
Expand Down

0 comments on commit d0692ea

Please sign in to comment.