mirror of
https://github.com/ajnart/homarr.git
synced 2026-01-30 03:09:19 +01:00
* fix(deps): update dependency @auth/core to ^0.32.0 * fix(deps): update dependency @auth/core to ^0.32.0 * fix: type issues with next-auth types --------- Co-authored-by: homarr-renovate[bot] <158783068+homarr-renovate[bot]@users.noreply.github.com> Co-authored-by: Meier Lukas <meierschlumpf@gmail.com>
53 lines
1.2 KiB
TypeScript
53 lines
1.2 KiB
TypeScript
import { randomUUID } from "crypto";
|
|
import type { Session } from "next-auth";
|
|
|
|
import type { Database } from "@homarr/db";
|
|
|
|
import { getCurrentUserPermissionsAsync } from "./callbacks";
|
|
|
|
export const sessionMaxAgeInSeconds = 30 * 24 * 60 * 60; // 30 days
|
|
export const sessionTokenCookieName = "next-auth.session-token";
|
|
|
|
export const expireDateAfter = (seconds: number) => {
|
|
return new Date(Date.now() + seconds * 1000);
|
|
};
|
|
|
|
export const generateSessionToken = () => {
|
|
return randomUUID();
|
|
};
|
|
|
|
export const getSessionFromTokenAsync = async (db: Database, token: string | undefined): Promise<Session | null> => {
|
|
if (!token) {
|
|
return null;
|
|
}
|
|
|
|
const session = await db.query.sessions.findFirst({
|
|
where: ({ sessionToken }, { eq }) => eq(sessionToken, token),
|
|
columns: {
|
|
expires: true,
|
|
},
|
|
with: {
|
|
user: {
|
|
columns: {
|
|
id: true,
|
|
name: true,
|
|
email: true,
|
|
image: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
if (!session) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
user: {
|
|
...session.user,
|
|
permissions: await getCurrentUserPermissionsAsync(db, session.user.id),
|
|
},
|
|
expires: session.expires.toISOString(),
|
|
};
|
|
};
|