Skip to content

Commit

Permalink
request #16 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
udofia2 committed Oct 18, 2020
1 parent 91dfe61 commit 7aea7b8
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 63 deletions.
61 changes: 34 additions & 27 deletions api/controller/citizen.Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,67 +60,74 @@ const citizenActions = (Citizens, bcrypt, mySecrete, jwt, validationResult) => {
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}

try {
const {
name,
firstName,
lastName,
email,
password,
// POB,
phone,
placeOfBirth,
residentialAddress,
dateOfBirth,
nationality,
passport,
fingerPrint,
periodOfResidence,
age,
passportPages,
passportNum,
nationalIdentificationNumber,
password,
password1,
} = req.body;

const user = await Citizens.findOne({ email });

if (user) return res.status(400).json(`${email} is already in use`);

const citizen = new Citizens({
// _id: mongoose.Types.ObjectId,
name,
firstName,
lastName,
email,
password,
passport,
phone,
placeOfBirth,
residentialAddress,
dateOfBirth,
nationality,
periodOfResidence,
profileImage: req.file.path,
passportNum,
nationalIdentificationNumber,
password,
password1,
// profileImage: req.file.path,
});

const salt = await bcrypt.genSalt(10);
const hash = await bcrypt.hash(password, salt);
citizen.password = hash;

await citizen.save();

console.log(citizen);

const payload = {
user: citizen._id,
};
const token = jwt.sign(payload, mySecrete, { expiresIn: "1hr" });


console.log("works here");
res.status(201).json({
msg: `${citizen.name.first} ${citizen.name.last} is successfully registered`,
toke,
msg: `${citizen.firstName} ${citizen.lastName} is successfully registered`,
token,
request: {
Login: {
type: "POST",
url: "http://localhost:3000/api/v1/citizen/login",
description:
"Registered citizens can follow the provided url to login to their profile page. If you are using postman to, the request will be a post request",
"Registered citizens can follow the provided url to login to their profile page. If you are using postman to, the request will be a post request",
},
},
});
} catch (err) {
res.status(500).json(err);
}
};

/**
* @param POST /api/v1/citizen/login
* @desc route for citizens to signin on the platform
Expand Down
18 changes: 17 additions & 1 deletion api/middleware/formValidation.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
const validation = (check) => {
const regForm = [
check("firstName").not().isEmpty(),
check("lastName").not().isEmpty(),
check("email").isEmail(),
check("password").isLength({ min: 5 }),
check("phone").not().isEmpty(),
check("placeOfBirth").not().isEmpty(),
check("residentialAddress").not().isEmpty(),
check("dateOfBirth").not().isEmpty(),
check("nationality").not().isEmpty(),
check("passportNum").not().isEmpty(),
check("nationalIdentificationNumber").not().isEmpty(),
check("password").isLength({ min: 5 }).custom((value, {req, loc, path}) => {
if(value !== req.body.password1) {
//throw error if passwords don't match
throw new Error("Password do not match")
} else {
return value;
}
})
];

const loginForm = [
Expand Down
116 changes: 82 additions & 34 deletions api/model/citizen.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,100 @@ const mongoose = require("mongoose");
const { Schema } = mongoose;

const citizenSchema = new Schema({
name: {
first: {
type: String,
required: true,
},
last: {
type: String,
required: true,
},
firstName: {
type: String,
required: true
},
lastName : {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true,
required: true
},
password: {
phone: {
type: String,
required: true,
required: true
},
placeOfBirth: {
type: String,
required: true
},
residentialAddress: {
type: String,
required: true
},
dateOfBirth: {
type: String,
required: true
},
profileImage: String,
// POB: {
// type: Date,
// required: true,
// },
nationality: {
type: String,
required: true,
required: true
},
passport: {
number: {
type: Number,
require: true,
},

date: {
type: Date,
required: true,
},
placeOfIssuance: {
type: String,
required: true,
},
passportNum: {
type: String,
required: true
},
nationalIdentificationNumber: {
type: Number,
required: true
},
password: {
type: String,
required: true
},
fingerPrint: {
password1: {
type: String,
required: true
},
// name: {
// first: {
// type: String,
// required: true,
// },
// last: {
// type: String,
// required: true,
// },
// },
// email: {
// type: String,
// required: true,
// unique: true,
// },
// password: {
// type: String,
// required: true,
// },
// profileImage: String,
// POB: {
// type: Date,
// required: true,
// },
// nationality: {
// type: String,
// required: true,
// },
// passport: {
// number: {
// type: Number,
// require: true,
// },

// date: {
// type: Date,
// required: true,
// },
// placeOfIssuance: {
// type: String,
// required: true,
// },
// },
// fingerPrint: {
// type: String,
// },
// periodOfResidence: {
// type: Date,
// required: true,
Expand Down
2 changes: 1 addition & 1 deletion config/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const config = require("./default");
const dbConnection = () => {
const db = mongoose.connection;

mongoose.connect(config.MONGO_URI, {
mongoose.connect(config.MONGO_DB, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
Expand Down

0 comments on commit 7aea7b8

Please sign in to comment.