Estoy configurando Lucia para el manejo de sesiones de mi aplicacion, siguiendo los pasos de su pagina me encuentro que el middleware me da error: column users.id does not exist. Que justamente en mi tabla users el id esta con el nombre user_id. No se si hice la configuracion correctamente
auth.ts
import { Lucia } from "lucia";
import { NodePostgresAdapter } from "@lucia-auth/adapter-postgresql";
import { pool } from "./models/db.js";
const adapter = new NodePostgresAdapter(pool, {
user: "users",
session: "user_session"
});
export const lucia = new Lucia(adapter, {
sessionCookie: {
attributes: {
secure: import.meta.env.PROD
}
},
getUserAttributes: (attributes) => {
return {
user_id: attributes.user_id,
establishment_id: attributes.establishment_id
};
}
});
declare module "lucia" {
interface Register {
Lucia: typeof lucia;
}
}
middleware.ts
import { lucia } from "./auth";
import { verifyRequestOrigin } from "lucia";
import { defineMiddleware } from "astro:middleware";
export const onRequest = defineMiddleware(async (context, next) => {
if (context.request.method !== "GET") {
const originHeader = context.request.headers.get("Origin");
const hostHeader = context.request.headers.get("Host");
if (!originHeader || !hostHeader || !verifyRequestOrigin(originHeader, [hostHeader])) {
return new Response(null, {
status: 403
});
}
}
const sessionId = context.cookies.get(lucia.sessionCookieName)?.value ?? null;
if (!sessionId) {
context.locals.user = null;
context.locals.session = null;
return next();
}
const { session, user } = await lucia.validateSession(sessionId);
if (session && session.fresh) {
const sessionCookie = lucia.createSessionCookie(session.id);
context.cookies.set(sessionCookie.name, sessionCookie.value, sessionCookie.attributes);
}
if (!session) {
const sessionCookie = lucia.createBlankSessionCookie();
context.cookies.set(sessionCookie.name, sessionCookie.value, sessionCookie.attributes);
}
context.locals.session = session;
context.locals.user = user;
return next();
});
Viendo el /node_modules/@lucia-auth/adapter-postgresql/dist/base.js
async getUserFromSessionId(sessionId) {
const result = await this.controller.get(`SELECT ${this.escapedUserTableName}.* FROM ${this.escapedSessionTableName} INNER JOIN ${this.escapedUserTableName} ON ${this.escapedUserTableName}.id = ${this.escapedSessionTableName}.user_id WHERE ${this.escapedSessionTableName}.id = $1`, [sessionId]);
if (!result)
return null;
return transformIntoDatabaseUser(result);
}
El error parece ser en esa llamada que hace el ${this.escapedUserTableName}.id y deberia ser ${this.escapedUserTableName}.user_id.