chore: update prettier configuration for print width (#519)
* feat: update prettier configuration for print width * chore: apply code formatting to entire repository * fix: remove build files * fix: format issue --------- Co-authored-by: Meier Lukas <meierschlumpf@gmail.com>
This commit is contained in:
@@ -7,17 +7,9 @@ import { eq, inArray } from "@homarr/db";
|
||||
import { groupMembers, groupPermissions } from "@homarr/db/schema/sqlite";
|
||||
import { getPermissionsWithChildren } from "@homarr/definitions";
|
||||
|
||||
import {
|
||||
expireDateAfter,
|
||||
generateSessionToken,
|
||||
sessionMaxAgeInSeconds,
|
||||
sessionTokenCookieName,
|
||||
} from "./session";
|
||||
import { expireDateAfter, generateSessionToken, sessionMaxAgeInSeconds, sessionTokenCookieName } from "./session";
|
||||
|
||||
export const getCurrentUserPermissionsAsync = async (
|
||||
db: Database,
|
||||
userId: string,
|
||||
) => {
|
||||
export const getCurrentUserPermissionsAsync = async (db: Database, userId: string) => {
|
||||
const dbGroupMembers = await db.query.groupMembers.findMany({
|
||||
where: eq(groupMembers.userId, userId),
|
||||
});
|
||||
@@ -27,19 +19,13 @@ export const getCurrentUserPermissionsAsync = async (
|
||||
permission: groupPermissions.permission,
|
||||
})
|
||||
.from(groupPermissions)
|
||||
.where(
|
||||
groupIds.length > 0
|
||||
? inArray(groupPermissions.groupId, groupIds)
|
||||
: undefined,
|
||||
);
|
||||
.where(groupIds.length > 0 ? inArray(groupPermissions.groupId, groupIds) : undefined);
|
||||
const permissionKeys = dbGroupPermissions.map(({ permission }) => permission);
|
||||
|
||||
return getPermissionsWithChildren(permissionKeys);
|
||||
};
|
||||
|
||||
export const createSessionCallback = (
|
||||
db: Database,
|
||||
): NextAuthCallbackOf<"session"> => {
|
||||
export const createSessionCallback = (db: Database): NextAuthCallbackOf<"session"> => {
|
||||
return async ({ session, user }) => {
|
||||
return {
|
||||
...session,
|
||||
@@ -54,10 +40,7 @@ export const createSessionCallback = (
|
||||
};
|
||||
|
||||
export const createSignInCallback =
|
||||
(
|
||||
adapter: Adapter,
|
||||
isCredentialsRequest: boolean,
|
||||
): NextAuthCallbackOf<"signIn"> =>
|
||||
(adapter: Adapter, isCredentialsRequest: boolean): NextAuthCallbackOf<"signIn"> =>
|
||||
async ({ user }) => {
|
||||
if (!isCredentialsRequest) return true;
|
||||
|
||||
@@ -89,5 +72,7 @@ export const createSignInCallback =
|
||||
};
|
||||
|
||||
type NextAuthCallbackRecord = Exclude<NextAuthConfig["callbacks"], undefined>;
|
||||
export type NextAuthCallbackOf<TKey extends keyof NextAuthCallbackRecord> =
|
||||
Exclude<NextAuthCallbackRecord[TKey], undefined>;
|
||||
export type NextAuthCallbackOf<TKey extends keyof NextAuthCallbackRecord> = Exclude<
|
||||
NextAuthCallbackRecord[TKey],
|
||||
undefined
|
||||
>;
|
||||
|
||||
@@ -28,10 +28,7 @@ export const createConfiguration = (isCredentialsRequest: boolean) =>
|
||||
},
|
||||
trustHost: true,
|
||||
adapter,
|
||||
providers: [
|
||||
Credentials(createCredentialsConfiguration(db)),
|
||||
EmptyNextAuthProvider(),
|
||||
],
|
||||
providers: [Credentials(createCredentialsConfiguration(db)), EmptyNextAuthProvider()],
|
||||
callbacks: {
|
||||
session: createSessionCallback(db),
|
||||
signIn: createSignInCallback(adapter, isCredentialsRequest),
|
||||
|
||||
@@ -3,15 +3,11 @@ import { z } from "zod";
|
||||
|
||||
export const env = createEnv({
|
||||
server: {
|
||||
AUTH_SECRET:
|
||||
process.env.NODE_ENV === "production"
|
||||
? z.string().min(1)
|
||||
: z.string().min(1).optional(),
|
||||
AUTH_SECRET: process.env.NODE_ENV === "production" ? z.string().min(1) : z.string().min(1).optional(),
|
||||
},
|
||||
client: {},
|
||||
runtimeEnv: {
|
||||
AUTH_SECRET: process.env.AUTH_SECRET,
|
||||
},
|
||||
skipValidation:
|
||||
Boolean(process.env.CI) || Boolean(process.env.SKIP_ENV_VALIDATION),
|
||||
skipValidation: Boolean(process.env.CI) || Boolean(process.env.SKIP_ENV_VALIDATION),
|
||||
});
|
||||
|
||||
@@ -17,10 +17,6 @@ declare module "@auth/core/types" {
|
||||
|
||||
export * from "./security";
|
||||
|
||||
export const createHandlers = (isCredentialsRequest: boolean) =>
|
||||
createConfiguration(isCredentialsRequest);
|
||||
export const createHandlers = (isCredentialsRequest: boolean) => createConfiguration(isCredentialsRequest);
|
||||
|
||||
export {
|
||||
getSessionFromTokenAsync as getSessionFromToken,
|
||||
sessionTokenCookieName,
|
||||
} from "./session";
|
||||
export { getSessionFromTokenAsync as getSessionFromToken, sessionTokenCookieName } from "./session";
|
||||
|
||||
@@ -19,24 +19,15 @@ export type BoardPermissionsProps = (
|
||||
isPublic: boolean;
|
||||
};
|
||||
|
||||
export const constructBoardPermissions = (
|
||||
board: BoardPermissionsProps,
|
||||
session: Session | null,
|
||||
) => {
|
||||
export const constructBoardPermissions = (board: BoardPermissionsProps, session: Session | null) => {
|
||||
const creatorId = "creator" in board ? board.creator?.id : board.creatorId;
|
||||
|
||||
return {
|
||||
hasFullAccess:
|
||||
session?.user?.id === creatorId ||
|
||||
session?.user.permissions.includes("board-full-access"),
|
||||
hasFullAccess: session?.user?.id === creatorId || session?.user.permissions.includes("board-full-access"),
|
||||
hasChangeAccess:
|
||||
session?.user?.id === creatorId ||
|
||||
board.userPermissions.some(
|
||||
({ permission }) => permission === "board-change",
|
||||
) ||
|
||||
board.groupPermissions.some(
|
||||
({ permission }) => permission === "board-change",
|
||||
) ||
|
||||
board.userPermissions.some(({ permission }) => permission === "board-change") ||
|
||||
board.groupPermissions.some(({ permission }) => permission === "board-change") ||
|
||||
session?.user.permissions.includes("board-modify-all"),
|
||||
hasViewAccess:
|
||||
session?.user?.id === creatorId ||
|
||||
|
||||
@@ -35,13 +35,8 @@ export const createCredentialsConfiguration = (db: Database) =>
|
||||
return null;
|
||||
}
|
||||
|
||||
console.log(
|
||||
`user ${user.name} is trying to log in. checking password...`,
|
||||
);
|
||||
const isValidPassword = await bcrypt.compare(
|
||||
data.password,
|
||||
user.password,
|
||||
);
|
||||
console.log(`user ${user.name} is trying to log in. checking password...`);
|
||||
const isValidPassword = await bcrypt.compare(data.password, user.password);
|
||||
|
||||
if (!isValidPassword) {
|
||||
console.log(`password for user ${user.name} was incorrect`);
|
||||
|
||||
@@ -26,13 +26,7 @@ describe("Credentials authorization", () => {
|
||||
expect(result).toEqual({ id: userId, name: "test" });
|
||||
});
|
||||
|
||||
const passwordsThatShouldNotAuthorize = [
|
||||
"wrong",
|
||||
"Test",
|
||||
"test ",
|
||||
" test",
|
||||
" test ",
|
||||
];
|
||||
const passwordsThatShouldNotAuthorize = ["wrong", "Test", "test ", " test", " test "];
|
||||
|
||||
passwordsThatShouldNotAuthorize.forEach((password) => {
|
||||
it(`should not authorize user with incorrect credentials (${password})`, async () => {
|
||||
|
||||
@@ -16,10 +16,7 @@ export const generateSessionToken = () => {
|
||||
return randomUUID();
|
||||
};
|
||||
|
||||
export const getSessionFromTokenAsync = async (
|
||||
db: Database,
|
||||
token: string | undefined,
|
||||
): Promise<Session | null> => {
|
||||
export const getSessionFromTokenAsync = async (db: Database, token: string | undefined): Promise<Session | null> => {
|
||||
if (!token) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -6,20 +6,11 @@ import type { Account, User } from "next-auth";
|
||||
import type { JWT } from "next-auth/jwt";
|
||||
import { describe, expect, it, test, vi } from "vitest";
|
||||
|
||||
import {
|
||||
groupMembers,
|
||||
groupPermissions,
|
||||
groups,
|
||||
users,
|
||||
} from "@homarr/db/schema/sqlite";
|
||||
import { groupMembers, groupPermissions, groups, users } from "@homarr/db/schema/sqlite";
|
||||
import { createDb } from "@homarr/db/test";
|
||||
import * as definitions from "@homarr/definitions";
|
||||
|
||||
import {
|
||||
createSessionCallback,
|
||||
createSignInCallback,
|
||||
getCurrentUserPermissionsAsync,
|
||||
} from "../callbacks";
|
||||
import { createSessionCallback, createSignInCallback, getCurrentUserPermissionsAsync } from "../callbacks";
|
||||
|
||||
describe("getCurrentUserPermissions", () => {
|
||||
test("should return empty permissions when non existing user requested", async () => {
|
||||
@@ -96,9 +87,7 @@ describe("session callback", () => {
|
||||
});
|
||||
});
|
||||
|
||||
type AdapterSessionInput = Parameters<
|
||||
Exclude<Adapter["createSession"], undefined>
|
||||
>[0];
|
||||
type AdapterSessionInput = Parameters<Exclude<Adapter["createSession"], undefined>>[0];
|
||||
|
||||
const createAdapter = () => {
|
||||
const result = {
|
||||
@@ -131,8 +120,7 @@ vi.mock("next/headers", async (importOriginal) => {
|
||||
const mod = await importOriginal<HeadersExport>();
|
||||
|
||||
const result = {
|
||||
set: (name: string, value: string, options: Partial<ResponseCookie>) =>
|
||||
options as ResponseCookie,
|
||||
set: (name: string, value: string, options: Partial<ResponseCookie>) => options as ResponseCookie,
|
||||
} as unknown as ReadonlyRequestCookies;
|
||||
|
||||
vi.spyOn(result, "set");
|
||||
@@ -145,10 +133,7 @@ vi.mock("next/headers", async (importOriginal) => {
|
||||
describe("createSignInCallback", () => {
|
||||
it("should return true if not credentials request", async () => {
|
||||
const isCredentialsRequest = false;
|
||||
const signInCallback = createSignInCallback(
|
||||
createAdapter(),
|
||||
isCredentialsRequest,
|
||||
);
|
||||
const signInCallback = createSignInCallback(createAdapter(), isCredentialsRequest);
|
||||
const result = await signInCallback({
|
||||
user: { id: "1", emailVerified: new Date("2023-01-13") },
|
||||
account: {} as Account,
|
||||
@@ -158,10 +143,7 @@ describe("createSignInCallback", () => {
|
||||
|
||||
it("should return true if no user", async () => {
|
||||
const isCredentialsRequest = true;
|
||||
const signInCallback = createSignInCallback(
|
||||
createAdapter(),
|
||||
isCredentialsRequest,
|
||||
);
|
||||
const signInCallback = createSignInCallback(createAdapter(), isCredentialsRequest);
|
||||
const result = await signInCallback({
|
||||
user: undefined as unknown as User,
|
||||
account: {} as Account,
|
||||
@@ -195,16 +177,12 @@ describe("createSignInCallback", () => {
|
||||
userId: user.id,
|
||||
expires: mockSessionExpiry,
|
||||
});
|
||||
expect(cookies().set).toHaveBeenCalledWith(
|
||||
"next-auth.session-token",
|
||||
mockSessionToken,
|
||||
{
|
||||
path: "/",
|
||||
expires: mockSessionExpiry,
|
||||
httpOnly: true,
|
||||
sameSite: "lax",
|
||||
secure: true,
|
||||
},
|
||||
);
|
||||
expect(cookies().set).toHaveBeenCalledWith("next-auth.session-token", mockSessionToken, {
|
||||
path: "/",
|
||||
expires: mockSessionExpiry,
|
||||
httpOnly: true,
|
||||
sameSite: "lax",
|
||||
secure: true,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -20,14 +20,11 @@ describe("expireDateAfter should calculate date after specified seconds", () =>
|
||||
["2023-07-01T00:00:00Z", 60 * 60 * 24 * 30, "2023-07-31T00:00:00Z"], // 30 days
|
||||
["2023-07-01T00:00:00Z", 60 * 60 * 24 * 365, "2024-06-30T00:00:00Z"], // 1 year
|
||||
["2023-07-01T00:00:00Z", 60 * 60 * 24 * 365 * 10, "2033-06-28T00:00:00Z"], // 10 years
|
||||
])(
|
||||
"should calculate date %s and after %i seconds to equal %s",
|
||||
(initialDate, seconds, expectedDate) => {
|
||||
vi.setSystemTime(new Date(initialDate));
|
||||
const result = expireDateAfter(seconds);
|
||||
expect(result).toEqual(new Date(expectedDate));
|
||||
},
|
||||
);
|
||||
])("should calculate date %s and after %i seconds to equal %s", (initialDate, seconds, expectedDate) => {
|
||||
vi.setSystemTime(new Date(initialDate));
|
||||
const result = expireDateAfter(seconds);
|
||||
expect(result).toEqual(new Date(expectedDate));
|
||||
});
|
||||
});
|
||||
|
||||
describe("generateSessionToken should return a random UUID", () => {
|
||||
|
||||
Reference in New Issue
Block a user