complete backend
This commit is contained in:
parent
4edea5d729
commit
c570604348
1 changed files with 204 additions and 157 deletions
|
|
@ -3,9 +3,12 @@ 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 (
|
||||||
|
email: string,
|
||||||
|
password: string
|
||||||
|
): Promise<{
|
||||||
success: boolean;
|
success: boolean;
|
||||||
message: string;
|
message: string;
|
||||||
can_register?: boolean;
|
can_register?: boolean;
|
||||||
|
|
@ -19,17 +22,23 @@ export const checkUserInDb = async (email: string, password: string): Promise<{
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isAdspillarEmail(email)) {
|
if (!isAdspillarEmail(email)) {
|
||||||
return { success: false, message: "Invalid email domain", can_register: false, can_login: false };
|
return {
|
||||||
}
|
success: false,
|
||||||
|
message: "Invalid email domain",
|
||||||
else {
|
can_register: false,
|
||||||
const findUser = await db.select({
|
can_login: false,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
const findUser = await db
|
||||||
|
.select({
|
||||||
email: users.email,
|
email: users.email,
|
||||||
password: users.password,
|
password: users.password,
|
||||||
is_active: users.is_active,
|
is_active: users.is_active,
|
||||||
is_verified: users.is_verified,
|
is_verified: users.is_verified,
|
||||||
refresh_token: users.refresh_token,
|
refresh_token: users.refresh_token,
|
||||||
}).from(users).where(eq(users.email, email));
|
})
|
||||||
|
.from(users)
|
||||||
|
.where(eq(users.email, email));
|
||||||
|
|
||||||
if (!findUser[0]) {
|
if (!findUser[0]) {
|
||||||
return { success: true, message: "User not found", can_register: true };
|
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,
|
success: true,
|
||||||
message: "User verified successfully",
|
message: "User verified successfully",
|
||||||
can_login: true,
|
can_login: true,
|
||||||
email: findUser[0].email // Ensure email is included
|
email: findUser[0].email, // Ensure email is included
|
||||||
};
|
};
|
||||||
}
|
} else if (
|
||||||
else if (isMatch && findUser[0].is_verified === false && findUser[0].is_active) {
|
isMatch &&
|
||||||
return { success: false, message: "User not verified", can_login: false };
|
findUser[0].is_verified === false &&
|
||||||
}
|
findUser[0].is_active
|
||||||
else if (isMatch && findUser[0].is_active === false && findUser[0].is_verified) {
|
) {
|
||||||
|
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 };
|
return { success: false, message: "User not active", can_login: false };
|
||||||
}
|
} else {
|
||||||
else {
|
return {
|
||||||
return { success: false, message: "Invalid password", can_login: false };
|
success: false,
|
||||||
|
message: "Invalid password",
|
||||||
|
can_login: false,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -90,25 +112,33 @@ export const checkUserInDb = async (email: string, password: string): Promise<{
|
||||||
// else {
|
// else {
|
||||||
// return { success: false, message: "Invalid credentials", can_login: false, can_register: false };
|
// return { success: false, message: "Invalid credentials", can_login: false, can_register: false };
|
||||||
// }
|
// }
|
||||||
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.log("Error verifying user:", error);
|
console.log("Error verifying user:", error);
|
||||||
return { success: false, message: "Error verifying user" };
|
return { success: false, message: "Error verifying user" };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const storeRefreshToken = async (
|
||||||
export const storeRefreshToken = async (email: string, refreshToken: string): Promise<{ success: boolean; message: string }> => {
|
email: string,
|
||||||
|
refreshToken: string
|
||||||
|
): Promise<{ success: boolean; message: string }> => {
|
||||||
try {
|
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" };
|
return { success: true, message: "Refresh token stored successfully" };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("Error storing refresh token:", error);
|
console.log("Error storing refresh token:", error);
|
||||||
return { success: false, message: "Error storing refresh token" };
|
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) => {
|
const sendEmail = async (email: string, token: string) => {
|
||||||
try {
|
try {
|
||||||
const transporter = nodemailer.createTransport({
|
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 = {
|
const mailOptions = {
|
||||||
from: ENV.MAIL_USER,
|
from: ENV.MAIL_USER,
|
||||||
to: email,
|
to: email,
|
||||||
subject: 'Verify Your Email Address',
|
subject: "Verify Your Email Address",
|
||||||
html: `<p>Please verify your email by clicking the following link:</p>
|
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 {
|
||||||
|
status: 200,
|
||||||
|
message:
|
||||||
|
"Verification email sent, link will valid till next 10 minutes",
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error sending email:", 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);
|
const emailResponse = await sendEmail(email, token);
|
||||||
set.status = emailResponse.status;
|
set.status = emailResponse.status;
|
||||||
return emailResponse;
|
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) => {
|
const sendEmail = async (email: string, token: string) => {
|
||||||
try {
|
try {
|
||||||
const transporter = nodemailer.createTransport({
|
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 = {
|
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 {
|
||||||
|
status: 200,
|
||||||
|
message:
|
||||||
|
"Reset password email sent, link will valid till next 10 minutes",
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error sending email:", 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);
|
const emailResponse = await sendEmail(email, token);
|
||||||
set.status = emailResponse.status;
|
set.status = emailResponse.status;
|
||||||
return emailResponse;
|
return emailResponse;
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue