Skip to content

Commit 588f37d

Browse files
shortid implemented
1 parent da4856d commit 588f37d

File tree

2 files changed

+114
-78
lines changed

2 files changed

+114
-78
lines changed

server/controllers/signup.js

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,39 @@
1+
const User = require("../models/user");
2+
const shortId = require("shortid")
3+
14
exports.signup = (req,res)=>{
2-
const {name,email,password} = req.body
3-
res.json({
4-
user:{name,email,password}
5+
// const {name,email,password} = req.body
6+
// res.json({
7+
// user:{name,email,password}
8+
// });
9+
10+
// if user exist
11+
User.findOne({email: req.body.email}).exec((err,user)=>{
12+
if(user){
13+
return res.status(400).json({
14+
error: "Email already exists"
15+
})
16+
}
17+
// if not
18+
const {name,email,password} = req.body
19+
let username = shortId.generate()
20+
let profile = `${process.env.CLIENT_URL}/profile/${username}`
21+
22+
// create a new user
23+
let newUser = new User({name,email,password,profile,username})
24+
// save that user
25+
newUser.save((err,success)=>{
26+
if(err){
27+
return res.status(400).json({
28+
error:err
29+
})
30+
}
31+
res.json({
32+
message: "Completed Signup process.Please Login to continue"
33+
})
34+
// res.json({
35+
// user: success
36+
// })
37+
})
538
})
6-
}
39+
};

server/models/user.js

Lines changed: 77 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,89 @@
11
const mongoose = require('mongoose');
2-
const crypto = require("crypto");
2+
const crypto = require('crypto');
33

4-
const UserSchema = new mongoose.Schema({
5-
username: {
6-
type: String,
7-
required: true,
8-
trim:true,
9-
unique: true,
10-
max:32,
11-
index:true,
12-
lowercase:true
13-
},
14-
name: {
15-
type: String,
16-
required: true,
17-
trim:true,
18-
max:32,
19-
},
20-
email: {
21-
type: String,
22-
required: true,
23-
trim:true,
24-
unique: true,
25-
lowercase:true
26-
},
27-
profile: {
28-
type: String,
29-
required: true,
30-
}
31-
hashed_password: {
32-
type: String,
33-
required: true
34-
},
35-
salt: String,
36-
about:{
37-
type:String
38-
},
39-
role:{
40-
type:Number,
41-
trim:true
42-
},
43-
photo:{
44-
data:Buffer,
45-
contentType: String
4+
const UserSchema = new mongoose.Schema(
5+
{
6+
username: {
7+
type: String,
8+
trim: true,
9+
required: true,
10+
max: 12,
11+
unique: true,
12+
index: true,
13+
lowercase: true
14+
},
15+
name: {
16+
type: String,
17+
trim: true,
18+
required: true,
19+
max: 32
20+
},
21+
email: {
22+
type: String,
23+
trim: true,
24+
required: true,
25+
unique: true,
26+
lowercase: true
27+
},
28+
profile: {
29+
type: String,
30+
required: true
31+
},
32+
hashed_password: {
33+
type: String,
34+
required: true
35+
},
36+
salt: String,
37+
about: {
38+
type: String
39+
},
40+
role: {
41+
type: Number,
42+
default: 0
43+
},
44+
photo: {
45+
data: Buffer,
46+
contentType: String
47+
},
48+
resetPasswordLink: {
49+
data: String,
50+
default: ''
51+
}
4652
},
47-
resetPasswordLink:{
48-
data:String,
49-
deafult:""
50-
}
51-
},{timestamp: true});
53+
{ timestamps: true }
54+
);
5255

53-
UserSchema.virtual("password").set(function(password){
54-
// create a temp var _password
55-
this._password = password
56-
// generate salt
57-
this.salt= this.makeSalt()
58-
// encrypt password
59-
this.hashed_password = this.encryptPassword(password);
60-
}).get(function(){
61-
return this._password;
62-
});
63-
64-
UserSchema.methods = {
65-
66-
authenticate: function(plainText){
67-
return this.encryptPassword(plainText) === this.hashed_password;
68-
},
56+
UserSchema.virtual('password').set(function(password) {
57+
// create a temporarity variable called _password
58+
this._password = password;
59+
// generate salt
60+
this.salt = this.makeSalt();
61+
// encryptPassword
62+
this.hashed_password = this.encryptPassword(password);
63+
}).get(function() {
64+
return this._password;
65+
});
6966

67+
UserSchema.methods = {
68+
authenticate: function(plainText) {
69+
return this.encryptPassword(plainText) === this.hashed_password;
70+
},
7071

71-
encryptPassword: function(password){
72-
if(!password) return ""
72+
encryptPassword: function(password) {
73+
if (!password) return '';
7374
try {
74-
return crypto.createHmac("sha1",this.salt).update(password).digest("hex")
75-
} catch(err) {
76-
return ""
75+
return crypto
76+
.createHmac('sha1', this.salt)
77+
.update(password)
78+
.digest('hex');
79+
} catch (err) {
80+
return '';
7781
}
7882
},
7983

80-
makeSalt: function(){
81-
return Math.round(new Date().valueOf() * Math.random()) + "";
84+
makeSalt: function() {
85+
return Math.round(new Date().valueOf() * Math.random()) + '';
8286
}
83-
}
84-
87+
};
8588

86-
module.exports = mongoose.model("Users",UserSchema);
89+
module.exports = mongoose.model('Users', UserSchema);

0 commit comments

Comments
 (0)