mirror of
https://github.com/hex248/sprint.git
synced 2026-02-08 02:33:01 +00:00
improved /user/update
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
import type { UserRecord } from "@issue/shared";
|
import type { UserRecord } from "@issue/shared";
|
||||||
import type { AuthedRequest } from "../../auth/middleware";
|
import type { AuthedRequest } from "../../auth/middleware";
|
||||||
|
import { hashPassword } from "../../auth/utils";
|
||||||
import { getUserById } from "../../db/queries";
|
import { getUserById } from "../../db/queries";
|
||||||
|
|
||||||
// /user/update?id=1&name=NewName&passwordHash=NewHash&serverURL=NewURL
|
// /user/update?id=1&name=NewName&password=NewPassword
|
||||||
export default async function update(req: AuthedRequest) {
|
export default async function update(req: AuthedRequest) {
|
||||||
const url = new URL(req.url);
|
const url = new URL(req.url);
|
||||||
const id = url.searchParams.get("id");
|
const id = url.searchParams.get("id");
|
||||||
@@ -16,11 +17,14 @@ export default async function update(req: AuthedRequest) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const name = url.searchParams.get("name") || undefined;
|
const name = url.searchParams.get("name") || undefined;
|
||||||
const passwordHash = url.searchParams.get("passwordHash") || undefined;
|
const password = url.searchParams.get("password") || undefined;
|
||||||
const serverURL = url.searchParams.get("serverURL") || undefined;
|
let passwordHash: string | undefined;
|
||||||
|
if (password !== undefined) {
|
||||||
|
passwordHash = await hashPassword(password);
|
||||||
|
}
|
||||||
|
|
||||||
const { updateById } = await import("../../db/queries/users");
|
const { updateById } = await import("../../db/queries/users");
|
||||||
const updatedUser = await updateById(user.id, { name, passwordHash, serverURL });
|
const updatedUser = await updateById(user.id, { name, passwordHash });
|
||||||
|
|
||||||
if (!updatedUser) {
|
if (!updatedUser) {
|
||||||
return new Response("failed to update user", { status: 500 });
|
return new Response("failed to update user", { status: 500 });
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
export * as issue from "./issue";
|
export * as issue from "./issue";
|
||||||
export * as organisation from "./organisation";
|
export * as organisation from "./organisation";
|
||||||
export * as project from "./project";
|
export * as project from "./project";
|
||||||
|
export * as user from "./user";
|
||||||
|
|
||||||
export type ServerQueryInput = {
|
export type ServerQueryInput = {
|
||||||
onSuccess?: (data: any, res: Response) => void;
|
onSuccess?: (data: any, res: Response) => void;
|
||||||
|
|||||||
@@ -5,20 +5,17 @@ export async function update({
|
|||||||
id,
|
id,
|
||||||
name,
|
name,
|
||||||
password,
|
password,
|
||||||
serverURL,
|
|
||||||
onSuccess,
|
onSuccess,
|
||||||
onError,
|
onError,
|
||||||
}: {
|
}: {
|
||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
password: string;
|
password: string;
|
||||||
serverURL: string;
|
|
||||||
} & ServerQueryInput) {
|
} & ServerQueryInput) {
|
||||||
const url = new URL(`${getServerURL()}/user/update`);
|
const url = new URL(`${getServerURL()}/user/update`);
|
||||||
url.searchParams.set("id", `${id}`);
|
url.searchParams.set("id", `${id}`);
|
||||||
url.searchParams.set("name", name.trim());
|
url.searchParams.set("name", name.trim());
|
||||||
url.searchParams.set("password", password.trim());
|
url.searchParams.set("password", password.trim());
|
||||||
url.searchParams.set("serverURL", serverURL.trim());
|
|
||||||
|
|
||||||
const res = await fetch(url.toString(), {
|
const res = await fetch(url.toString(), {
|
||||||
headers: getAuthHeaders(),
|
headers: getAuthHeaders(),
|
||||||
|
|||||||
Reference in New Issue
Block a user