60 lines
1.3 KiB
TypeScript
60 lines
1.3 KiB
TypeScript
import Elysia, { t } from "elysia";
|
|
import { generateToken, getUserData, updateUser } from "./auth.controller";
|
|
import { verifyAuth } from "../../middlewares/auth.middlewares";
|
|
|
|
export const authRoute = new Elysia({
|
|
prefix: "/api/auth",
|
|
tags: ["Auth"],
|
|
detail: {
|
|
description: "Routes for managing users",
|
|
},
|
|
});
|
|
|
|
authRoute.get(
|
|
"/user/:userId",
|
|
async ({ params: { userId } }) => await getUserData(userId),
|
|
{
|
|
params: t.Object({
|
|
userId: t.String(),
|
|
}),
|
|
}
|
|
);
|
|
|
|
authRoute.post(
|
|
"/user/update/:userId",
|
|
async ({ params: { userId }, body }) => await updateUser(userId, body),
|
|
{
|
|
params: t.Object({
|
|
userId: t.String(),
|
|
}),
|
|
body: t.Object({
|
|
paid_status: t.String(),
|
|
package_expire_date: t.String(),
|
|
}),
|
|
}
|
|
);
|
|
|
|
authRoute.get(
|
|
"/generate-token/:userId",
|
|
async (context) => await generateToken(context)
|
|
);
|
|
|
|
authRoute.get("/user/me", async ({ cookie }) => {
|
|
const authData = await verifyAuth(cookie);
|
|
if (authData.status !== 200) {
|
|
return authData;
|
|
} else {
|
|
const userId: string | any = authData.userId;
|
|
const response = await getUserData(userId);
|
|
if (response?.status === 200) {
|
|
return {
|
|
...response.data,
|
|
token: authData.token,
|
|
status: 200,
|
|
message: "User data fetched successfully",
|
|
};
|
|
} else {
|
|
return response;
|
|
}
|
|
}
|
|
});
|