ich geh behindert

This commit is contained in:
2025-06-05 01:34:10 +02:00
parent 0ae23e5272
commit 375c48d72f
478 changed files with 11113 additions and 231267 deletions

View File

@@ -0,0 +1,72 @@
import type { UserRole } from "@/server/auth/permissions";
import { db } from "@/server/db";
import { sessions, users } from "@/server/db/schema";
import { env } from "@/utils/env";
import { DrizzleSQLiteAdapter } from "@lucia-auth/adapter-drizzle";
import { Lucia, type RegisteredDatabaseUserAttributes, type Session } from "lucia";
import { cookies } from "next/headers";
import { cache } from "react";
const adapter = new DrizzleSQLiteAdapter(db, sessions, users);
export const lucia = new Lucia(adapter, {
sessionCookie: {
expires: false,
attributes: {
secure: env.RUNTIME_ENVIRONMENT === "prod",
},
},
getUserAttributes: (attributes) => {
return {
id: attributes.id,
username: attributes.username,
displayName: attributes.displayName,
email: attributes.email,
role: attributes.role,
};
},
});
export const validateRequest = cache(
async (): Promise<{ user: RegisteredDatabaseUserAttributes; session: Session } | { user: null; session: null }> => {
const sessionId = cookies().get(lucia.sessionCookieName)?.value ?? null;
if (!sessionId) {
return {
user: null,
session: null,
};
}
const result = await lucia.validateSession(sessionId);
// next.js throws when you attempt to set cookie when rendering page
try {
if (result.session?.fresh) {
const sessionCookie = lucia.createSessionCookie(result.session.id);
cookies().set(sessionCookie.name, sessionCookie.value, sessionCookie.attributes);
}
if (!result.session) {
const sessionCookie = lucia.createBlankSessionCookie();
cookies().set(sessionCookie.name, sessionCookie.value, sessionCookie.attributes);
}
} catch {}
return result as {
user: RegisteredDatabaseUserAttributes;
session: Session;
};
},
);
declare module "lucia" {
interface Register {
Lucia: typeof Lucia;
DatabaseUserAttributes: {
id: string;
github_id: number;
username: string;
displayName: string;
email: string;
role: UserRole;
};
}
}

View File

@@ -0,0 +1,13 @@
import { env } from "@/utils/env";
import { GitHub } from "arctic";
export const github = new GitHub(env.OAUTH.CLIENT_ID, env.OAUTH.CLIENT_SECRET, {
enterpriseDomain: "https://git.i.mercedes-benz.com",
});
export interface GitHubUserResult {
id: number;
login: string;
name: string;
email: string;
}

View File

@@ -0,0 +1,28 @@
import type { RegisteredDatabaseUserAttributes } from "lucia";
export enum UserRole {
ADMIN = "admin",
USER = "user",
GUEST = "guest",
}
/**
* @deprecated
*/
export function hasRole(user: RegisteredDatabaseUserAttributes | null | undefined, role: UserRole) {
return user?.role === role;
}
/**
* @deprecated
*/
export function translateUserRole(role: UserRole) {
switch (role) {
case UserRole.ADMIN:
return "Administrator";
case UserRole.USER:
return "Benutzer";
case UserRole.GUEST:
return "Gast";
}
}