test: add initial unit tests (#56)
* 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
This commit is contained in:
@@ -5,55 +5,23 @@ import Credentials from "next-auth/providers/credentials";
|
||||
|
||||
import { db } from "@homarr/db";
|
||||
|
||||
import { credentialsConfiguration } from "./providers/credentials";
|
||||
import { createSignInCallback, sessionCallback } from "./callbacks";
|
||||
import { createCredentialsConfiguration } from "./providers/credentials";
|
||||
import { EmptyNextAuthProvider } from "./providers/empty";
|
||||
import { expireDateAfter, generateSessionToken } from "./session";
|
||||
import { sessionMaxAgeInSeconds, sessionTokenCookieName } from "./session";
|
||||
|
||||
const adapter = DrizzleAdapter(db);
|
||||
const sessionMaxAgeInSeconds = 30 * 24 * 60 * 60; // 30 days
|
||||
|
||||
export const createConfiguration = (isCredentialsRequest: boolean) =>
|
||||
NextAuth({
|
||||
adapter,
|
||||
providers: [Credentials(credentialsConfiguration), EmptyNextAuthProvider()],
|
||||
providers: [
|
||||
Credentials(createCredentialsConfiguration(db)),
|
||||
EmptyNextAuthProvider(),
|
||||
],
|
||||
callbacks: {
|
||||
session: ({ session, user }) => ({
|
||||
...session,
|
||||
user: {
|
||||
...session.user,
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
},
|
||||
}),
|
||||
signIn: async ({ user }) => {
|
||||
if (!isCredentialsRequest) return true;
|
||||
|
||||
if (!user) return true;
|
||||
|
||||
const sessionToken = generateSessionToken();
|
||||
const sessionExpiry = expireDateAfter(sessionMaxAgeInSeconds);
|
||||
|
||||
// https://github.com/nextauthjs/next-auth/issues/6106
|
||||
if (!adapter?.createSession) {
|
||||
return false;
|
||||
}
|
||||
|
||||
await adapter.createSession({
|
||||
sessionToken: sessionToken,
|
||||
userId: user.id!,
|
||||
expires: sessionExpiry,
|
||||
});
|
||||
|
||||
cookies().set("next-auth.session-token", sessionToken, {
|
||||
path: "/",
|
||||
expires: sessionExpiry,
|
||||
httpOnly: true,
|
||||
sameSite: "lax",
|
||||
secure: true,
|
||||
});
|
||||
|
||||
return true;
|
||||
},
|
||||
session: sessionCallback,
|
||||
signIn: createSignInCallback(adapter, isCredentialsRequest),
|
||||
},
|
||||
session: {
|
||||
strategy: "database",
|
||||
@@ -65,7 +33,7 @@ export const createConfiguration = (isCredentialsRequest: boolean) =>
|
||||
},
|
||||
jwt: {
|
||||
encode() {
|
||||
const cookie = cookies().get("next-auth.session-token")?.value;
|
||||
const cookie = cookies().get(sessionTokenCookieName)?.value;
|
||||
return cookie ?? "";
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user