2024-07-21 19:05:48 +00:00
|
|
|
// lib/server/lucia.ts
|
|
|
|
|
import { Lucia, TimeSpan } from 'lucia';
|
|
|
|
|
import { DrizzlePostgreSQLAdapter } from '@lucia-auth/adapter-drizzle';
|
2024-08-15 23:25:41 +00:00
|
|
|
import { db } from '../database';
|
|
|
|
|
import { sessionsTable, usersTable } from '../database/tables';
|
2024-07-21 19:05:48 +00:00
|
|
|
import { config } from '../../common/config';
|
|
|
|
|
|
|
|
|
|
const adapter = new DrizzlePostgreSQLAdapter(db, sessionsTable, usersTable);
|
|
|
|
|
|
|
|
|
|
export const lucia = new Lucia(adapter, {
|
|
|
|
|
getSessionAttributes: (attributes) => {
|
|
|
|
|
return {
|
|
|
|
|
ipCountry: attributes.ip_country,
|
|
|
|
|
ipAddress: attributes.ip_address,
|
|
|
|
|
isTwoFactorAuthEnabled: attributes.twoFactorAuthEnabled,
|
|
|
|
|
isTwoFactorAuthenticated: attributes.isTwoFactorAuthenticated,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
getUserAttributes: (attributes) => {
|
|
|
|
|
return {
|
2024-08-15 23:25:41 +00:00
|
|
|
// ...attributes,
|
|
|
|
|
username: attributes.username,
|
|
|
|
|
email: attributes.email,
|
2024-08-23 02:26:22 +00:00
|
|
|
firstName: attributes.first_name,
|
|
|
|
|
lastName: attributes.last_name,
|
2024-08-15 23:25:41 +00:00
|
|
|
theme: attributes.theme,
|
2024-07-21 19:05:48 +00:00
|
|
|
};
|
|
|
|
|
},
|
2024-08-15 01:07:50 +00:00
|
|
|
sessionExpiresIn: new TimeSpan(2, 'w'), // 2 weeks
|
2024-07-21 19:05:48 +00:00
|
|
|
sessionCookie: {
|
|
|
|
|
name: 'session',
|
|
|
|
|
expires: false, // session cookies have very long lifespan (2 years)
|
|
|
|
|
attributes: {
|
|
|
|
|
// set to `true` when using HTTPS
|
|
|
|
|
secure: config.isProduction,
|
|
|
|
|
sameSite: 'strict',
|
|
|
|
|
domain: config.domain,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
declare module 'lucia' {
|
|
|
|
|
interface Register {
|
|
|
|
|
Lucia: typeof lucia;
|
|
|
|
|
DatabaseUserAttributes: DatabaseUserAttributes;
|
|
|
|
|
DatabaseSessionAttributes: DatabaseSessionAttributes;
|
|
|
|
|
}
|
|
|
|
|
interface DatabaseSessionAttributes {
|
|
|
|
|
ip_country: string;
|
|
|
|
|
ip_address: string;
|
|
|
|
|
twoFactorAuthEnabled: boolean;
|
|
|
|
|
isTwoFactorAuthenticated: boolean;
|
|
|
|
|
}
|
|
|
|
|
interface DatabaseUserAttributes {
|
|
|
|
|
username: string;
|
|
|
|
|
email: string;
|
2024-08-23 02:26:22 +00:00
|
|
|
first_name: string;
|
|
|
|
|
last_name: string;
|
2024-07-21 19:05:48 +00:00
|
|
|
theme: string;
|
|
|
|
|
}
|
|
|
|
|
}
|