mirror of
https://github.com/ajnart/homarr.git
synced 2026-01-30 11:19:12 +01:00
* chore: add initial db migration * test: add unit tests for packages auth, common, widgets * fix: deep source issues * fix: format issues * wip: add unit tests for api routers * fix: deep source issues * test: add missing unit tests for integration router * wip: board tests * test: add unit tests for board router * fix: remove unnecessary null assertions * fix: deepsource issues * fix: formatting * fix: pnpm lock * fix: lint and typecheck issues * chore: address pull request feedback * fix: non-null assertions * fix: lockfile broken
62 lines
1.4 KiB
TypeScript
62 lines
1.4 KiB
TypeScript
import { cookies } from "next/headers";
|
|
import type { Adapter } from "@auth/core/adapters";
|
|
import type { NextAuthConfig } from "next-auth";
|
|
|
|
import {
|
|
expireDateAfter,
|
|
generateSessionToken,
|
|
sessionMaxAgeInSeconds,
|
|
sessionTokenCookieName,
|
|
} from "./session";
|
|
|
|
export const sessionCallback: NextAuthCallbackOf<"session"> = ({
|
|
session,
|
|
user,
|
|
}) => ({
|
|
...session,
|
|
user: {
|
|
...session.user,
|
|
id: user.id,
|
|
name: user.name,
|
|
},
|
|
});
|
|
|
|
export const createSignInCallback =
|
|
(
|
|
adapter: Adapter,
|
|
isCredentialsRequest: boolean,
|
|
): NextAuthCallbackOf<"signIn"> =>
|
|
async ({ user }) => {
|
|
if (!isCredentialsRequest) return true;
|
|
|
|
if (!user) return true;
|
|
|
|
// https://github.com/nextauthjs/next-auth/issues/6106
|
|
if (!adapter?.createSession) {
|
|
return false;
|
|
}
|
|
|
|
const sessionToken = generateSessionToken();
|
|
const sessionExpiry = expireDateAfter(sessionMaxAgeInSeconds);
|
|
|
|
await adapter.createSession({
|
|
sessionToken,
|
|
userId: user.id!,
|
|
expires: sessionExpiry,
|
|
});
|
|
|
|
cookies().set(sessionTokenCookieName, sessionToken, {
|
|
path: "/",
|
|
expires: sessionExpiry,
|
|
httpOnly: true,
|
|
sameSite: "lax",
|
|
secure: true,
|
|
});
|
|
|
|
return true;
|
|
};
|
|
|
|
type NextAuthCallbackRecord = Exclude<NextAuthConfig["callbacks"], undefined>;
|
|
export type NextAuthCallbackOf<TKey extends keyof NextAuthCallbackRecord> =
|
|
Exclude<NextAuthCallbackRecord[TKey], undefined>;
|