forked from Hanna-sa/Project-ICT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignup.js
38 lines (31 loc) · 1000 Bytes
/
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
const express = require('express');
const router = express.Router();
const bcrypt = require('bcrypt');
const User = require('../models/User');
router.post('/s', async (req, res) => {
const { username,place,age, email,password,phoneNumber} = req.body;
try {
// Check if the user already exists
const existingUser = await User.findOne({ email });
if (existingUser) {
return res.status(400).json({ message: 'User already exists' });
}
// Hash the password
const hashedPassword = await bcrypt.hash(password, 10);
// Create a new user
const newUser = new User({
username,
place,
age,
email,
password:hashedPassword,
phoneNumber
});
// Save the user to the database
await newUser.save();
res.status(201).json({ message: 'User created successfully' });
} catch (error) {
res.status(500).json({ message: 'Internal server error' });
}
});
module.exports = router;