complete backend
This commit is contained in:
parent
4edea5d729
commit
c570604348
1 changed files with 204 additions and 157 deletions
|
|
@ -3,175 +3,222 @@ 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<{
|
||||
success: boolean;
|
||||
message: string;
|
||||
can_register?: boolean;
|
||||
can_login?: boolean;
|
||||
email?: string;
|
||||
export const checkUserInDb = async (
|
||||
email: string,
|
||||
password: string
|
||||
): Promise<{
|
||||
success: boolean;
|
||||
message: string;
|
||||
can_register?: boolean;
|
||||
can_login?: boolean;
|
||||
email?: string;
|
||||
}> => {
|
||||
try {
|
||||
function isAdspillarEmail(email: string) {
|
||||
const regex = /^[a-zA-Z0-9._%+-]+@adspillar\.com$/;
|
||||
return regex.test(email);
|
||||
}
|
||||
|
||||
if (!isAdspillarEmail(email)) {
|
||||
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));
|
||||
|
||||
if (!findUser[0]) {
|
||||
return { success: true, message: "User not found", can_register: true };
|
||||
}
|
||||
|
||||
const hash = findUser[0].password;
|
||||
const isMatch = await Bun.password.verify(password, hash);
|
||||
|
||||
if (isMatch && findUser[0].is_verified && findUser[0].is_active) {
|
||||
return {
|
||||
success: true,
|
||||
message: "User verified successfully",
|
||||
can_login: true,
|
||||
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) {
|
||||
return { success: false, message: "User not active", can_login: false };
|
||||
}
|
||||
else {
|
||||
return { success: false, message: "Invalid password", can_login: false };
|
||||
}
|
||||
}
|
||||
|
||||
// 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));
|
||||
|
||||
// if (!findUser[0]) {
|
||||
// return { success: true, message: "Wrong credentials", can_register: true };
|
||||
// }
|
||||
|
||||
// const hash = findUser[0].password;
|
||||
// const isMatch = await Bun.password.verify(password, hash);
|
||||
|
||||
// if (isMatch && findUser[0].is_verified && findUser[0].is_active) {
|
||||
// return {
|
||||
// success: true,
|
||||
// message: "User verified successfully",
|
||||
// can_login: true,
|
||||
// email: findUser[0].email, // Ensure email is included
|
||||
// can_register: false
|
||||
// };
|
||||
// }
|
||||
// else if (isMatch && findUser[0].is_verified === false && findUser[0].is_active) {
|
||||
// return { success: false, message: "User not verified", can_login: false, can_register: false };
|
||||
// }
|
||||
// else if (isMatch && findUser[0].is_active === false && findUser[0].is_verified) {
|
||||
// return { success: false, message: "User not active", can_login: false, can_register: false };
|
||||
// }
|
||||
// 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" };
|
||||
try {
|
||||
function isAdspillarEmail(email: string) {
|
||||
const regex = /^[a-zA-Z0-9._%+-]+@adspillar\.com$/;
|
||||
return regex.test(email);
|
||||
}
|
||||
|
||||
if (!isAdspillarEmail(email)) {
|
||||
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));
|
||||
|
||||
if (!findUser[0]) {
|
||||
return { success: true, message: "User not found", can_register: true };
|
||||
}
|
||||
|
||||
const hash = findUser[0].password;
|
||||
const isMatch = await Bun.password.verify(password, hash);
|
||||
|
||||
if (isMatch && findUser[0].is_verified && findUser[0].is_active) {
|
||||
return {
|
||||
success: true,
|
||||
message: "User verified successfully",
|
||||
can_login: true,
|
||||
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
|
||||
) {
|
||||
return { success: false, message: "User not active", can_login: false };
|
||||
} else {
|
||||
return {
|
||||
success: false,
|
||||
message: "Invalid password",
|
||||
can_login: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 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));
|
||||
|
||||
// if (!findUser[0]) {
|
||||
// return { success: true, message: "Wrong credentials", can_register: true };
|
||||
// }
|
||||
|
||||
// const hash = findUser[0].password;
|
||||
// const isMatch = await Bun.password.verify(password, hash);
|
||||
|
||||
// if (isMatch && findUser[0].is_verified && findUser[0].is_active) {
|
||||
// return {
|
||||
// success: true,
|
||||
// message: "User verified successfully",
|
||||
// can_login: true,
|
||||
// email: findUser[0].email, // Ensure email is included
|
||||
// can_register: false
|
||||
// };
|
||||
// }
|
||||
// else if (isMatch && findUser[0].is_verified === false && findUser[0].is_active) {
|
||||
// return { success: false, message: "User not verified", can_login: false, can_register: false };
|
||||
// }
|
||||
// else if (isMatch && findUser[0].is_active === false && findUser[0].is_verified) {
|
||||
// return { success: false, message: "User not active", can_login: false, can_register: false };
|
||||
// }
|
||||
// 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 }> => {
|
||||
try {
|
||||
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 storeRefreshToken = async (email: string, refreshToken: string): Promise<{ success: boolean; message: string }> => {
|
||||
export const sendVerificationEmail = async (
|
||||
email: string,
|
||||
token: string,
|
||||
set: any
|
||||
) => {
|
||||
const sendEmail = async (email: string, token: string) => {
|
||||
try {
|
||||
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" };
|
||||
}
|
||||
}
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: ENV.MAIL_HOST,
|
||||
port: ENV.MAIL_PORT,
|
||||
auth: {
|
||||
user: ENV.MAIL_USER,
|
||||
pass: ENV.MAIL_PASS,
|
||||
},
|
||||
});
|
||||
|
||||
export const sendVerificationEmail = async (email: string, token: string, set: any) => {
|
||||
const sendEmail = async (email: string, token: string) => {
|
||||
try {
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: ENV.MAIL_HOST,
|
||||
port: ENV.MAIL_PORT,
|
||||
auth: {
|
||||
user: ENV.MAIL_USER,
|
||||
pass: ENV.MAIL_PASS,
|
||||
},
|
||||
});
|
||||
|
||||
const url = `${ENV.SERVER_URL}:${ENV.SERVER_PORT}/api/auth/verify?token=${token}`;
|
||||
const mailOptions = {
|
||||
from: ENV.MAIL_USER,
|
||||
to: email,
|
||||
subject: 'Verify Your Email Address',
|
||||
html: `<p>Please verify your email by clicking the following link:</p>
|
||||
const url = `${ENV.SERVER_URL}/auth/verify?token=${token}`;
|
||||
const mailOptions = {
|
||||
from: ENV.MAIL_USER,
|
||||
to: email,
|
||||
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" };
|
||||
} catch (error) {
|
||||
console.error("Error sending email:", error);
|
||||
return { status: 500, message: "Internal server error, unable to send email" };
|
||||
}
|
||||
};
|
||||
const emailResponse = await sendEmail(email, token);
|
||||
set.status = emailResponse.status;
|
||||
return emailResponse;
|
||||
}
|
||||
await transporter.sendMail(mailOptions);
|
||||
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",
|
||||
};
|
||||
}
|
||||
};
|
||||
const emailResponse = await sendEmail(email, token);
|
||||
set.status = emailResponse.status;
|
||||
return emailResponse;
|
||||
};
|
||||
|
||||
export const sendResetPasswordEmail = async (email: string, token: string, set: any) => {
|
||||
const sendEmail = async (email: string, token: string) => {
|
||||
try {
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: ENV.MAIL_HOST,
|
||||
port: ENV.MAIL_PORT,
|
||||
auth: {
|
||||
user: ENV.MAIL_USER,
|
||||
pass: ENV.MAIL_PASS,
|
||||
},
|
||||
});
|
||||
export const sendResetPasswordEmail = async (
|
||||
email: string,
|
||||
token: string,
|
||||
set: any
|
||||
) => {
|
||||
const sendEmail = async (email: string, token: string) => {
|
||||
try {
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: ENV.MAIL_HOST,
|
||||
port: ENV.MAIL_PORT,
|
||||
auth: {
|
||||
user: ENV.MAIL_USER,
|
||||
pass: ENV.MAIL_PASS,
|
||||
},
|
||||
});
|
||||
|
||||
const url = `${ENV.SERVER_URL}:${ENV.SERVER_PORT}/api/auth/reset-password?token=${token}`;
|
||||
const mailOptions = {
|
||||
from: ENV.MAIL_USER,
|
||||
to: email,
|
||||
subject: 'Reset Your Password',
|
||||
html: `<p>Please reset your password by clicking the following link:</p>
|
||||
const url = `${ENV.SERVER_URL}/auth/reset-password?token=${token}`;
|
||||
const mailOptions = {
|
||||
from: ENV.MAIL_USER,
|
||||
to: email,
|
||||
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" };
|
||||
} catch (error) {
|
||||
console.error("Error sending email:", error);
|
||||
return { status: 500, message: "Internal server error, unable to send email" };
|
||||
}
|
||||
};
|
||||
const emailResponse = await sendEmail(email, token);
|
||||
set.status = emailResponse.status;
|
||||
return emailResponse;
|
||||
}
|
||||
|
||||
};
|
||||
await transporter.sendMail(mailOptions);
|
||||
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",
|
||||
};
|
||||
}
|
||||
};
|
||||
const emailResponse = await sendEmail(email, token);
|
||||
set.status = emailResponse.status;
|
||||
return emailResponse;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue