update
This commit is contained in:
parent
f3416f86f6
commit
27339ba1bd
2 changed files with 144 additions and 99 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { createClerkClient } from "@clerk/backend";
|
||||
import { ENV } from "../../config/env"
|
||||
import { ENV } from "../../config/env";
|
||||
import { users } from "../../db/schema";
|
||||
import { db } from "../../db";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
|
@ -7,7 +7,11 @@ import { eq } from "drizzle-orm";
|
|||
// @ts-ignore
|
||||
import jwt from "jsonwebtoken";
|
||||
|
||||
import { checkUserInDB, createUser, storeRefreshToken } from "../../helper/auth/auth.helper";
|
||||
import {
|
||||
checkUserInDB,
|
||||
createUser,
|
||||
storeRefreshToken,
|
||||
} from "../../helper/auth/auth.helper";
|
||||
import { verifyAuth } from "../../middlewares/auth.middlewares";
|
||||
|
||||
// Initialize Clerk with your API key
|
||||
|
|
@ -17,11 +21,10 @@ export const getUserData = async (userId: string) => {
|
|||
try {
|
||||
const [user, checkInDB] = await Promise.all([
|
||||
clerk.users.getUser(userId),
|
||||
checkUserInDB(userId)
|
||||
checkUserInDB(userId),
|
||||
]);
|
||||
|
||||
if (user && !checkInDB.found) {
|
||||
|
||||
// Validate and transform user data
|
||||
const userDBData = {
|
||||
id: user.id,
|
||||
|
|
@ -33,10 +36,18 @@ export const getUserData = async (userId: string) => {
|
|||
|
||||
const userData = await createUser(userDBData);
|
||||
|
||||
return { status: 200, message: "User retrieved successfully", data: userData };
|
||||
return {
|
||||
status: 200,
|
||||
message: "User retrieved successfully",
|
||||
data: userData,
|
||||
};
|
||||
}
|
||||
if (user && checkInDB.found) {
|
||||
return { status: 200, message: "User retrieved successfully", data: checkInDB };
|
||||
return {
|
||||
status: 200,
|
||||
message: "User retrieved successfully",
|
||||
data: checkInDB,
|
||||
};
|
||||
}
|
||||
if (!user) {
|
||||
return { status: 404, message: "User not found" };
|
||||
|
|
@ -47,20 +58,36 @@ export const getUserData = async (userId: string) => {
|
|||
}
|
||||
};
|
||||
|
||||
export const updateUser = async (id: string, body: {
|
||||
paid_status: string,
|
||||
package_expire_date: string,
|
||||
}) => {
|
||||
export const updateUser = async (
|
||||
id: string,
|
||||
body: {
|
||||
paid_status: string;
|
||||
package_expire_date: string;
|
||||
}
|
||||
) => {
|
||||
try {
|
||||
const updateUserData = await db.update(users).set({ paid_status: body?.paid_status, expires_in: body?.package_expire_date }).where(eq(users.id, id)).returning({ updatedId: users.id });
|
||||
|
||||
return { status: 200, message: "User updated successfully", updateUserData };
|
||||
const updateUserData = await db
|
||||
.update(users)
|
||||
.set({
|
||||
paid_status: body?.paid_status,
|
||||
expires_in: body?.package_expire_date,
|
||||
})
|
||||
.where(eq(users.id, id))
|
||||
.returning({ updatedId: users.id });
|
||||
|
||||
return {
|
||||
status: 200,
|
||||
message: "User updated successfully",
|
||||
updateUserData,
|
||||
};
|
||||
} catch (error: any) {
|
||||
console.error("Error in updateUser:", error.message || error.toString());
|
||||
return { status: 500, message: `An error occurred while updating the user` };
|
||||
return {
|
||||
status: 500,
|
||||
message: `An error occurred while updating the user`,
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const generateToken = async (context: any) => {
|
||||
try {
|
||||
|
|
@ -71,16 +98,24 @@ export const generateToken = async (context: any) => {
|
|||
if (access_cookie !== undefined || refresh_cookie !== undefined) {
|
||||
const verify = await verifyAuth(context?.cookie);
|
||||
return verify;
|
||||
}
|
||||
else if (access_cookie === undefined && refresh_cookie === undefined && userId !== undefined) {
|
||||
} else if (
|
||||
access_cookie === undefined &&
|
||||
refresh_cookie === undefined &&
|
||||
userId !== undefined
|
||||
) {
|
||||
const user = await checkUserInDB(userId);
|
||||
if (user?.found === true) {
|
||||
|
||||
// generate access token
|
||||
const accessToken = jwt.sign({ userId }, ENV.JWT_ACCESS_TOKEN_SECRET, { expiresIn: '3h' });
|
||||
const accessToken = jwt.sign({ userId }, ENV.JWT_ACCESS_TOKEN_SECRET, {
|
||||
expiresIn: "3h",
|
||||
});
|
||||
|
||||
// generate refresh token
|
||||
const refreshToken = jwt.sign({ userId }, ENV.JWT_REFRESH_TOKEN_SECRET, { expiresIn: '7d' });
|
||||
const refreshToken = jwt.sign(
|
||||
{ userId },
|
||||
ENV.JWT_REFRESH_TOKEN_SECRET,
|
||||
{ expiresIn: "7d" }
|
||||
);
|
||||
|
||||
// store refresh token in db
|
||||
const storeRToken = await storeRefreshToken(userId, refreshToken);
|
||||
|
|
@ -90,7 +125,7 @@ export const generateToken = async (context: any) => {
|
|||
value: accessToken,
|
||||
httpOnly: true,
|
||||
secure: true, // Set to true in production
|
||||
sameSite: 'none', // Adjust based on your needs
|
||||
sameSite: "none", // Adjust based on your needs
|
||||
path: "/",
|
||||
maxAge: 3 * 60 * 60, // 3 hours in seconds
|
||||
});
|
||||
|
|
@ -99,27 +134,33 @@ export const generateToken = async (context: any) => {
|
|||
value: refreshToken,
|
||||
httpOnly: true,
|
||||
secure: true, // Set to true in production
|
||||
sameSite: 'none', // Adjust based on your needs
|
||||
sameSite: "none", // Adjust based on your needs
|
||||
path: "/",
|
||||
maxAge: 7 * 24 * 60 * 60, // 7 days in seconds
|
||||
});
|
||||
|
||||
return { status: 201, message: "Token generated successfully", token: accessToken, user: user.user };
|
||||
return {
|
||||
status: 201,
|
||||
message: "Token generated successfully",
|
||||
token: accessToken,
|
||||
user: user.user,
|
||||
};
|
||||
}
|
||||
return { status: 500, message: "An error occurred while storing the refresh token" };
|
||||
}
|
||||
else {
|
||||
return {
|
||||
status: 500,
|
||||
message: "An error occurred while storing the refresh token",
|
||||
};
|
||||
} else {
|
||||
return { status: 404, message: "User not found" };
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return { status: 404, message: "Unauthorized!!!" };
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error("Error in generateToken:", error.message || error.toString());
|
||||
return { status: 500, message: `An error occurred while generating the token` };
|
||||
return {
|
||||
status: 500,
|
||||
message: `An error occurred while generating the token`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
|
|
|||
|
|
@ -44,5 +44,9 @@ const app = new Elysia()
|
|||
)
|
||||
.listen(ENV.SERVER_PORT);
|
||||
|
||||
app.routes.forEach((route) => {
|
||||
console.log(`Route: ${route.method} ${route.path}`);
|
||||
});
|
||||
|
||||
console.log(`🦊 Elysia is running at ${ENV.SERVER_URL}`);
|
||||
console.log(`Swagger docs available at ${ENV.SERVER_URL}/swagger`);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue