Skip to content
Snippets Groups Projects
Commit 56077672 authored by Ahmad, Rezwan (PG/T - Comp Sci & Elec Eng)'s avatar Ahmad, Rezwan (PG/T - Comp Sci & Elec Eng)
Browse files

Register Controller Created and Tested

parent 5901b5ae
No related branches found
No related tags found
1 merge request!9Create endpoint for user-registration
import UserModel from '../model/User.model.js';
import bcrypt from 'bcrypt';
/** POST: http://localhost:8080/api/register
* @param : {
"username" : "example123",
......@@ -7,7 +10,61 @@
}
*/
export async function register(req,res){
res.json('register route')
try {
const { username, password, profile, email } = req.body;
// Checking existing Username
const existUsername = new Promise((resolve, reject) => {
UserModel.findOne({ username }, function(err, user){
if(err) reject(new Error(err))
if(user) reject({ error : "Please use unique username"});
resolve();
})
});
// Checking existing Email
const existEmail = new Promise((resolve, reject) => {
UserModel.findOne({ email }, function(err, email){
if(err) reject(new Error(err))
if(email) reject({ error : "Please use unique Email"});
resolve();
})
});
Promise.all([existUsername, existEmail])
.then(() => {
if(password){
bcrypt.hash(password, 10)
.then( hashedPassword => {
const user = new UserModel({
username,
password: hashedPassword,
profile: profile || '',
email
});
// return save result as a response
user.save()
.then(result => res.status(201).send({ msg: "User Registered Successfully"}))
.catch(error => res.status(500).send({error}))
}).catch(error => {
return res.status(500).send({
error : "Enable to hashed password"
})
})
}
}).catch(error => {
return res.status(500).send({ error })
})
} catch (error) {
return res.status(500).send(error);
}
}
/** POST: http://localhost:8080/api/login
......
This diff is collapsed.
......@@ -11,6 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"bcrypt": "^5.1.0",
"cors": "^2.8.5",
"express": "^4.18.2",
"mongodb-memory-server": "^8.10.2",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment