refactor(profile): changed to patch vs posting

This commit is contained in:
2025-04-23 15:25:46 -05:00
parent 5a48dcf553
commit 57d3727f49

View File

@@ -7,114 +7,124 @@ import { createLog } from "../../../logger/logger.js";
const app = new OpenAPIHono(); const app = new OpenAPIHono();
const UserSchema = z.object({ const UserSchema = z.object({
password: z password: z
.string() .string()
.min(6, { message: "Passwords must be longer than 3 characters" }) .min(6, { message: "Passwords must be longer than 3 characters" })
.regex(/[A-Z]/, { .regex(/[A-Z]/, {
message: "Password must contain at least one uppercase letter", message: "Password must contain at least one uppercase letter",
}) })
.regex(/[\W_]/, { .regex(/[\W_]/, {
message: "Password must contain at least one special character", message: "Password must contain at least one special character",
}) })
.openapi({ example: "Password1!" }), .openapi({ example: "Password1!" }),
}); });
app.openapi( app.openapi(
createRoute({ createRoute({
tags: ["auth:user"], tags: ["auth:user"],
summary: "Updates a users Profile", summary: "Updates a users Profile",
description: "Currently you can only update your password over the API", description: "Currently you can only update your password over the API",
method: "post", method: "patch",
path: "/profile", path: "/profile",
middleware: authMiddleware, middleware: authMiddleware,
request: { request: {
body: { body: {
content: { content: {
"application/json": { schema: UserSchema }, "application/json": { schema: UserSchema },
},
},
}, },
}, responses: {
}, 200: {
responses: { content: {
200: { "application/json": {
content: { schema: z.object({
"application/json": { message: z.string().optional().openapi({
schema: z.object({ example: "User Profile has been updated",
message: z }),
.string() }),
.optional() },
.openapi({ example: "User Profile has been updated" }), },
}), description: "Sucess return",
}, },
401: {
content: {
"application/json": {
schema: z.object({
message: z
.string()
.optional()
.openapi({ example: "Unauthenticated" }),
}),
},
},
description: "Unauthorized",
},
500: {
content: {
"application/json": {
schema: z.object({
message: z
.string()
.optional()
.openapi({ example: "Internal Server error" }),
}),
},
},
description: "Internal Server Error",
},
}, },
description: "Sucess return", }),
}, async (c) => {
401: { // make sure we have a vaid user being accessed thats really logged in
content: { const authHeader = c.req.header("Authorization");
"application/json": {
schema: z.object({
message: z
.string()
.optional()
.openapi({ example: "Unauthenticated" }),
}),
},
},
description: "Unauthorized",
},
500: {
content: {
"application/json": {
schema: z.object({
message: z
.string()
.optional()
.openapi({ example: "Internal Server error" }),
}),
},
},
description: "Internal Server Error",
},
},
}),
async (c) => {
// make sure we have a vaid user being accessed thats really logged in
const authHeader = c.req.header("Authorization");
if (authHeader?.includes("Basic")) { if (authHeader?.includes("Basic")) {
return c.json( return c.json(
{ message: "You are a Basic user! Please login to get a token" }, {
401 message:
); "You are a Basic user! Please login to get a token",
},
401
);
}
if (!authHeader) {
return c.json({ success: false, message: "Unauthorized" }, 401);
}
const token = authHeader?.split("Bearer ")[1] || "";
let user;
try {
const payload = await verify(token, process.env.JWT_SECRET!);
user = payload.user;
} catch (error) {
createLog(
"error",
"lst",
"auth",
"Failed session check, user must be logged out"
);
return c.json({ success: false, message: "Unauthorized" }, 401);
}
// now pass all the data over to update the user info
try {
const data = await c?.req.json();
await updateProfile(user, data, token);
return c.json({
success: true,
message: "Your profile has been updated",
});
} catch (error) {
console.log(error);
return c.json({
success: false,
message: "There was an error",
error,
});
}
} }
if (!authHeader) {
return c.json({ message: "Unauthorized" }, 401);
}
const token = authHeader?.split("Bearer ")[1] || "";
let user;
try {
const payload = await verify(token, process.env.JWT_SECRET!);
user = payload.user;
} catch (error) {
createLog(
"error",
"lst",
"auth",
"Failed session check, user must be logged out"
);
return c.json({ message: "Unauthorized" }, 401);
}
// now pass all the data over to update the user info
try {
const data = await c?.req.json();
await updateProfile(user, data, token);
return c.json({ message: "Your profile has been updated" });
} catch (error) {
return c.json({ message: "There was an error", error });
}
}
); );
export default app; export default app;