From 782384cad6ecf9b658c8de354b3d30336b75182e Mon Sep 17 00:00:00 2001 From: Don Strawsburg Date: Thu, 30 Jan 2025 17:00:22 -0500 Subject: [PATCH] I hate authentication --- .env.local | 2 +- package.json | 4 ++- pnpm-lock.yaml | 18 ++++++++++- src/actions/userActions.ts | 41 +++++++++++++++++------- src/app/Admin/Accounts/page.tsx | 7 ++-- src/app/components/PopNavDialog/page.tsx | 8 +++-- src/components/AccountsTable/index.tsx | 6 ++-- src/db/index.ts | 1 + src/drizzle/schema/schema.ts | 7 ++-- src/lib/auth/index.ts | 3 +- src/server/db/index.ts | 2 +- 11 files changed, 72 insertions(+), 27 deletions(-) diff --git a/.env.local b/.env.local index 484400e..07f1ec5 100644 --- a/.env.local +++ b/.env.local @@ -2,4 +2,4 @@ AUTH_SECRET="a73X70xifFO5+V9oQ+/NKDDTgA4dsuWWxvFX6T1v1ns=" # Added by `npx auth` NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_bmV3LXN3YW4tMjguY2xlcmsuYWNjb3VudHMuZGV2JA CLERK_SECRET_KEY=•••••••••••••••••••••••••••••••••••••••••••••••••• -REACT_EDITOR=atom \ No newline at end of file +REACT_EDITOR=code \ No newline at end of file diff --git a/package.json b/package.json index 4bcf2d7..ac4a4d8 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "observable": "link:@trpc/server/observable", "oslo": "^1.2.1", "path": "^0.12.7", + "perf_hooks": "^0.0.1", "pg": "^8.13.1", "postgres": "^3.4.5", "prettier": "^3.4.2", @@ -63,6 +64,7 @@ "superjson": "^2.2.2", "tailwind-merge": "^2.5.4", "tailwindcss-animate": "^1.0.7", + "tls": "^0.0.1", "uuid": "^11.0.3", "zod": "^3.24.1" }, @@ -82,7 +84,7 @@ "drizzle-orm": "^0.38.2", "eslint": "^8", "eslint-config-next": "15.0.3", - "fs": "^0.0.1-security", + "fs": "0.0.1-security", "next-auth": "^5.0.0-beta.25", "postcss": "^8.5.1", "tailwindcss": "^3.4.17", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 38d05ee..2c78f64 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -137,6 +137,9 @@ importers: path: specifier: ^0.12.7 version: 0.12.7 + perf_hooks: + specifier: ^0.0.1 + version: 0.0.1 pg: specifier: ^8.13.1 version: 8.13.1 @@ -170,6 +173,9 @@ importers: tailwindcss-animate: specifier: ^1.0.7 version: 1.0.7(tailwindcss@3.4.17) + tls: + specifier: ^0.0.1 + version: 0.0.1 uuid: specifier: ^11.0.3 version: 11.0.5 @@ -223,7 +229,7 @@ importers: specifier: 15.0.3 version: 15.0.3(eslint@8.57.1)(typescript@5.7.3) fs: - specifier: ^0.0.1-security + specifier: 0.0.1-security version: 0.0.1-security next-auth: specifier: ^5.0.0-beta.25 @@ -3624,6 +3630,9 @@ packages: peberminta@0.9.0: resolution: {integrity: sha512-XIxfHpEuSJbITd1H3EeQwpcZbTLHc+VVr8ANI9t5sit565tsI4/xK3KWTUFE2e6QiangUkh3B0jihzmGnNrRsQ==} + perf_hooks@0.0.1: + resolution: {integrity: sha512-qG/D9iA4KDme+KF4vCObJy6Bouu3BlQnmJ8jPydVPm32NJBD9ZK1ZNgXSYaZKHkVC1sKSqUiLgFvAZPUiIEnBw==} + pg-cloudflare@1.1.1: resolution: {integrity: sha512-xWPagP/4B6BgFO+EKz3JONXv3YDgvkbVrGw2mTo3D6tVDQRh1e7cqVGvyR3BE+eQgAvx1XhW/iEASj4/jCWl3Q==} @@ -4195,6 +4204,9 @@ packages: tiny-warning@1.0.3: resolution: {integrity: sha512-lBN9zLN/oAf68o3zNXYrdCt1kP8WsiGW8Oo2ka41b2IM5JL/S1CTyX1rW0mb/zSuJun0ZUrDxx4sqvYS2FWzPA==} + tls@0.0.1: + resolution: {integrity: sha512-GzHpG+hwupY8VMR6rYsnAhTHqT/97zT45PG8WD5eTT1lq+dFE0nN+1PYpsoBcHJgSmTz5ceK2Cv88IkPmIPOtQ==} + to-regex-range@5.0.1: resolution: {integrity: sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==} engines: {node: '>=8.0'} @@ -7720,6 +7732,8 @@ snapshots: peberminta@0.9.0: {} + perf_hooks@0.0.1: {} + pg-cloudflare@1.1.1: optional: true @@ -8356,6 +8370,8 @@ snapshots: tiny-warning@1.0.3: {} + tls@0.0.1: {} + to-regex-range@5.0.1: dependencies: is-number: 7.0.0 diff --git a/src/actions/userActions.ts b/src/actions/userActions.ts index 36a97b2..0966fe5 100644 --- a/src/actions/userActions.ts +++ b/src/actions/userActions.ts @@ -2,9 +2,13 @@ import { eq, not , asc} from "drizzle-orm"; import { revalidatePath } from "next/cache"; import { db } from "@src/db"; -import { users } from "@schemas/schema"; +import { sessions, users } from "@schemas/schema"; import { stringWidth } from "bun"; import { generateId } from "lucia"; +import { validateRequest } from "@/lib/auth/validate-request"; +import { lucia } from "@/lib/auth"; +import { redirect } from "next/navigation"; +import { cookies } from "next/headers"; export const getData = async () => { const data = await db.select().from(users).orderBy(asc(users.last_name)); @@ -29,12 +33,7 @@ export const getUserByID = async (id:string) => { return data[0]; }; -/*export const addUser = async ( first_name: string, last_name: string, username: string, email: string, password_hash : string) => { - await db.insert(users).values({ - first_name : first_name, last_name: last_name, username: email, email: email, password_hash : password_hash - }); -};*/ -export const addUser = async (id: string, first_name: string, last_name: string, username: string, email: string, emailVerified: boolean, password_hash: string, hashedPassword:string) => { +export const addUser = async (id: string, first_name: string, last_name: string, username: string, email: string, emailVerified: boolean, hashedPassword:string) => { const [addedUser] = await db.insert(users).values({ id: id, name: `${first_name} ${last_name}`, @@ -43,8 +42,7 @@ export const addUser = async (id: string, first_name: string, last_name: string, username: email, email: email, emailVerified:emailVerified, - password_hash: password_hash, - hash_password: hashedPassword, + hashedPassword: hashedPassword, full_name: `${first_name} ${last_name}`, }).returning(); // Returns the inserted user (adjust "*" to specific columns if necessary) @@ -56,7 +54,7 @@ export const deleteUser = async (id: string) => { revalidatePath("/"); }; -export const editUser = async (id: string, first_name: string, last_name: string, username: string, email : string, password_hash: string) => { +export const editUser = async (id: string, first_name: string, last_name: string, username: string, email : string, hashedPassword: string) => { await db .update(users) .set({ @@ -64,7 +62,7 @@ export const editUser = async (id: string, first_name: string, last_name: string last_name: last_name, username: username, email: email, - password_hash: password_hash + hashedPassword: hashedPassword }) .where(eq(users.id, id)); revalidatePath("/"); @@ -79,4 +77,23 @@ export const makeAdmin = async ( email : string) => { }) .where(eq(users.email, email)); revalidatePath("/"); -}; \ No newline at end of file +}; + +export const logoutSessionKeep = async (sessionId: string) => { + console.log(sessionId); + await db.delete(sessions).where(eq(sessions.id, sessionId)); + revalidatePath("/"); +} + +export async function logoutSession(): Promise<{ error: string } | void> { + const { session } = await validateRequest(); + if (!session) { + return { + error: "No session found", + }; + } + await lucia.invalidateSession(session.id); + const sessionCookie = lucia.createBlankSessionCookie(); + (await cookies()).set(sessionCookie.name, sessionCookie.value, sessionCookie.attributes); + return redirect("/"); +} \ No newline at end of file diff --git a/src/app/Admin/Accounts/page.tsx b/src/app/Admin/Accounts/page.tsx index 312f8d1..fba71c1 100644 --- a/src/app/Admin/Accounts/page.tsx +++ b/src/app/Admin/Accounts/page.tsx @@ -1,5 +1,5 @@ -import { accounts } from "@schemas/schema"; -import { getViewAccounts } from "@actions/accountActions"; +import { users } from "@schemas/schema"; +import { getAllUsersOrdrByLastname } from "@actions/userActions"; import AccountsTable from "@components/AccountsTable"; // Adjust the import path as necessary import React, { Suspense } from 'react'; import { ColumnHeadings } from "@src/lib/bb_utils"; @@ -10,11 +10,12 @@ import PageHero from "@components/PageHero"; export default async function AccountsPage() { const columnHeadings = new ColumnHeadings([ + "E-Mail", "First Name", "Last Name", "Actions", ]); - const data = await getViewAccounts(); + const data = await getAllUsersOrdrByLastname(); return (
diff --git a/src/app/components/PopNavDialog/page.tsx b/src/app/components/PopNavDialog/page.tsx index 802c9b2..880c91d 100644 --- a/src/app/components/PopNavDialog/page.tsx +++ b/src/app/components/PopNavDialog/page.tsx @@ -23,6 +23,10 @@ import { import { validateRequest } from "@/lib/auth/validate-request"; import { User } from "lucia"; import Cookies from "js-cookie"; +import { logoutSession } from "@/actions/userActions"; +import { useRouter } from "next/navigation"; +import { lucia } from "@/lib/auth"; +import { logout } from "@/lib/auth/actions"; const navigation = { categories: [ @@ -90,6 +94,7 @@ const navigation = { export default function PopNavDialog(props:any) { const [open, setOpen] = useState(false); const [user, setUser] = useState(null); + const router = useRouter(); useEffect(() => { const fetchUser = async () => { @@ -372,8 +377,7 @@ export default function PopNavDialog(props:any) {
+ href={linkPath} onClick={async (e) => {e.preventDefault; console.log("in the onclick"); await logout()} } className="text-sm font-medium text-gray-700 hover:text-gray-800"> {user == null?"Sign In": "Log Out"}