-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from priyachau12/main
I have added a User authentication of Signup page
- Loading branch information
Showing
13 changed files
with
774 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.