11const 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