complete backend

This commit is contained in:
smfahim25 2025-03-20 15:46:50 +06:00
parent 4edea5d729
commit c570604348

View file

@ -3,9 +3,12 @@ import { db } from "../../db";
import { users } from "../../db/schema";
import { ENV } from "../../config/env";
// @ts-ignore
import nodemailer from 'nodemailer';
import nodemailer from "nodemailer";
export const checkUserInDb = async (email: string, password: string): Promise<{
export const checkUserInDb = async (
email: string,
password: string
): Promise<{
success: boolean;
message: string;
can_register?: boolean;
@ -19,17 +22,23 @@ export const checkUserInDb = async (email: string, password: string): Promise<{
}
if (!isAdspillarEmail(email)) {
return { success: false, message: "Invalid email domain", can_register: false, can_login: false };
}
else {
const findUser = await db.select({
return {
success: false,
message: "Invalid email domain",
can_register: false,
can_login: false,
};
} else {
const findUser = await db
.select({
email: users.email,
password: users.password,
is_active: users.is_active,
is_verified: users.is_verified,
refresh_token: users.refresh_token,
}).from(users).where(eq(users.email, email));
})
.from(users)
.where(eq(users.email, email));
if (!findUser[0]) {
return { success: true, message: "User not found", can_register: true };
@ -43,17 +52,30 @@ export const checkUserInDb = async (email: string, password: string): Promise<{
success: true,
message: "User verified successfully",
can_login: true,
email: findUser[0].email // Ensure email is included
email: findUser[0].email, // Ensure email is included
};
}
else if (isMatch && findUser[0].is_verified === false && findUser[0].is_active) {
return { success: false, message: "User not verified", can_login: false };
}
else if (isMatch && findUser[0].is_active === false && findUser[0].is_verified) {
} else if (
isMatch &&
findUser[0].is_verified === false &&
findUser[0].is_active
) {
return {
success: false,
message: "User not verified",
can_login: false,
};
} else if (
isMatch &&
findUser[0].is_active === false &&
findUser[0].is_verified
) {
return { success: false, message: "User not active", can_login: false };
}
else {
return { success: false, message: "Invalid password", can_login: false };
} else {
return {
success: false,
message: "Invalid password",
can_login: false,
};
}
}
@ -90,25 +112,33 @@ export const checkUserInDb = async (email: string, password: string): Promise<{
// else {
// return { success: false, message: "Invalid credentials", can_login: false, can_register: false };
// }
} catch (error: any) {
console.log("Error verifying user:", error);
return { success: false, message: "Error verifying user" };
}
};
export const storeRefreshToken = async (email: string, refreshToken: string): Promise<{ success: boolean; message: string }> => {
export const storeRefreshToken = async (
email: string,
refreshToken: string
): Promise<{ success: boolean; message: string }> => {
try {
await db.update(users).set({ refresh_token: refreshToken }).where(eq(users.email, email));
await db
.update(users)
.set({ refresh_token: refreshToken })
.where(eq(users.email, email));
return { success: true, message: "Refresh token stored successfully" };
} catch (error) {
console.log("Error storing refresh token:", error);
return { success: false, message: "Error storing refresh token" };
}
}
};
export const sendVerificationEmail = async (email: string, token: string, set: any) => {
export const sendVerificationEmail = async (
email: string,
token: string,
set: any
) => {
const sendEmail = async (email: string, token: string) => {
try {
const transporter = nodemailer.createTransport({
@ -120,29 +150,40 @@ export const sendVerificationEmail = async (email: string, token: string, set: a
},
});
const url = `${ENV.SERVER_URL}:${ENV.SERVER_PORT}/api/auth/verify?token=${token}`;
const url = `${ENV.SERVER_URL}/auth/verify?token=${token}`;
const mailOptions = {
from: ENV.MAIL_USER,
to: email,
subject: 'Verify Your Email Address',
subject: "Verify Your Email Address",
html: `<p>Please verify your email by clicking the following link:</p>
<p><a href="${url}">Verify email</a></p>
<p>This link will be valid for the next 10 minutes.</p>`,
};
await transporter.sendMail(mailOptions);
return { status: 200, message: "Verification email sent, link will valid till next 10 minutes" };
return {
status: 200,
message:
"Verification email sent, link will valid till next 10 minutes",
};
} catch (error) {
console.error("Error sending email:", error);
return { status: 500, message: "Internal server error, unable to send email" };
return {
status: 500,
message: "Internal server error, unable to send email",
};
}
};
const emailResponse = await sendEmail(email, token);
set.status = emailResponse.status;
return emailResponse;
}
};
export const sendResetPasswordEmail = async (email: string, token: string, set: any) => {
export const sendResetPasswordEmail = async (
email: string,
token: string,
set: any
) => {
const sendEmail = async (email: string, token: string) => {
try {
const transporter = nodemailer.createTransport({
@ -154,24 +195,30 @@ export const sendResetPasswordEmail = async (email: string, token: string, set:
},
});
const url = `${ENV.SERVER_URL}:${ENV.SERVER_PORT}/api/auth/reset-password?token=${token}`;
const url = `${ENV.SERVER_URL}/auth/reset-password?token=${token}`;
const mailOptions = {
from: ENV.MAIL_USER,
to: email,
subject: 'Reset Your Password',
subject: "Reset Your Password",
html: `<p>Please reset your password by clicking the following link:</p>
<p><a href="${url}">Reset password</a></p>
<p>This link will be valid for the next 10 minutes.</p>`,
};
await transporter.sendMail(mailOptions);
return { status: 200, message: "Reset password email sent, link will valid till next 10 minutes" };
return {
status: 200,
message:
"Reset password email sent, link will valid till next 10 minutes",
};
} catch (error) {
console.error("Error sending email:", error);
return { status: 500, message: "Internal server error, unable to send email" };
return {
status: 500,
message: "Internal server error, unable to send email",
};
}
};
const emailResponse = await sendEmail(email, token);
set.status = emailResponse.status;
return emailResponse;
}
};