Skip to content

Commit

Permalink
shortid implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
pramit-marattha committed Jan 11, 2021
1 parent da4856d commit 588f37d
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 78 deletions.
41 changes: 37 additions & 4 deletions server/controllers/signup.js
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
// })
})
})
}
};
151 changes: 77 additions & 74 deletions server/models/user.js
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);

0 comments on commit 588f37d

Please sign in to comment.