3
3
*/
4
4
var mongoose = require ( 'mongoose' ) ,
5
5
Schema = mongoose . Schema ,
6
- bcrypt = require ( 'bcrypt ' ) ,
6
+ crypto = require ( 'crypto ' ) ,
7
7
_ = require ( 'underscore' ) ,
8
8
authTypes = [ 'github' , 'twitter' , 'facebook' , 'google' ] ;
9
9
@@ -17,6 +17,7 @@ var UserSchema = new Schema({
17
17
username : String ,
18
18
provider : String ,
19
19
hashed_password : String ,
20
+ salt : String ,
20
21
facebook : { } ,
21
22
twitter : { } ,
22
23
github : { } ,
@@ -28,6 +29,7 @@ var UserSchema = new Schema({
28
29
*/
29
30
UserSchema . virtual ( 'password' ) . set ( function ( password ) {
30
31
this . _password = password ;
32
+ this . salt = this . makeSalt ( ) ;
31
33
this . hashed_password = this . encryptPassword ( password ) ;
32
34
} ) . get ( function ( ) {
33
35
return this . _password ;
@@ -90,7 +92,17 @@ UserSchema.methods = {
90
92
* @api public
91
93
*/
92
94
authenticate : function ( plainText ) {
93
- return bcrypt . compareSync ( plainText , this . hashed_password ) ;
95
+ return this . encryptPassword ( plainText ) === this . hashed_password ;
96
+ } ,
97
+
98
+ /**
99
+ * Make salt
100
+ *
101
+ * @return {String }
102
+ * @api public
103
+ */
104
+ makeSalt : function ( ) {
105
+ return Math . round ( ( new Date ( ) . valueOf ( ) * Math . random ( ) ) ) + '' ;
94
106
} ,
95
107
96
108
/**
@@ -102,7 +114,7 @@ UserSchema.methods = {
102
114
*/
103
115
encryptPassword : function ( password ) {
104
116
if ( ! password ) return '' ;
105
- return bcrypt . hashSync ( password , 10 ) ;
117
+ return crypto . createHmac ( 'sha1' , this . salt ) . update ( password ) . digest ( 'hex' ) ;
106
118
}
107
119
} ;
108
120
0 commit comments