Files
Backend_AutoGen/src/api-server/controller/authController.ts

158 lines
3.6 KiB
TypeScript

import { Request, Response } from "express";
import {
forgetPassword,
signinService,
signupService,
} from "../../shared/services/AuthService";
export const signupController = async (
req: Request,
res: Response
): Promise<void> => {
try {
const { userName, email, password, confirmPassword } = req.body;
if (!userName || !email || !password || !confirmPassword) {
res.status(400).json({
message: "All fields are required",
});
return;
}
const data = {
userName,
email,
password,
confirmPassword,
};
const result = await signupService(data);
switch (result.status) {
case "User Already exists":
res.status(403).json({
message: "User Already exists",
});
break;
case "Passwords do not match":
res.status(200).json({
message: "Passwords do not match",
});
break;
case "Signup unsuccessfull":
res.status(200).json({
message: "Signup unsuccessfull",
});
break;
case "Success":
res.status(200).json({
message: "Signup Successfull",
});
break;
default:
res.status(500).json({
message: "Internal server error",
});
break;
}
} catch (error) {
res.status(500).json({
message: "Unknown error",
});
}
};
export const signinController = async (
req: Request,
res: Response
): Promise<void> => {
try {
const { email, password } = req.body;
if (!email || !password) {
res.status(400).json({
message: "All fields are required",
});
return;
}
const data = {
email,
password,
};
const result = await signinService(data);
console.log("result: ", result);
switch (result.status) {
case "User not found!!! Kindly Signup":
res.status(403).json({
message: "User not found!!! Kindly Signup",
});
break;
case "Password is invalid...Check the credentials":
res.status(200).json({
message: "Password is invalid...Check the credentials",
});
break;
case "Success":
res.status(200).json({
message: "Signup Successfull",
data: result.data,
});
break;
default:
res.status(500).json({
message: "Internal server error",
});
break;
}
} catch (error) {
res.status(500).json({
message: "Unknown error",
});
}
};
export const forgetPasswordController = async (
req: Request,
res: Response
): Promise<void> => {
try {
const { email } = req.body;
if (!email) {
res.status(400).json({
message: "All fields are required",
});
return;
}
const data = {
email,
};
const result = await forgetPassword(data);
console.log("result: ", result);
switch (result.status) {
case "User not found!!! Kindly Signup":
res.status(403).json({
message: "User not found!!! Kindly Signup",
});
break;
case "Password is invalid...Check the credentials":
res.status(200).json({
message: "Password is invalid...Check the credentials",
});
break;
case "Success":
res.status(200).json({
message: "Signup Successfull",
// data: result.data,
});
break;
default:
res.status(500).json({
message: "Internal server error",
});
break;
}
} catch (error) {
res.status(500).json({
message: "Unknown error",
});
}
};