forked from atums.world/atums.world
add s3 and local dir support, fix session roles, make sure delete invite checks if its valid first
This commit is contained in:
parent
9a91f1e7e3
commit
774c8e22ce
11 changed files with 171 additions and 12 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
import { resolve } from "path";
|
||||||
|
|
||||||
export const environment: Environment = {
|
export const environment: Environment = {
|
||||||
port: parseInt(process.env.PORT || "8080", 10),
|
port: parseInt(process.env.PORT || "8080", 10),
|
||||||
host: process.env.HOST || "0.0.0.0",
|
host: process.env.HOST || "0.0.0.0",
|
||||||
|
@ -25,3 +27,11 @@ export const jwt: {
|
||||||
secret: process.env.JWT_SECRET || "",
|
secret: process.env.JWT_SECRET || "",
|
||||||
expiresIn: process.env.JWT_EXPIRES || "1d",
|
expiresIn: process.env.JWT_EXPIRES || "1d",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const dataType: { type: string; path: string | undefined } = {
|
||||||
|
type: process.env.DATASOURCE_TYPE || "local",
|
||||||
|
path:
|
||||||
|
process.env.DATASOURCE_TYPE === "local"
|
||||||
|
? resolve(process.env.DATASOURCE_LOCAL_DIRECTORY || "./uploads")
|
||||||
|
: undefined,
|
||||||
|
};
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { logger } from "@helpers/logger";
|
import { logger } from "@helpers/logger";
|
||||||
import { type ReservedSQL, sql } from "bun";
|
import { type ReservedSQL, sql } from "bun";
|
||||||
|
|
||||||
const defaultSettings: { key: string; value: string }[] = [
|
const defaultSettings: Setting[] = [
|
||||||
{ key: "default_role", value: "user" },
|
{ key: "default_role", value: "user" },
|
||||||
{ key: "default_timezone", value: "UTC" },
|
{ key: "default_timezone", value: "UTC" },
|
||||||
{ key: "server_timezone", value: "UTC" },
|
{ key: "server_timezone", value: "UTC" },
|
||||||
|
@ -114,10 +114,10 @@ export async function setSetting(
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await reservation`
|
await reservation`
|
||||||
INSERT INTO settings ("key", "value")
|
INSERT INTO settings ("key", "value", updated_at)
|
||||||
VALUES (${key}, ${value})
|
VALUES (${key}, ${value}, NOW())
|
||||||
ON CONFLICT ("key")
|
ON CONFLICT ("key")
|
||||||
DO UPDATE SET "value" = ${value};`;
|
DO UPDATE SET "value" = ${value}, "updated_at" = NOW();`;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(["Could not set the setting:", error as Error]);
|
logger.error(["Could not set the setting:", error as Error]);
|
||||||
throw error;
|
throw error;
|
||||||
|
|
|
@ -23,8 +23,8 @@ export async function authByToken(
|
||||||
if (!authorizationToken || !isUUID(authorizationToken)) return null;
|
if (!authorizationToken || !isUUID(authorizationToken)) return null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result: UserSession[] =
|
const result: User[] =
|
||||||
await reservation`SELECT id, username, email, roles, avatar, timezone, authorization_token FROM users WHERE authorization_token = ${authorizationToken};`;
|
await reservation`SELECT * FROM users WHERE authorization_token = ${authorizationToken};`;
|
||||||
|
|
||||||
if (result.length === 0) return null;
|
if (result.length === 0) return null;
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ export async function authByToken(
|
||||||
username: result[0].username,
|
username: result[0].username,
|
||||||
email: result[0].email,
|
email: result[0].email,
|
||||||
email_verified: result[0].email_verified,
|
email_verified: result[0].email_verified,
|
||||||
roles: result[0].roles,
|
roles: result[0].roles[0].split(","),
|
||||||
avatar: result[0].avatar,
|
avatar: result[0].avatar,
|
||||||
timezone: result[0].timezone,
|
timezone: result[0].timezone,
|
||||||
authorization_token: result[0].authorization_token,
|
authorization_token: result[0].authorization_token,
|
||||||
|
|
39
src/index.ts
39
src/index.ts
|
@ -1,5 +1,7 @@
|
||||||
|
import { dataType } from "@config/environment";
|
||||||
import { logger } from "@helpers/logger";
|
import { logger } from "@helpers/logger";
|
||||||
import { type ReservedSQL, sql } from "bun";
|
import { type ReservedSQL, s3, sql } from "bun";
|
||||||
|
import { existsSync, mkdirSync } from "fs";
|
||||||
import { readdir } from "fs/promises";
|
import { readdir } from "fs/promises";
|
||||||
import { resolve } from "path";
|
import { resolve } from "path";
|
||||||
|
|
||||||
|
@ -40,6 +42,41 @@ async function main(): Promise<void> {
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (dataType.type === "local" && dataType.path) {
|
||||||
|
if (!existsSync(dataType.path)) {
|
||||||
|
try {
|
||||||
|
mkdirSync(dataType.path);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error([
|
||||||
|
"Could not create datasource local directory",
|
||||||
|
error as Error,
|
||||||
|
]);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info([
|
||||||
|
"Using local datasource directory",
|
||||||
|
`${dataType.path}`,
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
await s3.write("test", "test");
|
||||||
|
await s3.delete("test");
|
||||||
|
|
||||||
|
logger.info([
|
||||||
|
"Connected to S3 with bucket",
|
||||||
|
`${process.env.S3_BUCKET}`,
|
||||||
|
]);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error([
|
||||||
|
"Could not establish a connection to S3 bucket:",
|
||||||
|
error as Error,
|
||||||
|
]);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await redis.initialize();
|
await redis.initialize();
|
||||||
serverHandler.initialize();
|
serverHandler.initialize();
|
||||||
await initializeDatabase();
|
await initializeDatabase();
|
||||||
|
|
|
@ -141,7 +141,7 @@ async function handler(
|
||||||
username: user.username,
|
username: user.username,
|
||||||
email: user.email,
|
email: user.email,
|
||||||
email_verified: user.email_verified,
|
email_verified: user.email_verified,
|
||||||
roles: user.roles,
|
roles: user.roles[0].split(","),
|
||||||
avatar: user.avatar,
|
avatar: user.avatar,
|
||||||
timezone: user.timezone,
|
timezone: user.timezone,
|
||||||
authorization_token: user.authorization_token,
|
authorization_token: user.authorization_token,
|
||||||
|
|
|
@ -202,7 +202,7 @@ async function handler(
|
||||||
username: user.username,
|
username: user.username,
|
||||||
email: user.email,
|
email: user.email,
|
||||||
email_verified: user.email_verified,
|
email_verified: user.email_verified,
|
||||||
roles: user.roles,
|
roles: user.roles[0].split(","),
|
||||||
avatar: user.avatar,
|
avatar: user.avatar,
|
||||||
timezone: user.timezone,
|
timezone: user.timezone,
|
||||||
authorization_token: user.authorization_token,
|
authorization_token: user.authorization_token,
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { isValidInvite } from "@config/sql/users";
|
||||||
import { type ReservedSQL, sql } from "bun";
|
import { type ReservedSQL, sql } from "bun";
|
||||||
|
|
||||||
import { logger } from "@/helpers/logger";
|
import { logger } from "@/helpers/logger";
|
||||||
|
@ -34,6 +35,19 @@ async function handler(request: ExtendedRequest): Promise<Response> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { valid, error } = isValidInvite(invite);
|
||||||
|
|
||||||
|
if (!valid && error) {
|
||||||
|
return Response.json(
|
||||||
|
{
|
||||||
|
success: false,
|
||||||
|
code: 400,
|
||||||
|
error: error,
|
||||||
|
},
|
||||||
|
{ status: 400 },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const reservation: ReservedSQL = await sql.reserve();
|
const reservation: ReservedSQL = await sql.reserve();
|
||||||
let inviteData: Invite | null = null;
|
let inviteData: Invite | null = null;
|
||||||
|
|
||||||
|
|
92
src/routes/api/settings/set.ts
Normal file
92
src/routes/api/settings/set.ts
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
import { setSetting } from "@config/sql/settings";
|
||||||
|
|
||||||
|
import { logger } from "@/helpers/logger";
|
||||||
|
|
||||||
|
const routeDef: RouteDef = {
|
||||||
|
method: "POST",
|
||||||
|
accepts: "application/json",
|
||||||
|
returns: "application/json",
|
||||||
|
needsBody: "json",
|
||||||
|
};
|
||||||
|
|
||||||
|
async function handler(
|
||||||
|
request: ExtendedRequest,
|
||||||
|
requestBody: unknown,
|
||||||
|
): Promise<Response> {
|
||||||
|
const { key, value } = requestBody as { key: string; value: string };
|
||||||
|
|
||||||
|
if (!request.session) {
|
||||||
|
return Response.json(
|
||||||
|
{
|
||||||
|
success: false,
|
||||||
|
code: 403,
|
||||||
|
error: "Unauthorized",
|
||||||
|
},
|
||||||
|
{ status: 403 },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!request.session.roles.includes("admin")) {
|
||||||
|
return Response.json(
|
||||||
|
{
|
||||||
|
success: false,
|
||||||
|
code: 403,
|
||||||
|
error: "Unauthorized",
|
||||||
|
},
|
||||||
|
{ status: 403 },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!key || !value) {
|
||||||
|
return Response.json(
|
||||||
|
{
|
||||||
|
success: false,
|
||||||
|
code: 400,
|
||||||
|
error: "Expected key and value",
|
||||||
|
},
|
||||||
|
{ status: 400 },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
typeof key !== "string" ||
|
||||||
|
(typeof value !== "string" &&
|
||||||
|
typeof value !== "boolean" &&
|
||||||
|
typeof value !== "number")
|
||||||
|
) {
|
||||||
|
return Response.json(
|
||||||
|
{
|
||||||
|
success: false,
|
||||||
|
code: 400,
|
||||||
|
error: "Expected key to be a string and value to be a string, boolean, or number",
|
||||||
|
},
|
||||||
|
{ status: 400 },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await setSetting(key, value);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(["Could not set the setting:", error as Error]);
|
||||||
|
|
||||||
|
return Response.json(
|
||||||
|
{
|
||||||
|
success: false,
|
||||||
|
code: 500,
|
||||||
|
error: "Failed to set setting",
|
||||||
|
},
|
||||||
|
{ status: 500 },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Response.json(
|
||||||
|
{
|
||||||
|
success: true,
|
||||||
|
code: 200,
|
||||||
|
message: "Setting set",
|
||||||
|
},
|
||||||
|
{ status: 200 },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export { handler, routeDef };
|
|
@ -23,6 +23,7 @@ class ServerHandler {
|
||||||
this.router = new FileSystemRouter({
|
this.router = new FileSystemRouter({
|
||||||
style: "nextjs",
|
style: "nextjs",
|
||||||
dir: "./src/routes",
|
dir: "./src/routes",
|
||||||
|
fileExtensions: [".ts"],
|
||||||
origin: `http://${this.host}:${this.port}`,
|
origin: `http://${this.host}:${this.port}`,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
5
types/config.d.ts
vendored
5
types/config.d.ts
vendored
|
@ -8,3 +8,8 @@ type UserValidation = {
|
||||||
check: { valid: boolean; error?: string };
|
check: { valid: boolean; error?: string };
|
||||||
field: string;
|
field: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type Setting = {
|
||||||
|
key: string;
|
||||||
|
value: string;
|
||||||
|
};
|
||||||
|
|
2
types/session.d.ts
vendored
2
types/session.d.ts
vendored
|
@ -21,7 +21,7 @@ type User = {
|
||||||
email_verified: boolean;
|
email_verified: boolean;
|
||||||
password: string;
|
password: string;
|
||||||
avatar: boolean;
|
avatar: boolean;
|
||||||
roles: string[];
|
roles: string;
|
||||||
timezone: string;
|
timezone: string;
|
||||||
invited_by: UUID;
|
invited_by: UUID;
|
||||||
created_at: Date;
|
created_at: Date;
|
||||||
|
|
Loading…
Add table
Reference in a new issue