Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add confirm password field #53

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions client/src/pages/EmailVerifiedMessage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const EmailVerifiedMessage = () => {
const navigate = useNavigate();

return (
<div className="flex flex-col items-center justify-center h-screen bg-gray-100">
<div className="max-w-md w-full px-4 py-8 bg-white rounded-lg shadow-lg">
<div className="flex h-screen flex-col items-center justify-center bg-gray-100">
<div className="w-full max-w-md rounded-lg bg-white px-4 py-8 shadow-lg">
<div className="mb-4 text-center">
<h2 className="text-3xl font-bold text-green-600 mb-4">
<h2 className="mb-4 text-3xl font-bold text-green-600">
Congratulations!
</h2>
<p className="text-gray-600">
Expand All @@ -17,7 +17,7 @@ const EmailVerifiedMessage = () => {
</div>
<button
onClick={() => navigate("/signin")}
className="w-full py-2 px-4 bg-green-600 text-white rounded-md hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-green-600 focus:ring-opacity-50"
className="w-full rounded-md bg-green-600 px-4 py-2 text-white hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-green-600 focus:ring-opacity-50"
>
Login Now
</button>
Expand Down
80 changes: 67 additions & 13 deletions client/src/pages/SignUp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const SignUpNew = () => {
const [name, setName] = useState("");
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [confirmpassword, setConfirmpassword] = useState("");
const [avatar, setAvatar] = useState(null);
const [avatarError, setAvatarError] = useState(null);

Expand All @@ -39,6 +40,9 @@ const SignUpNew = () => {
const handlePasswordChange = (e) => {
setPassword(e.target.value);
};
const handlecPasswordChange = (e) => {
setConfirmpassword(e.target.value);
};

const handleAvatarChange = (e) => {
const file = e.target.files[0];
Expand Down Expand Up @@ -75,6 +79,7 @@ const SignUpNew = () => {
formData.append("name", name);
formData.append("email", email);
formData.append("password", password);
formData.append("confirmpassword", confirmpassword);
formData.append("avatar", avatar);
formData.append("role", "general");
formData.append("isConsentGiven", isConsentGiven.toString());
Expand Down Expand Up @@ -261,20 +266,69 @@ const SignUpNew = () => {
autoComplete="off"
/>
</div>
<div className="relative mt-4 flex items-center">
<span className="absolute">
<svg
xmlns="http://www.w3.org/2000/svg"
className="mx-3 h-6 w-6 text-gray-300"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z"
/>
</svg>
</span>
<input
id="confirmpassword"
name="confirmpassword"
type="password"
value={confirmpassword}
onChange={handlecPasswordChange}
className="block w-full rounded-lg border bg-white px-10 py-3 text-gray-700 focus:border-blue-400 focus:outline-none focus:ring focus:ring-blue-300 focus:ring-opacity-40"
placeholder="Confirm Password"
required
autoComplete="off"
/>
</div>
<div className="mt-6">
<button
disabled={loading}
type="submit"
className={`w-full transform rounded-lg bg-blue-500 px-6 py-3 text-sm font-medium tracking-wide text-white transition-colors duration-300 hover:bg-blue-700 focus:outline-none focus:ring focus:ring-blue-300 focus:ring-opacity-50 ${
loading ? "cursor-not-allowed opacity-50" : ""
}`}
>
{loading ? (
<ButtonLoadingSpinner loadingText={loadingText} />
) : (
<span>Sign Up</span>
)}
</button>
{password !== confirmpassword ? (
<button
style={{ cursor: "no-drop" }}
disabled={loading}
type="submit"
className={`w-full transform rounded-lg bg-blue-500 px-6 py-3 text-sm font-medium tracking-wide text-white transition-colors duration-300 hover:bg-blue-700 focus:outline-none focus:ring focus:ring-blue-300 focus:ring-opacity-50 ${
loading ? "cursor-not-allowed opacity-50" : ""
}`}
>
{loading ? (
<ButtonLoadingSpinner loadingText={loadingText} />
) : (
<span>Sign Up</span>
)}
</button>
) : (
<>
{" "}
<button
disabled={loading}
type="submit"
className={`w-full transform rounded-lg bg-blue-500 px-6 py-3 text-sm font-medium tracking-wide text-white transition-colors duration-300 hover:bg-blue-700 focus:outline-none focus:ring focus:ring-blue-300 focus:ring-opacity-50 ${
loading ? "cursor-not-allowed opacity-50" : ""
}`}
>
{loading ? (
<ButtonLoadingSpinner loadingText={loadingText} />
) : (
<span>Sign Up</span>
)}
</button>
</>
)}

<div onClick={() => setIsModalOpen(true)} className="mt-6">
{isConsentGiven && !isModerator ? (
Expand Down
4 changes: 2 additions & 2 deletions server/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ MONGODB_URI=mongodb://127.0.0.1:27017/db_socialecho or MongoDB Atlas connection
PORT=4000

# JWT
SECRET=
REFRESH_SECRET=
SECRET=sourabh
REFRESH_SECRET=sourabh

# Crypto
CRYPTO_KEY=
Expand Down
84 changes: 45 additions & 39 deletions server/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,49 +283,55 @@ const getUser = async (req, res, next) => {
* @param {Function} next - The next middleware function to call if consent is given by the user to enable context based auth.
*/
const addUser = async (req, res, next) => {
let newUser;
const hashedPassword = await bcrypt.hash(req.body.password, 10);
/**
* @type {boolean} isConsentGiven
*/
const isConsentGiven = JSON.parse(req.body.isConsentGiven);

const defaultAvatar =
"https://raw.githubusercontent.com/nz-m/public-files/main/dp.jpg";
const fileUrl = req.files?.[0]?.filename
? `${req.protocol}://${req.get("host")}/assets/userAvatars/${
req.files[0].filename
}`
: defaultAvatar;

const emailDomain = req.body.email.split("@")[1];
const role = emailDomain === "mod.socialecho.com" ? "moderator" : "general";

newUser = new User({
name: req.body.name,
email: req.body.email,
password: hashedPassword,
role: role,
avatar: fileUrl,
});
if (req.body.confirmpassword !== req.body.password) {
return res.status(400).json({
message: "Password not match",
});
} else {
let newUser;
const hashedPassword = await bcrypt.hash(req.body.password, 10);
/**
* @type {boolean} isConsentGiven
*/
const isConsentGiven = JSON.parse(req.body.isConsentGiven);

const defaultAvatar =
"https://raw.githubusercontent.com/nz-m/public-files/main/dp.jpg";
const fileUrl = req.files?.[0]?.filename
? `${req.protocol}://${req.get("host")}/assets/userAvatars/${
req.files[0].filename
}`
: defaultAvatar;

const emailDomain = req.body.email.split("@")[1];
const role = emailDomain === "mod.socialecho.com" ? "moderator" : "general";

newUser = new User({
name: req.body.name,
email: req.body.email,
password: hashedPassword,
role: role,
avatar: fileUrl,
});

try {
await newUser.save();
if (newUser.isNew) {
throw new Error("Failed to add user");
}
try {
await newUser.save();
if (newUser.isNew) {
throw new Error("Failed to add user");
}

if (isConsentGiven === false) {
res.status(201).json({
message: "User added successfully",
if (isConsentGiven === false) {
res.status(201).json({
message: "User added successfully",
});
} else {
next();
}
} catch (err) {
return res.status(200).json({
message: "Failed to add user",
});
} else {
next();
}
} catch (err) {
res.status(400).json({
message: "Failed to add user",
});
}
};

Expand Down