mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
49 lines
1.1 KiB
TypeScript
49 lines
1.1 KiB
TypeScript
import type { IncomingMessage } from "node:http";
|
|
import { TimeSpan } from "lucia";
|
|
import { Lucia } from "lucia/dist/core.js";
|
|
import { type ReturnValidateToken, adapter } from "./auth";
|
|
|
|
export const luciaToken = new Lucia(adapter, {
|
|
sessionCookie: {
|
|
attributes: {
|
|
secure: false,
|
|
},
|
|
},
|
|
sessionExpiresIn: new TimeSpan(365, "d"),
|
|
getUserAttributes: (attributes) => {
|
|
return {
|
|
email: attributes.email,
|
|
rol: attributes.rol,
|
|
secret: attributes.secret !== null,
|
|
};
|
|
},
|
|
});
|
|
|
|
export const validateBearerToken = async (
|
|
req: IncomingMessage,
|
|
): ReturnValidateToken => {
|
|
const authorizationHeader = req.headers.authorization;
|
|
const sessionId = luciaToken.readBearerToken(authorizationHeader ?? "");
|
|
if (!sessionId) {
|
|
return {
|
|
user: null,
|
|
session: null,
|
|
};
|
|
}
|
|
const result = await luciaToken.validateSession(sessionId);
|
|
return {
|
|
session: result.session,
|
|
...((result.user && {
|
|
user: {
|
|
authId: result.user.id,
|
|
email: result.user.email,
|
|
rol: result.user.rol,
|
|
id: result.user.id,
|
|
secret: result.user.secret,
|
|
},
|
|
}) || {
|
|
user: null,
|
|
}),
|
|
};
|
|
};
|