mirror of
https://gitea.gofwd.group/dstrawsb/ballistic-builder.git
synced 2025-12-06 02:36:44 -05:00
I hate authentication
This commit is contained in:
@@ -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("/");
|
||||
};
|
||||
};
|
||||
|
||||
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("/");
|
||||
}
|
||||
@@ -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 (
|
||||
<div>
|
||||
<PageHero title="Accounts" />
|
||||
|
||||
@@ -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<User | null>(null);
|
||||
const router = useRouter();
|
||||
|
||||
useEffect(() => {
|
||||
const fetchUser = async () => {
|
||||
@@ -372,8 +377,7 @@ export default function PopNavDialog(props:any) {
|
||||
<div className="ml-auto flex items-center">
|
||||
<div className="hidden lg:flex lg:flex-1 lg:items-center lg:justify-end lg:space-x-6">
|
||||
<a
|
||||
href= {linkPath}
|
||||
className="text-sm font-medium text-gray-700 hover:text-gray-800" >
|
||||
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"}
|
||||
</a>
|
||||
<span aria-hidden="true" className="h-6 w-px bg-gray-200" />
|
||||
|
||||
@@ -70,12 +70,14 @@ export default async function AccountsTable( props: any ) {
|
||||
<tr key={item.uuid}>
|
||||
<td className="whitespace-wrap flex items-center py-4 pl-4 pr-3 text-xs font-medium text-gray-900 ">
|
||||
|
||||
<Link href={`/UserProfile/${item.uuid}`}><span className="pl-2"> {item.first_name}</span></Link>
|
||||
<Link href={`/UserProfile/${item.id}`}><span className="pl-2"> {item.email}</span></Link>
|
||||
</td>
|
||||
<td className="whitespace-nowrap px-3 py-4 text-xs text-gray-900">
|
||||
{item.first_name}
|
||||
</td>
|
||||
<td className="whitespace-nowrap px-3 py-4 text-xs text-gray-900">
|
||||
{item.last_name}
|
||||
</td>
|
||||
|
||||
|
||||
<td className="whitespace-nowrap px-3 py-4 text-xs text-gray-900">
|
||||
<button
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'dotenv/config';
|
||||
import { drizzle } from 'drizzle-orm/node-postgres';
|
||||
|
||||
import { Pool } from 'pg';
|
||||
|
||||
// db/index.ts
|
||||
|
||||
@@ -404,7 +404,7 @@ export const accounts = pgTable("accounts", {
|
||||
}
|
||||
);
|
||||
|
||||
/* export const vw_accounts = pgView("vw_accounts", {
|
||||
/* export const vw_accounts = pgView("vw_accounts", {
|
||||
uuid: uuid().defaultRandom(),
|
||||
userId: text("user_id").notNull(),
|
||||
type: text().notNull(),
|
||||
@@ -420,7 +420,7 @@ export const accounts = pgTable("accounts", {
|
||||
first_name: text("first_name"),
|
||||
last_name: text("last_name"),
|
||||
|
||||
},).existing(); */
|
||||
},) */
|
||||
|
||||
/* From here down is the authentication library Lusia tables */
|
||||
|
||||
@@ -430,7 +430,6 @@ export const users = pgTable("users",
|
||||
name: varchar("name"),
|
||||
username: varchar({ length: 50 }),
|
||||
discordId: varchar("discord_id", { length: 255 }).unique(),
|
||||
password_hash: varchar("password_hash", { length: 255 }),
|
||||
email: varchar("email", { length: 255 }).unique().notNull(),
|
||||
emailVerified: boolean("email_verified").default(false).notNull(),
|
||||
hashedPassword: varchar("hashed_password", { length: 255 }),
|
||||
@@ -469,6 +468,8 @@ export const sessions = pgTable(
|
||||
id: varchar("id", { length: 255 }).primaryKey(),
|
||||
userId: varchar("user_id", { length: 21 }).notNull(),
|
||||
expiresAt: timestamp("expires_at", { withTimezone: true, mode: "date" }).notNull(),
|
||||
createdAt: timestamp("created_at", { mode: 'string' }).default(sql`CURRENT_TIMESTAMP`),
|
||||
updatedAt: timestamp("updated_at", { mode: 'string' }).default(sql`CURRENT_TIMESTAMP`),
|
||||
},
|
||||
(t) => ({
|
||||
userIdx: index("session_user_idx").on(t.userId),
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Discord } from "arctic";
|
||||
import { DrizzlePostgreSQLAdapter } from "@lucia-auth/adapter-drizzle";
|
||||
import { env } from "@/env.js";
|
||||
import { db } from "@/server/db";
|
||||
//import { db } from "@db/index";
|
||||
import { sessions, users, type User as DbUser } from "@schemas/schema";
|
||||
import { absoluteUrl } from "@/lib/utils"
|
||||
|
||||
@@ -26,7 +27,7 @@ export const lucia = new Lucia(adapter, {
|
||||
updatedAt: attributes.updatedAt,
|
||||
};
|
||||
},
|
||||
sessionExpiresIn: new TimeSpan(30, "d"),
|
||||
sessionExpiresIn: new TimeSpan(1, "d"),
|
||||
sessionCookie: {
|
||||
name: "session",
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { drizzle } from "drizzle-orm/postgres-js";
|
||||
import postgres from "postgres";
|
||||
import { env } from "@/env";
|
||||
import * as schema from "@schemas/schema";
|
||||
import * as schema from "./schema";
|
||||
|
||||
export const connection = postgres(env.DATABASE_URL, {
|
||||
max_lifetime: 10, // Remove this line if you're deploying to Docker / VPS
|
||||
|
||||
Reference in New Issue
Block a user