@@ -12,6 +12,7 @@ var Model = require('../loopback').Model
1212 , LocalStrategy = require ( 'passport-local' ) . Strategy
1313 , BaseAccessToken = require ( './access-token' )
1414 , DEFAULT_TTL = 1209600 // 2 weeks in seconds
15+ , DEFAULT_RESET_PW_TTL = 15 * 60 // 15 mins in seconds
1516 , DEFAULT_MAX_TTL = 31556926 ; // 1 year in seconds
1617
1718/**
@@ -235,6 +236,42 @@ User.confirm = function (uid, token, redirect, fn) {
235236 }
236237 } ) ;
237238}
239+
240+ User . resetPassword = function ( options , cb ) {
241+ var UserModel = this ;
242+ var ttl = UserModel . settings . resetPasswordTokenTTL || DEFAULT_RESET_PW_TTL ;
243+
244+ options = options || { } ;
245+ if ( typeof options . email === 'string' ) {
246+ UserModel . findOne ( { email : options . email } , function ( err , user ) {
247+ if ( err ) {
248+ cb ( err ) ;
249+ } else if ( user ) {
250+ // create a short lived access token for temp login to change password
251+ // TODO(ritch) - eventually this should only allow password change
252+ user . accessTokens . create ( { ttl : ttl } , function ( err , accessToken ) {
253+ if ( err ) {
254+ cb ( err ) ;
255+ } else {
256+ cb ( ) ;
257+ UserModel . emit ( 'resetPasswordRequest' , {
258+ email : options . email ,
259+ accessToken : accessToken
260+ } ) ;
261+ }
262+ } )
263+ } else {
264+ cb ( ) ;
265+ }
266+ } ) ;
267+ } else {
268+ var err = new Error ( 'email is required' ) ;
269+ err . statusCode = 400 ;
270+
271+ cb ( err ) ;
272+ }
273+ }
274+
238275/**
239276 * Setup an extended user model.
240277 */
@@ -286,6 +323,16 @@ User.setup = function () {
286323 }
287324 ) ;
288325
326+ loopback . remoteMethod (
327+ UserModel . resetPassword ,
328+ {
329+ accepts : [
330+ { arg : 'options' , type : 'object' , required : true , http : { source : 'body' } }
331+ ] ,
332+ http : { verb : 'post' , path : '/reset' }
333+ }
334+ ) ;
335+
289336 UserModel . on ( 'attached' , function ( ) {
290337 UserModel . afterRemote ( 'confirm' , function ( ctx , inst , next ) {
291338 if ( ctx . req ) {
0 commit comments