11'use strict' ;
2-
2+ /*eslint no-invalid-this:0*/
33import crypto from 'crypto' ;
4- import mongoose from 'mongoose' ;
54mongoose . Promise = require ( 'bluebird' ) ;
6- import { Schema } from 'mongoose' ; < % if ( filters . oauth ) { % >
5+ import mongoose , { Schema } from 'mongoose' ; < % if ( filters . oauth ) { % >
76
87const authTypes = [ 'github' , 'twitter' , 'facebook' , 'google' ] ; < % } % >
98
@@ -12,13 +11,17 @@ var UserSchema = new Schema({
1211 email : {
1312 type : String ,
1413 lowercase : true ,
15- required : < % if ( filters . oauth ) { % > function ( ) {
16- if ( authTypes . indexOf ( this . provider ) === - 1 ) {
14+ < % _ if ( filters . oauth ) { - % >
15+ required ( ) {
16+ if ( authTypes . indexOf ( this . provider ) === - 1 ) {
1717 return true ;
1818 } else {
1919 return false ;
2020 }
21- } < % } else { % > true < % } % >
21+ }
22+ < % _ } else { - % >
23+ required : true
24+ < % _ } - % >
2225 } ,
2326 role : {
2427 type : String ,
@@ -27,17 +30,17 @@ var UserSchema = new Schema({
2730 password : {
2831 type : String ,
2932 required : < % if ( filters . oauth ) { % > function ( ) {
30- if ( authTypes . indexOf ( this . provider ) === - 1 ) {
33+ if ( authTypes . indexOf ( this . provider ) === - 1 ) {
3134 return true ;
3235 } else {
3336 return false ;
3437 }
3538 } < % } else { % > true < % } % >
3639 } ,
3740 provider : String ,
38- salt : String < % if ( filters . oauth ) { % > , < % if ( filters . facebookAuth ) { % >
39- facebook : { } , < % } % > < % if ( filters . twitterAuth ) { % >
40- twitter : { } , < % } % > < % if ( filters . googleAuth ) { % >
41+ salt : String < % if ( filters . oauth ) { % > , < % if ( filters . facebookAuth ) { % >
42+ facebook : { } , < % } % > < % if ( filters . twitterAuth ) { % >
43+ twitter : { } , < % } % > < % if ( filters . googleAuth ) { % >
4144 google : { } , < % } % >
4245 github: { } < % } % >
4346} ) ;
@@ -51,8 +54,8 @@ UserSchema
5154 . virtual ( 'profile' )
5255 . get ( function ( ) {
5356 return {
54- ' name' : this . name ,
55- ' role' : this . role
57+ name : this . name ,
58+ role : this . role
5659 } ;
5760 } ) ;
5861
@@ -61,8 +64,8 @@ UserSchema
6164 . virtual ( 'token' )
6265 . get ( function ( ) {
6366 return {
64- ' _id' : this . _id ,
65- ' role' : this . role
67+ _id : this . _id ,
68+ role : this . role
6669 } ;
6770 } ) ;
6871
@@ -73,18 +76,20 @@ UserSchema
7376// Validate empty email
7477UserSchema
7578 . path ( 'email' )
76- . validate ( function ( email ) { < % if ( filters . oauth ) { % >
77- if ( authTypes . indexOf ( this . provider ) !== - 1 ) {
79+ . validate ( function ( email ) {
80+ < % _ if ( filters . oauth ) { - % >
81+ if ( authTypes . indexOf ( this . provider ) !== - 1 ) {
7882 return true ;
79- } < % } % >
83+ }
84+ < % _ } - % >
8085 return email.length;
8186 } , 'Email cannot be blank' ) ;
8287
8388// Validate empty password
8489UserSchema
8590 . path ( 'password' )
86- . validate ( function ( password ) { < % if ( filters . oauth ) { % >
87- if ( authTypes . indexOf ( this . provider ) !== - 1 ) {
91+ . validate ( function ( password ) { < % if ( filters . oauth ) { % >
92+ if ( authTypes . indexOf ( this . provider ) !== - 1 ) {
8893 return true ;
8994 } < % } % >
9095 return password . length ;
@@ -94,15 +99,16 @@ UserSchema
9499UserSchema
95100 . path ( 'email' )
96101 . validate ( function ( value , respond ) {
97- var self = this ;
98- < % _ if ( filters . oauth ) { _ % >
99- if ( authTypes . indexOf ( this . provider ) !== - 1 ) {
102+ < % _ if ( filters . oauth ) { - % >
103+ if ( authTypes . indexOf ( this . provider ) !== - 1 ) {
100104 return respond ( true ) ;
101- } < % } % >
105+ }
106+
107+ < % _ } - % >
102108 return this.constructor.findOne({ email : value } ).exec()
103- . then ( function ( user ) {
104- if ( user ) {
105- if ( self . id === user . id ) {
109+ .then(user = > {
110+ if ( user ) {
111+ if ( this . id === user . id ) {
106112 return respond ( true ) ;
107113 }
108114 return respond(false);
@@ -124,30 +130,30 @@ var validatePresenceOf = function(value) {
124130UserSchema
125131 . pre ( 'save' , function ( next ) {
126132 // Handle new/update passwords
127- if ( ! this . isModified ( 'password' ) ) {
133+ if ( ! this . isModified ( 'password' ) ) {
128134 return next ( ) ;
129135 }
130136
131- if ( ! validatePresenceOf ( this . password ) ) {
132- < % if ( filters . oauth ) { % > if ( authTypes . indexOf ( this . provider ) === - 1 ) {
133- < % } % > return next ( new Error ( 'Invalid password' ) ) ; < % if ( filters . oauth ) { % >
137+ if ( ! validatePresenceOf ( this . password ) ) {
138+ < % if ( filters . oauth ) { % > if ( authTypes . indexOf ( this . provider ) === - 1 ) {
139+ < % } % > return next ( new Error ( 'Invalid password' ) ) ; < % if ( filters . oauth ) { % >
134140 } else {
135141 return next ( ) ;
136142 } < % } % >
137143 }
138144
139145 // Make salt with a callback
140146 this . makeSalt ( ( saltErr , salt ) => {
141- if ( saltErr ) {
147+ if ( saltErr ) {
142148 return next ( saltErr ) ;
143149 }
144150 this . salt = salt ;
145151 this . encryptPassword ( this . password , ( encryptErr , hashedPassword ) => {
146- if ( encryptErr ) {
152+ if ( encryptErr ) {
147153 return next ( encryptErr ) ;
148154 }
149155 this . password = hashedPassword ;
150- next ( ) ;
156+ return next ( ) ;
151157 } ) ;
152158 } ) ;
153159 } ) ;
@@ -165,19 +171,19 @@ UserSchema.methods = {
165171 * @api public
166172 */
167173 authenticate ( password , callback ) {
168- if ( ! callback ) {
174+ if ( ! callback ) {
169175 return this . password === this . encryptPassword ( password ) ;
170176 }
171177
172178 this . encryptPassword ( password , ( err , pwdGen ) => {
173- if ( err ) {
179+ if ( err ) {
174180 return callback ( err ) ;
175181 }
176182
177- if ( this . password === pwdGen ) {
178- callback ( null , true ) ;
183+ if ( this . password === pwdGen ) {
184+ return callback ( null , true ) ;
179185 } else {
180- callback ( null , false ) ;
186+ return callback ( null , false ) ;
181187 }
182188 } ) ;
183189 } ,
@@ -193,24 +199,24 @@ UserSchema.methods = {
193199 makeSalt ( byteSize , callback ) {
194200 var defaultByteSize = 16 ;
195201
196- if ( typeof arguments [ 0 ] === 'function' ) {
202+ if ( typeof arguments [ 0 ] === 'function' ) {
197203 callback = arguments [ 0 ] ;
198204 byteSize = defaultByteSize ;
199- } else if ( typeof arguments [ 1 ] === 'function' ) {
205+ } else if ( typeof arguments [ 1 ] === 'function' ) {
200206 callback = arguments [ 1 ] ;
201207 } else {
202208 throw new Error ( 'Missing Callback' ) ;
203209 }
204210
205- if ( ! byteSize ) {
211+ if ( ! byteSize ) {
206212 byteSize = defaultByteSize ;
207213 }
208214
209215 return crypto . randomBytes ( byteSize , ( err , salt ) => {
210- if ( err ) {
211- callback ( err ) ;
216+ if ( err ) {
217+ return callback ( err ) ;
212218 } else {
213- callback ( null , salt . toString ( 'base64' ) ) ;
219+ return callback ( null , salt . toString ( 'base64' ) ) ;
214220 }
215221 } ) ;
216222 } ,
@@ -224,8 +230,8 @@ UserSchema.methods = {
224230 * @api public
225231 */
226232 encryptPassword ( password , callback ) {
227- if ( ! password || ! this . salt ) {
228- if ( ! callback ) {
233+ if ( ! password || ! this . salt ) {
234+ if ( ! callback ) {
229235 return null ;
230236 } else {
231237 return callback ( 'Missing password or salt' ) ;
@@ -236,13 +242,13 @@ UserSchema.methods = {
236242 var defaultKeyLength = 64 ;
237243 var salt = new Buffer ( this . salt , 'base64' ) ;
238244
239- if ( ! callback ) {
245+ if ( ! callback ) {
240246 return crypto . pbkdf2Sync ( password , salt , defaultIterations , defaultKeyLength )
241247 . toString ( 'base64' ) ;
242248 }
243249
244250 return crypto . pbkdf2 ( password , salt , defaultIterations , defaultKeyLength , ( err , key ) => {
245- if ( err ) {
251+ if ( err ) {
246252 callback ( err ) ;
247253 } else {
248254 callback ( null , key . toString ( 'base64' ) ) ;
0 commit comments