mirror of
https://gitlab.com/little-lines/backend.git
synced 2024-11-22 06:36:51 +00:00
create userHandler
This commit is contained in:
parent
f1f614d4e7
commit
cdd52920dc
4 changed files with 14 additions and 13 deletions
|
@ -11,21 +11,26 @@ const getAllUser = asyncHandler(async (req, res) => {
|
||||||
// Create new user => POST api/users/create
|
// Create new user => POST api/users/create
|
||||||
const createUser = asyncHandler(async (req, res) => {
|
const createUser = asyncHandler(async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { username,
|
const { username, password, email, isGoogleAccount } = req.body;
|
||||||
password,
|
|
||||||
email,
|
|
||||||
isGoogleAccount } = req.body;
|
|
||||||
|
|
||||||
const users = await user.create({
|
const newUser = await user.create({
|
||||||
username,
|
username,
|
||||||
password,
|
password,
|
||||||
email,
|
email,
|
||||||
isGoogleAccount
|
isGoogleAccount
|
||||||
});
|
});
|
||||||
|
|
||||||
res.status(200).json(users);
|
// Respond with the newly created user
|
||||||
} catch(err) {
|
res.status(201).json(newUser);
|
||||||
console.log(err)
|
} catch (err) {
|
||||||
|
// Handle validation errors
|
||||||
|
if (err.name === 'ValidationError') {
|
||||||
|
console.error(err);
|
||||||
|
return res.status(400).json({ error: err.message });
|
||||||
|
}
|
||||||
|
|
||||||
|
console.error(err);
|
||||||
|
res.status(500).json({ error: 'An error occurred while creating the user.' });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -13,10 +13,6 @@ const userSchema = new mongoose.Schema({
|
||||||
required: true,
|
required: true,
|
||||||
select : false
|
select : false
|
||||||
},
|
},
|
||||||
imgPath: {
|
|
||||||
type: String,
|
|
||||||
required: true
|
|
||||||
},
|
|
||||||
email: {
|
email: {
|
||||||
type: String,
|
type: String,
|
||||||
required: true
|
required: true
|
||||||
|
|
|
@ -6,7 +6,7 @@ const connectDb = require('./config/dbConnection');
|
||||||
|
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use('/api/users', require('./routes/user'));
|
app.use('/api/users', require('./routes/userRoute'));
|
||||||
|
|
||||||
|
|
||||||
app.listen(process.env.PORT, () => {
|
app.listen(process.env.PORT, () => {
|
||||||
|
|
Loading…
Reference in a new issue