-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsignup.js
76 lines (62 loc) · 2.19 KB
/
signup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const express = require('express');
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const cors = require('cors');
require('dotenv').config({ path: '.envsignup' });// Load environment variables
// Connect t
mongoose.connect(process.env.MONGO_URI)
.then(() => console.log('Connected to MongoDB'))
.catch((error) => console.error('MongoDB connection error:', error));
const app = express();
const port = process.env.PORT || 3002;
// Middleware
app.use(cors()); // Enable CORS
app.use(express.json()); // Parse JSON requests
// User Schema
const userSchema = new mongoose.Schema({
fullName: { type: String, required: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
});
// User Model
const User = mongoose.model('User', userSchema);
// Registration Endpoint
app.post('/api/users', async (req, res) => {
const { fullName, email, password, confirmPassword, userType } = req.body;
// Validate fields
if (!fullName || !email || !password || !confirmPassword || userType === undefined) {
return res.status(400).json({ message: 'All fields are required' });
}
if (password !== confirmPassword) {
return res.status(400).json({ message: 'Passwords do not match' });
}
// Validate email format
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
return res.status(400).json({ message: 'Invalid email format' });
}
try {
// Check if user exists
const existingUser = await User.findOne({ email });
if (existingUser) {
return res.status(400).json({ message: 'User already exists' });
}
// Hash password
const hashedPassword = await bcrypt.hash(password, 10);
// Save user
const user = new User({
fullName,
email,
password: hashedPassword,
});
const result = await user.save();
res.status(201).json({ message: 'User registered successfully', userId: result._id });
} catch (error) {
console.error('Error saving user:', error);
res.status(500).json({ message: 'Error saving user data', error });
}
});
// Start server
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});