31 lines
709 B
TypeScript
31 lines
709 B
TypeScript
import type { IncomingHttpHeaders } from 'node:http';
|
|
import { fromNodeHeaders } from 'better-auth/node';
|
|
|
|
export interface SocketSessionResult {
|
|
session: unknown;
|
|
user: { id: string };
|
|
}
|
|
|
|
export interface SessionAuth {
|
|
api: {
|
|
getSession(context: { headers: Headers }): Promise<SocketSessionResult | null>;
|
|
};
|
|
}
|
|
|
|
export async function validateSocketSession(
|
|
headers: IncomingHttpHeaders,
|
|
auth: SessionAuth,
|
|
): Promise<SocketSessionResult | null> {
|
|
const sessionHeaders = fromNodeHeaders(headers);
|
|
const result = await auth.api.getSession({ headers: sessionHeaders });
|
|
|
|
if (!result) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
session: result.session,
|
|
user: { id: result.user.id },
|
|
};
|
|
}
|