Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Registration #12

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ cache_temp/*
static_temp/*
.vscode
notice-config.js
package-lock.json
#package-lock.json
.env
75 changes: 71 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,79 @@
const express = require('express');
const logger = require('morgan');
const morgan = require('morgan');
const fs = require('fs')
const { connect } = require('mongoose')
const {success, error, info, log} = require('consola');

//Bring the app constants
const {DB, PORT} = require('./config/application');

//Initialize the application
const app = express();

//Middleware
app.use(morgan('tiny'));
app.use(express.json());
app.use(function (err, req, res, next) {
if (err instanceof SyntaxError) {
error({message:err, badge:true})
return res.status(400).json({message:err.message})
}else{
next();
}

});


//Router middleware
app.use('/api/user', require('./routes/users'));

let retryAttemptCount = 5;
const startApp = async function(){
try{
//DB connection
info({
message:'Trying to connect to the Database....',
badge:true
})

await connect(DB, {
useUnifiedTopology: true,
useNewUrlParser: true,
useFindAndModify: true
})

info({
message:`Successfully conneted to the Database \n${DB}`,
badge: true
});

//Start to listen the port
app.listen(PORT, () => {
log({
message:`Server started and listening on PORT: ${PORT}`,
badge:true
})
});

}catch(err){
error({
message:`Unable to connect to Database /n${DB}\n${err}`
});

while(retryAttemptCount > 0){
--retryAttemptCount;
startApp();
}

}

}

startApp()


app.get('/', (req, res) => {
res.send('Hello CMS!')
});

app.listen(3000, () => {
console.log('Server started and listening on PORT: 3000');
});

6 changes: 6 additions & 0 deletions config/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require('dotenv').config();

module.exports = {
DB: process.env.APP_DB,
PORT: process.env.PORT || 3000
}
51 changes: 51 additions & 0 deletions models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const { Schema, model } = require('mongoose');


const UserSchema = new Schema({
firstName:{
type:String
},
lastName:{
type:String
},
userName:{
type:String,
required:true
},
email:{
type:String,
validate: {
validator: function(v) {
return /[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/.test(v);
},
message: props => `${props.value} is not a valid email address!`
},
required: [true, 'User email address required']

},
password:{
type:String,
validate: {
validator: function(v) {
return new RegExp('^[a-zA-Z0-9]{3,30}$').test(v);
},
message: props => `password is invalid`
},
required: [true, 'User password required']
},
role:{
type:String,
default:'editor',
enum: ['admin', 'editor']
},
status:{
type:String,
default:'new',
enum:['new', 'verified']
}
}, {timestamps: true});

//use validation manually before password hashing
UserSchema.set('validateBeforeSave', false);

module.exports = model('users', UserSchema);
Loading