Simplified form control in Login and Register. Fixed registration bug. Throw better errors
This commit is contained in:
1 parent
7ce28b7042
commit
08565c9eda
6 files changed
+8
-37
No files matched your search
@@ -44,7 +44,7 @@ const Login = () => {
|
||||
} else {
|
||||
setErrors({});
|
||||
}
|
||||
}, []);
|
||||
}, [form]);
|
||||
|
||||
const handleSubmit = async () => {
|
||||
try {
|
||||
@@ -77,15 +77,6 @@ const Login = () => {
|
||||
};
|
||||
|
||||
const handleInput = (e) => {
|
||||
const fieldName = idMap[e.target.id];
|
||||
// Extract and validate individual fields as input changes
|
||||
const fieldSchema = loginValidation.extract(fieldName);
|
||||
const { error } = fieldSchema.validate(e.target.value);
|
||||
let errMsg = "";
|
||||
if (error) {
|
||||
errMsg = error.message;
|
||||
}
|
||||
setErrors({ ...errors, [fieldName]: errMsg });
|
||||
const newForm = { ...form, [idMap[e.target.id]]: e.target.value };
|
||||
setForm(newForm);
|
||||
};
|
||||
|
||||
@@ -52,18 +52,9 @@ const Register = () => {
|
||||
} else {
|
||||
setErrors({});
|
||||
}
|
||||
}, []);
|
||||
}, [form]);
|
||||
|
||||
const handleInput = (e) => {
|
||||
const fieldName = idMap[e.target.id];
|
||||
// Extract and validate individual fields as input changes
|
||||
const fieldSchema = registerValidation.extract(fieldName);
|
||||
const { error } = fieldSchema.validate(e.target.value);
|
||||
let errMsg = "";
|
||||
if (error) {
|
||||
errMsg = error.message;
|
||||
}
|
||||
setErrors({ ...errors, [fieldName]: errMsg });
|
||||
const newForm = { ...form, [idMap[e.target.id]]: e.target.value };
|
||||
setForm(newForm);
|
||||
};
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
const express = require("express");
|
||||
const logger = require("../utils/logger");
|
||||
require("dotenv").config();
|
||||
const {
|
||||
@@ -11,7 +10,6 @@ const {
|
||||
editAlertBodyValidation,
|
||||
deleteAlertParamValidation,
|
||||
} = require("../validation/joi");
|
||||
const { get } = require("mongoose");
|
||||
|
||||
const SERVICE_NAME = "alerts";
|
||||
|
||||
|
||||
@@ -45,18 +45,6 @@ const registerController = async (req, res, next) => {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if the user exists
|
||||
try {
|
||||
const isUser = await req.db.getUserByEmail(req, res);
|
||||
if (isUser) {
|
||||
throw new Error("User already exists");
|
||||
}
|
||||
} catch (error) {
|
||||
error.service = SERVICE_NAME;
|
||||
next(error);
|
||||
return;
|
||||
}
|
||||
|
||||
// Create a new user
|
||||
try {
|
||||
const newUser = await req.db.insertUser(req, res);
|
||||
@@ -101,8 +89,8 @@ const loginController = async (req, res, next) => {
|
||||
|
||||
// Compare password
|
||||
const match = await user.comparePassword(req.body.password);
|
||||
if (!match) {
|
||||
throw new Error("Password does not match!");
|
||||
if (match !== true) {
|
||||
throw new Error("Incorrect password");
|
||||
}
|
||||
|
||||
// Remove password from user object. Should this be abstracted to DB layer?
|
||||
|
||||
@@ -5,6 +5,7 @@ const Check = require("../models/Check");
|
||||
const Alert = require("../models/Alert");
|
||||
const RecoveryToken = require("../models/RecoveryToken");
|
||||
const crypto = require("crypto");
|
||||
const DUPLICATE_KEY_CODE = 11000; // MongoDB error code for duplicate key
|
||||
|
||||
const connect = async () => {
|
||||
try {
|
||||
@@ -34,6 +35,9 @@ const insertUser = async (req, res) => {
|
||||
await newUser.save();
|
||||
return await UserModel.findOne({ _id: newUser._id }).select("-password"); // .select() doesn't work with create, need to save then find
|
||||
} catch (error) {
|
||||
if (error.code === DUPLICATE_KEY_CODE) {
|
||||
throw new Error("Email already exists");
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -4,7 +4,6 @@ const handleErrors = (error, req, res, next) => {
|
||||
const status = error.status || 500;
|
||||
const message = error.message || "Something went wrong";
|
||||
const service = error.service || "Unknown service";
|
||||
|
||||
logger.error(error.message, { service: service });
|
||||
res.status(status).json({ success: false, msg: message });
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user