Skip to content

Commit

Permalink
Signup and Login Completed
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuj3553 committed Oct 28, 2024
1 parent ecddebc commit 1ff5df0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
4 changes: 2 additions & 2 deletions client/src/component/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const Login = ({ mode, showAlert, isloggedin, setloggedin }) => {
e.preventDefault();
setLoading(true);
try {
console.log("Credentials : ", credentials);
const response = await fetch(`${VITE_SERVER_PORT}/api/auth/login`, {
method: "POST",
headers: {
Expand All @@ -34,14 +35,13 @@ const Login = ({ mode, showAlert, isloggedin, setloggedin }) => {
body: JSON.stringify(credentials),
});
const json = await response.json();
console.log(json);

if (json.success) {
localStorage.setItem("token", json.authtoken);
showAlert("Logged in Successfully", "success");
toast.success("Login Successfully!");

setloggedin(!isloggedin)

navigate("/");
} else {
showAlert("Invalid Credentials", "danger");
Expand Down
4 changes: 2 additions & 2 deletions client/src/component/Signup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ const Signup = ({ mode }) => {
if (json.success) {
localStorage.setItem("token", json.authtoken);
navigate("/");
toast.success("Account Created Successfully!");
toast.success(json.message || "Account Created Successfully!");
} else {
toast.error("Account not created. Please check your email inbox!");
toast.error(json.message || "An error occurred. Please try again later.");
}
};

Expand Down
29 changes: 19 additions & 10 deletions server/Controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ require('dotenv').config(); // Load environment variables from .env file
// Signup route
const createUser = async (req, res) => {
const VITE_CLIENT_PORT = process.env.VITE_CLIENT_PORT || "https://bitbox-in.netlify.app";
console.log(process.env.EMAIL_USER);
console.log(process.env.EMAIL_PASS);
const { name, email, password } = req.body;

try {
Expand All @@ -18,7 +16,7 @@ const createUser = async (req, res) => {
const img = `https://api.dicebear.com/5.x/initials/svg?seed=${name}`;

// Create a new user (save in your database)
const user = new User({ name, image: img, email, password, verified: false });
const user = new User({ name, image: img, email, password: hashedPassword, verified: false });
await user.save();

const verificationToken = crypto.randomBytes(32).toString("hex");
Expand Down Expand Up @@ -49,10 +47,9 @@ const createUser = async (req, res) => {
message: `Error sending verification email: ${error.message}`,
});
}
res.status(200).json({
success: true,
message: "Signup successful! Please check your email for the verification link.",
});
if (!user.verified) {
return res.status(401).json({ success: false, message: "Signup successful! Please check your email for the verification link." });
}
});
} catch (error) {
res.status(500).json({ success: false, message: 'An error occurred during signup' });
Expand All @@ -70,14 +67,26 @@ const verifyToken = async (req, res) => {
message: "Invalid or expired verification link",
});
}
console.log(user);

user.verified = true;
user.verificationToken = undefined;
user.verificationToken = token;
await user.save();

if (user) {
return res.status(200).json({
success: true,
message: "Email verified successfully",
});
}

res.status(200).json({
success: true,
message: "Signup successfully",
});

const VITE_CLIENT_PORT = process.env.VITE_CLIENT_PORT || "https://bitbox-in.netlify.app";
// Redirect to the frontend's home page after verification
return res.redirect({ VITE_CLIENT_PORT });
return res.redirect(`${VITE_CLIENT_PORT}/login`);
} catch (err) {
console.error(err);
return res.status(500).json({
Expand Down
5 changes: 4 additions & 1 deletion server/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const loginLimiter = rateLimit({
"Too many login attempts from this IP, please try again after 5 minutes.",
});

router.post(
router.post(
"/login",
loginLimiter, // rate limiter middleware
[
Expand All @@ -96,6 +96,8 @@ router.post(
try {
// Find user by email
let user = await User.findOne({ email });

// If user does not exists
if (!user) {
success = false;
return res.status(400).json({
Expand All @@ -106,6 +108,7 @@ router.post(

// Compare provided password with stored password
const passwordCompare = await bcrypt.compare(password, user.password);

if (!passwordCompare) {
success = false;
return res.status(400).json({
Expand Down

0 comments on commit 1ff5df0

Please sign in to comment.