Skip to content

Commit

Permalink
Merge pull request #135 from priyachau12/main
Browse files Browse the repository at this point in the history
I have added a User authentication of Signup page
  • Loading branch information
shivamyadavrgipt authored Oct 22, 2024
2 parents 8ffdaaf + d2382c6 commit d7323cd
Show file tree
Hide file tree
Showing 13 changed files with 774 additions and 149 deletions.
152 changes: 105 additions & 47 deletions Backend/controllers/user.controller.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,105 @@
import bcrypt from 'bcryptjs';
import User from '../models/user.model.js';


export const registerUser = async (req, res) => {
try {
const { email, password } = req.body;

const existingUser = await User.findOne({ email });
if (existingUser) {
return res.status(400).json({ message: 'Email already in use' });
}

const hashedPassword = await bcrypt.hash(password, 12);
const newUser = new User({
email,
password: hashedPassword
});

await newUser.save();

res.status(201).json({ message: 'User registered successfully' });
} catch (error) {
res.status(500).json({ message: 'Error registering user', error: error.message });
}
};


export const loginUser = async (req, res) => {
try {
const { email, password } = req.body;

const user = await User.findOne({ email });
if (!user) {
return res.status(400).json({ message: 'User not found' });
}

const isPasswordCorrect = await bcrypt.compare(password, user.password);
if (!isPasswordCorrect) {
return res.status(400).json({ message: 'Invalid credentials' });
}

res.status(200).json({ message: 'Login successful' });
} catch (error) {
res.status(500).json({ message: 'Error logging in', error: error.message });
}
};
// import bcrypt from 'bcryptjs';
// import User from '../models/user.model.js';

// export const registerUser = async (req, res) => {
// try {
// const { email, password } = req.body;

// const existingUser = await User.findOne({ email });
// if (existingUser) {
// return res.status(400).json({ message: 'Email already in use' });
// }

// const hashedPassword = await bcrypt.hash(password, 12);
// const newUser = new User({
// email,
// password: hashedPassword
// });

// await newUser.save();

// res.status(201).json({ message: 'User registered successfully' });
// } catch (error) {
// res.status(500).json({ message: 'Error registering user', error: error.message });
// }
// };

// export const loginUser = async (req, res) => {
// try {
// const { email, password } = req.body;

// const user = await User.findOne({ email });
// if (!user) {
// return res.status(400).json({ message: 'User not found' });
// }

// const isPasswordCorrect = await bcrypt.compare(password, user.password);
// if (!isPasswordCorrect) {
// return res.status(400).json({ message: 'Invalid credentials' });
// }

// res.status(200).json({ message: 'Login successful' });
// } catch (error) {
// res.status(500).json({ message: 'Error logging in', error: error.message });
// }
// };
import bcrypt from "bcryptjs";
import User from "../models/user.model.js";

// User registration
export const registerUser = async (req, res) => {
try {
const { username, email, password, confirmPassword } = req.body;

// Check if password and confirm password match
// if (password !== confirmPassword) {
// return res.status(400).json({ message: 'Passwords do not match' });
// }

// Check if user already exists
const existingUser = await User.findOne({ email });
if (existingUser) {
return res.status(400).json({ message: "Email already in use" });
}

// Hash the password
const hashedPassword = await bcrypt.hash(password, 12);
const newUser = new User({ username, email, password: hashedPassword });

await newUser.save();
res.status(201).json({ message: "User registered successfully" });
} catch (error) {
res
.status(500)
.json({ message: "Error registering user", error: error.message });
}
};

// User login
export const loginUser = async (req, res) => {
try {
const { email, password } = req.body;

// Find user by email
const user = await User.findOne({ email });
if (!user) {
return res.status(400).json({ message: "User not found" });
}

// Check if password is correct
const isPasswordCorrect = await bcrypt.compare(password, user.password);
if (!isPasswordCorrect) {
return res.status(400).json({ message: "Invalid credentials" });
}

// Respond with success message (add token logic if needed)
res
.status(200)
.json({
message: "Login successful",
user: { username: user.username, email: user.email },
});
} catch (error) {
res.status(500).json({ message: "Error logging in", error: error.message });
}
};
32 changes: 18 additions & 14 deletions Backend/db/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import mongoose from "mongoose"

const connectionDB=async()=>{
try {
const connectioninstance=await mongoose.connect(`${process.env.MONGODB_URI}/blog}`)
console.log(`\n Mongodb connected ${connectioninstance.connection.host}`)
} catch (error) {
console.log("MongoDB Connection Error",error)
process.exit(1)

}
}

export default connectionDB
import mongoose from "mongoose";

const connectionDB = async () => {
try {
const connectioninstance = await mongoose.connect(
`${process.env.MONGODB_URI}}`
);
console.log(`\n Mongodb connected ${connectioninstance.connection.host}`);
} catch (error) {
console.log("MongoDB Connection Error", error);
process.exit(1);
}
};

export default connectionDB;
// priyachaurasiya730
// kqMCu6xmG4vy8aX9
// mongodb+srv://priyachaurasiya730:<db_password>@cluster0.a7tas.mongodb.net/
49 changes: 28 additions & 21 deletions Backend/models/user.model.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import mongoose, { Schema } from "mongoose";

const userSchema = new Schema({
email: {
type: String,
required: true,
unique: true,
lowercase: true,
trim: true,
},

password: {
type: String,
required: [true, "password is required"], // message to frontend
},

}, { timestamps: true });

const User = mongoose.model("User", userSchema); // create the document by this name

export default User
import mongoose, { Schema } from "mongoose";

const userSchema = new Schema(
{
username: {
type: String,
required: true,
unique: true,
trim: true,
},
email: {
type: String,
required: true,
unique: true,
lowercase: true,
trim: true,
},
password: {
type: String,
required: [true, "Password is required"], // Message to frontend
},
},
{ timestamps: true }
);

const User = mongoose.model("User", userSchema); // Create the document by this name

export default User;
32 changes: 23 additions & 9 deletions Backend/routes/user.route.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import express from 'express';
import { registerUser, loginUser } from '../controllers/user.controller.js';

const router = express.Router();

router.post('/register', registerUser);
router.post('/login', loginUser);

export default router;
// import express from 'express';
// import { registerUser, loginUser } from '../controllers/user.controller.js';

// const router = express.Router();

// router.post('/register', registerUser);
// router.post('/login', loginUser);

// export default router;
import express from 'express';
import { registerUser, loginUser } from '../controllers/user.controller.js';

const router = express.Router();

// Register User
router.post('/signup', registerUser);

// Login User
router.post('/login', loginUser);



export default router;
Loading

0 comments on commit d7323cd

Please sign in to comment.