mirror of
https://gitea.gofwd.group/dstrawsb/ballistic-builder.git
synced 2025-12-06 10:46:44 -05:00
lucia authentication
This commit is contained in:
55
src/lib/auth/index.ts
Normal file
55
src/lib/auth/index.ts
Normal 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"> {}
|
||||
Reference in New Issue
Block a user