-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da4856d
commit 588f37d
Showing
2 changed files
with
114 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,39 @@ | ||
const User = require("../models/user"); | ||
const shortId = require("shortid") | ||
|
||
exports.signup = (req,res)=>{ | ||
const {name,email,password} = req.body | ||
res.json({ | ||
user:{name,email,password} | ||
// const {name,email,password} = req.body | ||
// res.json({ | ||
// user:{name,email,password} | ||
// }); | ||
|
||
// if user exist | ||
User.findOne({email: req.body.email}).exec((err,user)=>{ | ||
if(user){ | ||
return res.status(400).json({ | ||
error: "Email already exists" | ||
}) | ||
} | ||
// if not | ||
const {name,email,password} = req.body | ||
let username = shortId.generate() | ||
let profile = `${process.env.CLIENT_URL}/profile/${username}` | ||
|
||
// create a new user | ||
let newUser = new User({name,email,password,profile,username}) | ||
// save that user | ||
newUser.save((err,success)=>{ | ||
if(err){ | ||
return res.status(400).json({ | ||
error:err | ||
}) | ||
} | ||
res.json({ | ||
message: "Completed Signup process.Please Login to continue" | ||
}) | ||
// res.json({ | ||
// user: success | ||
// }) | ||
}) | ||
}) | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,89 @@ | ||
const mongoose = require('mongoose'); | ||
const crypto = require("crypto"); | ||
const crypto = require('crypto'); | ||
|
||
const UserSchema = new mongoose.Schema({ | ||
username: { | ||
type: String, | ||
required: true, | ||
trim:true, | ||
unique: true, | ||
max:32, | ||
index:true, | ||
lowercase:true | ||
}, | ||
name: { | ||
type: String, | ||
required: true, | ||
trim:true, | ||
max:32, | ||
}, | ||
email: { | ||
type: String, | ||
required: true, | ||
trim:true, | ||
unique: true, | ||
lowercase:true | ||
}, | ||
profile: { | ||
type: String, | ||
required: true, | ||
} | ||
hashed_password: { | ||
type: String, | ||
required: true | ||
}, | ||
salt: String, | ||
about:{ | ||
type:String | ||
}, | ||
role:{ | ||
type:Number, | ||
trim:true | ||
}, | ||
photo:{ | ||
data:Buffer, | ||
contentType: String | ||
const UserSchema = new mongoose.Schema( | ||
{ | ||
username: { | ||
type: String, | ||
trim: true, | ||
required: true, | ||
max: 12, | ||
unique: true, | ||
index: true, | ||
lowercase: true | ||
}, | ||
name: { | ||
type: String, | ||
trim: true, | ||
required: true, | ||
max: 32 | ||
}, | ||
email: { | ||
type: String, | ||
trim: true, | ||
required: true, | ||
unique: true, | ||
lowercase: true | ||
}, | ||
profile: { | ||
type: String, | ||
required: true | ||
}, | ||
hashed_password: { | ||
type: String, | ||
required: true | ||
}, | ||
salt: String, | ||
about: { | ||
type: String | ||
}, | ||
role: { | ||
type: Number, | ||
default: 0 | ||
}, | ||
photo: { | ||
data: Buffer, | ||
contentType: String | ||
}, | ||
resetPasswordLink: { | ||
data: String, | ||
default: '' | ||
} | ||
}, | ||
resetPasswordLink:{ | ||
data:String, | ||
deafult:"" | ||
} | ||
},{timestamp: true}); | ||
{ timestamps: true } | ||
); | ||
|
||
UserSchema.virtual("password").set(function(password){ | ||
// create a temp var _password | ||
this._password = password | ||
// generate salt | ||
this.salt= this.makeSalt() | ||
// encrypt password | ||
this.hashed_password = this.encryptPassword(password); | ||
}).get(function(){ | ||
return this._password; | ||
}); | ||
|
||
UserSchema.methods = { | ||
|
||
authenticate: function(plainText){ | ||
return this.encryptPassword(plainText) === this.hashed_password; | ||
}, | ||
UserSchema.virtual('password').set(function(password) { | ||
// create a temporarity variable called _password | ||
this._password = password; | ||
// generate salt | ||
this.salt = this.makeSalt(); | ||
// encryptPassword | ||
this.hashed_password = this.encryptPassword(password); | ||
}).get(function() { | ||
return this._password; | ||
}); | ||
|
||
UserSchema.methods = { | ||
authenticate: function(plainText) { | ||
return this.encryptPassword(plainText) === this.hashed_password; | ||
}, | ||
|
||
encryptPassword: function(password){ | ||
if(!password) return "" | ||
encryptPassword: function(password) { | ||
if (!password) return ''; | ||
try { | ||
return crypto.createHmac("sha1",this.salt).update(password).digest("hex") | ||
} catch(err) { | ||
return "" | ||
return crypto | ||
.createHmac('sha1', this.salt) | ||
.update(password) | ||
.digest('hex'); | ||
} catch (err) { | ||
return ''; | ||
} | ||
}, | ||
|
||
makeSalt: function(){ | ||
return Math.round(new Date().valueOf() * Math.random()) + ""; | ||
makeSalt: function() { | ||
return Math.round(new Date().valueOf() * Math.random()) + ''; | ||
} | ||
} | ||
|
||
}; | ||
|
||
module.exports = mongoose.model("Users",UserSchema); | ||
module.exports = mongoose.model('Users', UserSchema); |