lucia authentication

This commit is contained in:
2025-01-28 15:09:06 -05:00
parent 84e5c9e399
commit 0ab4c88264
121 changed files with 5613 additions and 66 deletions

55
src/lib/auth/index.ts Normal file
View File

@@ -0,0 +1,55 @@
import { Lucia, TimeSpan } from "lucia";
import { Discord } from "arctic";
import { DrizzlePostgreSQLAdapter } from "@lucia-auth/adapter-drizzle";
import { env } from "@/env.js";
import { db } from "@/server/db";
import { sessions, users, type User as DbUser } from "@schemas/schema";
import { absoluteUrl } from "@/lib/utils"
// Uncomment the following lines if you are using nodejs 18 or lower. Not required in Node.js 20, CloudFlare Workers, Deno, Bun, and Vercel Edge Functions.
// import { webcrypto } from "node:crypto";
// globalThis.crypto = webcrypto as Crypto;
const adapter = new DrizzlePostgreSQLAdapter(db, sessions, users);
export const lucia = new Lucia(adapter, {
getSessionAttributes: (/* attributes */) => {
return {};
},
getUserAttributes: (attributes) => {
return {
id: attributes.id,
email: attributes.email,
emailVerified: attributes.emailVerified,
avatar: attributes.avatar,
createdAt: attributes.createdAt,
updatedAt: attributes.updatedAt,
};
},
sessionExpiresIn: new TimeSpan(30, "d"),
sessionCookie: {
name: "session",
expires: false, // session cookies have very long lifespan (2 years)
attributes: {
secure: env.NODE_ENV === "production",
},
},
});
export const discord = new Discord(
env.DISCORD_CLIENT_ID,
env.DISCORD_CLIENT_SECRET,
absoluteUrl("/login/discord/callback")
);
declare module "lucia" {
interface Register {
Lucia: typeof lucia;
DatabaseSessionAttributes: DatabaseSessionAttributes;
DatabaseUserAttributes: DatabaseUserAttributes;
}
}
interface DatabaseSessionAttributes {}
interface DatabaseUserAttributes extends Omit<DbUser, "hashedPassword"> {}