diff --git a/controllers/auth/auth.js b/controllers/auth/auth.js index 7e44a69..b5ca7a5 100644 --- a/controllers/auth/auth.js +++ b/controllers/auth/auth.js @@ -2,6 +2,7 @@ import response from "../../response.js"; import bcrypt from "bcryptjs"; import jwt from "jsonwebtoken"; import nodemailer from "nodemailer"; +import moment from "moment-timezone"; import models from "../../models/index.js"; const transporter = nodemailer.createTransport({ @@ -44,6 +45,7 @@ export const registerAdmin = async (req, res) => { EMAIL: EMAIL, PASSWORD: hashedPassword, ROLE: "admin", + IS_VALIDATED: 1, }); const adminResponse = { @@ -51,6 +53,7 @@ export const registerAdmin = async (req, res) => { NAME_USERS: newUser.NAME_USERS, EMAIL: newUser.EMAIL, ROLE: newUser.ROLE, + IS_VALIDATED: newUser.IS_VALIDATED, }; response(200, adminResponse, "Admin registration successful", res); @@ -119,10 +122,14 @@ export const registerTeacher = async (req, res) => { await transaction.commit(); + const now = moment().tz("Asia/Jakarta"); + const midnight = now.clone().endOf("day"); + const secondsUntilMidnight = midnight.diff(now, "seconds"); + const token = jwt.sign( { userId: newUser.ID }, process.env.VERIFY_TOKEN_SECRET, - { expiresIn: "1h" } + { expiresIn: secondsUntilMidnight } ); const validationLink = `${process.env.CLIENT_URL}/validate-email?token=${token}`; @@ -240,7 +247,7 @@ export const registerTeacher = async (req, res) => {
Important: This verification link will expire in 1 hour. If you don't complete the verification within this time, you'll need to register again.
+Important: This verification link will expire at 12:00 AM WIB. If you don't complete the verification by this time, you'll need to register again.
If you didn't create an account with SEALS, please ignore this email.
@@ -335,10 +342,14 @@ export const registerStudent = async (req, res) => { await transaction.commit(); + const now = moment().tz("Asia/Jakarta"); + const midnight = now.clone().endOf("day"); + const secondsUntilMidnight = midnight.diff(now, "seconds"); + const token = jwt.sign( { userId: newUser.ID }, process.env.VERIFY_TOKEN_SECRET, - { expiresIn: "1h" } + { expiresIn: secondsUntilMidnight } ); const validationLink = `${process.env.CLIENT_URL}/validate-email?token=${token}`; @@ -456,7 +467,7 @@ export const registerStudent = async (req, res) => {Important: This verification link will expire in 1 hour. If you don't complete the verification within this time, you'll need to register again.
+Important: This verification link will expire at 12:00 AM WIB. If you don't complete the verification by this time, you'll need to register again.
If you didn't create an account with SEALS, please ignore this email.
@@ -699,7 +710,12 @@ export const loginUser = async (req, res) => { } if (user.IS_VALIDATED !== 1) { - return response(403, null, "User is not validated! Please verify your email first.", res); + return response( + 403, + null, + "User is not validated! Please verify your email first.", + res + ); } const validPassword = await bcrypt.compare(PASSWORD, user.PASSWORD); diff --git a/index.js b/index.js index 7fac4df..08ca9e2 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ import router from "./routes/index.js"; import cookieParser from "cookie-parser"; import promBundle from "express-prom-bundle"; import cron from "node-cron"; +import moment from "moment-timezone"; import models from "./models/index.js"; dotenv.config(); @@ -35,10 +36,13 @@ app.use(cookieParser()); app.use(express.json()); app.use(express.urlencoded({ extended: true })); app.use(router); -app.use(express.static("public")); +app.use("/api", express.static("public")); +// cron.schedule("0 17 * * *", async () => { ini kalau timezone servernya UTC cron.schedule("0 0 * * *", async () => { - const oneDayAgo = new Date(Date.now() - 24 * 60 * 60 * 1000); + const now = moment().tz("Asia/Jakarta").format("YYYY-MM-DD HH:mm:ss"); + console.log(`Cron job executed at ${now} (WIB)`); + const transaction = await models.db.transaction(); try { @@ -46,7 +50,6 @@ cron.schedule("0 0 * * *", async () => { attributes: ["ID"], where: { IS_VALIDATED: 0, - TIME_USERS: { [models.Sequelize.Op.lt]: oneDayAgo }, }, transaction, }); @@ -65,16 +68,14 @@ cron.schedule("0 0 * * *", async () => { }); await models.User.destroy({ - where: { - ID: { [models.Sequelize.Op.in]: unvalidatedUserIDs }, - }, + where: { ID: { [models.Sequelize.Op.in]: unvalidatedUserIDs } }, transaction, }); } await transaction.commit(); console.log( - "Removed unvalidated users and their related data registered over 1 day ago." + "Removed unvalidated users and their related data" ); } catch (error) { await transaction.rollback(); @@ -83,6 +84,8 @@ cron.schedule("0 0 * * *", async () => { error ); } +}, { + timezone: "Asia/Jakarta" }); app.listen(process.env.APP_PORT, () => {