Skip to content

Commit

Permalink
Merge pull request #81 from fga-eps-mds/i05_US05_user_sign_up
Browse files Browse the repository at this point in the history
CRUD User Finished
  • Loading branch information
guilhermedelyra authored Sep 24, 2020
2 parents 4dac2c1 + c3179b6 commit 26d9dbe
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const morgan = require('morgan');
const mongoose = require('mongoose');

const itemRoutes = require('./routes/itemRoutes');
const authRoutes = require('./routes/authRoutes');

// MongoDB connection
mongoose
Expand All @@ -25,8 +26,9 @@ app.use(express.json());

// routes
app.use('/item',itemRoutes);
app.use('/auth',authRoutes);

// starting the server
app.listen(app.get('port'), () => {
console.log(`Server on port ${app.get('port')}`);
});
});
21 changes: 21 additions & 0 deletions src/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const userSchema = new Schema({
username: {
type: String,
required: true
},
password: {
type: String,
required: true
},
email: {
type: String,
required: true
}
});

const User = mongoose.model('User', userSchema);

module.exports = User;
91 changes: 91 additions & 0 deletions src/routes/authRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const express = require('express');

const router = express.Router();

const User = require('../models/user');
const userSchema = require('../schemas/userSchema');

router.get('/', (req, res) => {
res.json({
message: 'Authentication!'
});
});

router.post('/signup', async(req, res, next) => {

try {

const newUserData = req.body;
const result = userSchema.validate(req.body);

if( await User.findOne({ username: newUserData.username}) ) {
const error = new Error('Username already being used.');
return next(error);
}

if( result.error ) {
return next(result.error);
}

const user = new User(newUserData);

user.save()
.then( result => {
return res.send(result);
})
.catch( err => next(err));

} catch(err) {
return next(err);
}

});

router.put('/update-user/:id', async(req, res, next) => {

try {

const user = await User.findById(req.params.id);
const newData = req.body;

if ( !newData.username ) {
newData.username = user.username;
}
if ( !newData.password ) {
newData.password = user.password;
}
if ( !newData.email ) {
newData.email = user.email;
}

const result = userSchema.validate(newData);

if(result.error) {
return next(result.error);
}

await User.findOneAndUpdate({_id: req.params.id}, req.body, { useFindAndModify: false})
.then( () => {
res.send({ message: 'User updated successfully.'});
});

} catch(err) {
return next(err);
}

});

router.delete('/delete-user/:id', async(req, res, next) => {

try {

await User.findByIdAndDelete(req.params.id);
return res.send({ message: 'User successfully deleted.' });

} catch(err) {
return next(err);
}

});

module.exports = router;
10 changes: 10 additions & 0 deletions src/schemas/userSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const Joi = require('joi');

const userSchema = Joi.object({
username: Joi.string().alphanum().min(4).max(30).required(),
password: Joi.string().min(8).required(),
passwordConfirmation: Joi.string().min(8).required().valid(Joi.ref('password')).error(new Error('Password confirmation does not match.')),
email: Joi.string().email().required()
});

module.exports = userSchema;

0 comments on commit 26d9dbe

Please sign in to comment.