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 { users } from "../../db/schema";
|
||||||
import { ENV } from "../../config/env";
|
import { ENV } from "../../config/env";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import nodemailer from 'nodemailer';
|
import nodemailer from "nodemailer";
|
||||||
|
|
||||||
export const checkUserInDb = async (email: string, password: string): Promise<{
|
export const checkUserInDb = async (
|
||||||
success: boolean;
|
email: string,
|
||||||
message: string;
|
password: string
|
||||||
can_register?: boolean;
|
): Promise<{
|
||||||
can_login?: boolean;
|
success: boolean;
|
||||||
email?: string;
|
message: string;
|
||||||
|
can_register?: boolean;
|
||||||
|
can_login?: boolean;
|
||||||
|
email?: string;
|
||||||
}> => {
|
}> => {
|
||||||
try {
|
try {
|
||||||
function isAdspillarEmail(email: string) {
|
function isAdspillarEmail(email: string) {
|
||||||
const regex = /^[a-zA-Z0-9._%+-]+@adspillar\.com$/;
|
const regex = /^[a-zA-Z0-9._%+-]+@adspillar\.com$/;
|
||||||
return regex.test(email);
|
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" };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
try {
|
||||||
await db.update(users).set({ refresh_token: refreshToken }).where(eq(users.email, email));
|
const transporter = nodemailer.createTransport({
|
||||||
return { success: true, message: "Refresh token stored successfully" };
|
host: ENV.MAIL_HOST,
|
||||||
} catch (error) {
|
port: ENV.MAIL_PORT,
|
||||||
console.log("Error storing refresh token:", error);
|
auth: {
|
||||||
return { success: false, message: "Error storing refresh token" };
|
user: ENV.MAIL_USER,
|
||||||
}
|
pass: ENV.MAIL_PASS,
|
||||||
}
|
},
|
||||||
|
});
|
||||||
|
|
||||||
export const sendVerificationEmail = async (email: string, token: string, set: any) => {
|
const url = `${ENV.SERVER_URL}/auth/verify?token=${token}`;
|
||||||
const sendEmail = async (email: string, token: string) => {
|
const mailOptions = {
|
||||||
try {
|
from: ENV.MAIL_USER,
|
||||||
const transporter = nodemailer.createTransport({
|
to: email,
|
||||||
host: ENV.MAIL_HOST,
|
subject: "Verify Your Email Address",
|
||||||
port: ENV.MAIL_PORT,
|
html: `<p>Please verify your email by clicking the following link:</p>
|
||||||
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>
|
|
||||||
<p><a href="${url}">Verify email</a></p>
|
<p><a href="${url}">Verify email</a></p>
|
||||||
<p>This link will be valid for the next 10 minutes.</p>`,
|
<p>This link will be valid for the next 10 minutes.</p>`,
|
||||||
};
|
};
|
||||||
|
|
||||||
await transporter.sendMail(mailOptions);
|
await transporter.sendMail(mailOptions);
|
||||||
return { status: 200, message: "Verification email sent, link will valid till next 10 minutes" };
|
return {
|
||||||
} catch (error) {
|
status: 200,
|
||||||
console.error("Error sending email:", error);
|
message:
|
||||||
return { status: 500, message: "Internal server error, unable to send email" };
|
"Verification email sent, link will valid till next 10 minutes",
|
||||||
}
|
};
|
||||||
};
|
} catch (error) {
|
||||||
const emailResponse = await sendEmail(email, token);
|
console.error("Error sending email:", error);
|
||||||
set.status = emailResponse.status;
|
return {
|
||||||
return emailResponse;
|
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 (
|
||||||
const sendEmail = async (email: string, token: string) => {
|
email: string,
|
||||||
try {
|
token: string,
|
||||||
const transporter = nodemailer.createTransport({
|
set: any
|
||||||
host: ENV.MAIL_HOST,
|
) => {
|
||||||
port: ENV.MAIL_PORT,
|
const sendEmail = async (email: string, token: string) => {
|
||||||
auth: {
|
try {
|
||||||
user: ENV.MAIL_USER,
|
const transporter = nodemailer.createTransport({
|
||||||
pass: ENV.MAIL_PASS,
|
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 url = `${ENV.SERVER_URL}/auth/reset-password?token=${token}`;
|
||||||
const mailOptions = {
|
const mailOptions = {
|
||||||
from: ENV.MAIL_USER,
|
from: ENV.MAIL_USER,
|
||||||
to: email,
|
to: email,
|
||||||
subject: 'Reset Your Password',
|
subject: "Reset Your Password",
|
||||||
html: `<p>Please reset your password by clicking the following link:</p>
|
html: `<p>Please reset your password by clicking the following link:</p>
|
||||||
<p><a href="${url}">Reset password</a></p>
|
<p><a href="${url}">Reset password</a></p>
|
||||||
<p>This link will be valid for the next 10 minutes.</p>`,
|
<p>This link will be valid for the next 10 minutes.</p>`,
|
||||||
};
|
};
|
||||||
await transporter.sendMail(mailOptions);
|
await transporter.sendMail(mailOptions);
|
||||||
return { status: 200, message: "Reset password email sent, link will valid till next 10 minutes" };
|
return {
|
||||||
} catch (error) {
|
status: 200,
|
||||||
console.error("Error sending email:", error);
|
message:
|
||||||
return { status: 500, message: "Internal server error, unable to send email" };
|
"Reset password email sent, link will valid till next 10 minutes",
|
||||||
}
|
};
|
||||||
};
|
} catch (error) {
|
||||||
const emailResponse = await sendEmail(email, token);
|
console.error("Error sending email:", error);
|
||||||
set.status = emailResponse.status;
|
return {
|
||||||
return emailResponse;
|
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