implement user authentication controller, database configuration, admin initialization script, and update CORS origins

This commit is contained in:
Alaguraj0361 2026-04-17 14:48:06 +05:30
parent ec29463fb6
commit f0dd93bc00
4 changed files with 36 additions and 4 deletions

View File

@ -2,7 +2,7 @@ const mongoose = require("mongoose");
const connectDB = async () => {
try {
await mongoose.connect(process.env.MONGODB_URI, { dbName: "VG Products" });
await mongoose.connect(process.env.MONGODB_URI, { dbName: "VG_Products" });
console.log("✅ MongoDB connected");
} catch (err) {
console.error("❌ MongoDB connection error:", err);

32
create_admin.js Normal file
View File

@ -0,0 +1,32 @@
const mongoose = require("mongoose");
const bcrypt = require("bcrypt");
const User = require("./src/models/user.model.js");
require("dotenv").config();
async function createAdmin() {
await mongoose.connect(process.env.MONGODB_URI, { dbName: "VG_Products" });
console.log("Connected to MongoDB");
const email = "info@vgfenceproducts.com";
const password = "VGFenceProducts@2026";
const name = "VG Fence Admin";
const existing = await User.findOne({ email });
if (existing) {
console.log("Admin user already exists");
process.exit(0);
}
const passwordHash = await bcrypt.hash(password, 10);
await User.create({
name,
email,
passwordHash,
role: "admin"
});
console.log("Admin user created successfully!");
process.exit(0);
}
createAdmin().catch(console.error);

View File

@ -23,7 +23,7 @@ connectDB()
// ------------------ CORS ------------------
app.use(
cors({
origin: ["http://localhost:3500", "https://app.VG Products.co"],
origin: ["http://localhost:3000", "http://localhost:3001", "https://app.VG Products.co"],
credentials: true,
})
);

View File

@ -10,8 +10,8 @@ async function signup(req, res) {
const { name, email, mobileNumber, password } = req.body;
// Validate fields
if (!name || !email || !mobileNumber || !password) {
return res.status(400).json({ error: "All fields are required" });
if (!name || !email || !password) {
return res.status(400).json({ error: "Name, email and password are required" });
}
// Check if user exists