Merge pull request #685 from getmaxun/email-valid

feat: validate credentials
This commit is contained in:
Karishma Shukla
2025-07-15 22:13:15 +05:30
committed by GitHub
3 changed files with 63 additions and 39 deletions

View File

@@ -33,6 +33,14 @@ router.post("/register", async (req, res) => {
}); });
} }
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
return res.status(400).json({
error: "VALIDATION_ERROR",
code: "register.validation.invalid_email_format"
});
}
if (!password || password.length < 6) { if (!password || password.length < 6) {
return res.status(400).json({ return res.status(400).json({
error: "VALIDATION_ERROR", error: "VALIDATION_ERROR",
@@ -74,16 +82,16 @@ router.post("/register", async (req, res) => {
res.cookie("token", token, { res.cookie("token", token, {
httpOnly: true, httpOnly: true,
}); });
capture("maxun-oss-user-registered", { capture("maxun-oss-user-registered", {
email: user.email, email: user.email,
userId: user.id, userId: user.id,
registeredAt: new Date().toISOString(), registeredAt: new Date().toISOString(),
}); });
console.log(`User registered`); console.log(`User registered`);
res.json(user); res.json(user);
} catch (error: any) { } catch (error: any) {
console.log(`Could not register user - ${error}`); console.log(`Could not register user - ${error}`);
return res.status(500).json({ return res.status(500).json({
@@ -150,23 +158,23 @@ router.post("/login", async (req, res) => {
}); });
router.get("/logout", async (req, res) => { router.get("/logout", async (req, res) => {
try { try {
res.clearCookie("token"); res.clearCookie("token");
return res.status(200).json({ return res.status(200).json({
ok: true, ok: true,
message: "Logged out successfully", message: "Logged out successfully",
code: "success" code: "success"
}); });
} catch (error) { } catch (error) {
console.error('Logout error:', error); console.error('Logout error:', error);
return res.status(500).json({ return res.status(500).json({
ok: false, ok: false,
message: "Error during logout", message: "Error during logout",
code: "server", code: "server",
error: process.env.NODE_ENV === 'development' ? error : undefined error: process.env.NODE_ENV === 'development' ? error : undefined
}); });
}
} }
}
); );
router.get( router.get(
@@ -678,7 +686,7 @@ router.get("/airtable", requireSignIn, (req: Request, res) => {
router.get("/airtable/callback", requireSignIn, async (req: Request, res) => { router.get("/airtable/callback", requireSignIn, async (req: Request, res) => {
const authenticatedReq = req as AuthenticatedRequest; const authenticatedReq = req as AuthenticatedRequest;
const baseUrl = process.env.PUBLIC_URL || "http://localhost:5173"; const baseUrl = process.env.PUBLIC_URL || "http://localhost:5173";
try { try {
const { code, state, error } = authenticatedReq.query; const { code, state, error } = authenticatedReq.query;
@@ -694,7 +702,7 @@ router.get("/airtable/callback", requireSignIn, async (req: Request, res) => {
// Verify session data // Verify session data
if (!authenticatedReq.session?.code_verifier || authenticatedReq.session.robotId !== state.toString()) { if (!authenticatedReq.session?.code_verifier || authenticatedReq.session.robotId !== state.toString()) {
return res.status(400).json({ return res.status(400).json({
message: "Session expired - please restart the OAuth flow" message: "Session expired - please restart the OAuth flow"
}); });
} }
@@ -708,7 +716,7 @@ router.get("/airtable/callback", requireSignIn, async (req: Request, res) => {
body: new URLSearchParams({ body: new URLSearchParams({
grant_type: "authorization_code", grant_type: "authorization_code",
code: code.toString(), code: code.toString(),
client_id: process.env.AIRTABLE_CLIENT_ID!, client_id: process.env.AIRTABLE_CLIENT_ID!,
redirect_uri: process.env.AIRTABLE_REDIRECT_URI!, redirect_uri: process.env.AIRTABLE_REDIRECT_URI!,
code_verifier: authenticatedReq.session.code_verifier code_verifier: authenticatedReq.session.code_verifier
}), }),
@@ -811,7 +819,7 @@ router.get("/airtable/bases", requireSignIn, async (req: Request, res) => {
// Update robot with selected base // Update robot with selected base
router.post("/airtable/update", requireSignIn, async (req: Request, res) => { router.post("/airtable/update", requireSignIn, async (req: Request, res) => {
const authenticatedReq = req as AuthenticatedRequest; const authenticatedReq = req as AuthenticatedRequest;
const { baseId, robotId , baseName, tableName, tableId} = req.body; const { baseId, robotId, baseName, tableName, tableId } = req.body;
if (!baseId || !robotId) { if (!baseId || !robotId) {
return res.status(400).json({ message: "Base ID and Robot ID are required" }); return res.status(400).json({ message: "Base ID and Robot ID are required" });

View File

@@ -42,6 +42,12 @@ const Login = () => {
const submitForm = async (e: any) => { const submitForm = async (e: any) => {
e.preventDefault(); e.preventDefault();
if (!email.includes("@")) {
notify("error", "Please enter a valid email.");
return;
}
setLoading(true); setLoading(true);
try { try {
const { data } = await axios.post( const { data } = await axios.post(
@@ -55,11 +61,11 @@ const Login = () => {
navigate("/"); navigate("/");
} catch (err: any) { } catch (err: any) {
const errorResponse = err.response?.data; const errorResponse = err.response?.data;
const errorMessage = errorResponse?.code const errorMessage = errorResponse?.code
? t(errorResponse.code) ? t(errorResponse.code)
: t('login.error.generic'); : t('login.error.generic');
notify("error", errorMessage); notify("error", errorMessage);
setLoading(false); setLoading(false);
} }

View File

@@ -9,9 +9,8 @@ import { useThemeMode } from "../context/theme-provider";
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import i18n from '../i18n'; import i18n from '../i18n';
const Register = () => { const Register = () => {
const {t} = useTranslation(); const { t } = useTranslation();
const [form, setForm] = useState({ const [form, setForm] = useState({
email: "", email: "",
password: "", password: "",
@@ -39,6 +38,13 @@ const Register = () => {
const submitForm = async (e: any) => { const submitForm = async (e: any) => {
e.preventDefault(); e.preventDefault();
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
notify("error", "Invalid email format");
return;
}
setLoading(true); setLoading(true);
try { try {
const { data } = await axios.post(`${apiUrl}/auth/register`, { email, password }); const { data } = await axios.post(`${apiUrl}/auth/register`, { email, password });
@@ -46,12 +52,12 @@ const Register = () => {
notify("success", t('register.welcome_notification')); notify("success", t('register.welcome_notification'));
window.localStorage.setItem("user", JSON.stringify(data)); window.localStorage.setItem("user", JSON.stringify(data));
navigate("/"); navigate("/");
} catch (error:any) { } catch (error: any) {
const errorResponse = error.response?.data; const errorResponse = error.response?.data;
const errorMessage = errorResponse?.code const errorMessage = errorResponse?.code
? t(errorResponse.code) ? t(errorResponse.code)
: t('register.error.generic'); : t('register.error.generic');
notify("error", errorMessage); notify("error", errorMessage);
setLoading(false); setLoading(false);
@@ -68,7 +74,6 @@ const Register = () => {
mt: 6, mt: 6,
padding: 4, padding: 4,
backgroundColor: darkMode ? "#121212" : "#ffffff", backgroundColor: darkMode ? "#121212" : "#ffffff",
}} }}
> >
<Box <Box
@@ -80,7 +85,8 @@ const Register = () => {
color: darkMode ? "#ffffff" : "#333333", color: darkMode ? "#ffffff" : "#333333",
padding: 6, padding: 6,
borderRadius: 5, borderRadius: 5,
boxShadow: "0px 20px 40px rgba(0, 0, 0, 0.2), 0px -5px 10px rgba(0, 0, 0, 0.15)", boxShadow:
"0px 20px 40px rgba(0, 0, 0, 0.2), 0px -5px 10px rgba(0, 0, 0, 0.15)",
display: "flex", display: "flex",
flexDirection: "column", flexDirection: "column",
alignItems: "center", alignItems: "center",
@@ -143,9 +149,13 @@ const Register = () => {
t('register.button') t('register.button')
)} )}
</Button> </Button>
<Typography variant="body2" align="center" sx={{ color: darkMode ? "#ffffff" : "#333333" }}> <Typography
variant="body2"
align="center"
sx={{ color: darkMode ? "#ffffff" : "#333333" }}
>
{t('register.register_prompt')}{" "} {t('register.register_prompt')}{" "}
<Link to="/login" style={{ textDecoration: "none", color: "#ff33cc" }}> <Link to="/login" style={{ textDecoration: "none", color: "#ff33cc" }}>
{t('register.login_link')} {t('register.login_link')}
</Link> </Link>
</Typography> </Typography>
@@ -154,4 +164,4 @@ const Register = () => {
); );
}; };
export default Register; export default Register;