2014-05-15 15:20:23 +00:00
|
|
|
settings = require("settings-sharelatex")
|
|
|
|
async = require("async")
|
|
|
|
UserGetter = require("../User/UserGetter")
|
2014-05-16 09:43:55 +00:00
|
|
|
PasswordResetTokenHandler = require("./PasswordResetTokenHandler")
|
2014-05-15 15:20:23 +00:00
|
|
|
EmailHandler = require("../Email/EmailHandler")
|
|
|
|
AuthenticationManager = require("../Authentication/AuthenticationManager")
|
2014-05-15 17:08:21 +00:00
|
|
|
logger = require("logger-sharelatex")
|
2014-05-15 15:20:23 +00:00
|
|
|
|
|
|
|
module.exports =
|
|
|
|
|
2014-08-08 10:41:54 +00:00
|
|
|
generateAndEmailResetToken:(email, callback = (error, exists) ->)->
|
2014-05-15 15:50:38 +00:00
|
|
|
UserGetter.getUser email:email, (err, user)->
|
2014-05-15 15:20:23 +00:00
|
|
|
if err then return callback(err)
|
2014-05-15 15:50:38 +00:00
|
|
|
if !user?
|
2014-05-15 17:08:21 +00:00
|
|
|
logger.err email:email, "user could not be found for password reset"
|
2014-08-08 10:41:54 +00:00
|
|
|
return callback(null, false)
|
2014-05-16 09:43:55 +00:00
|
|
|
PasswordResetTokenHandler.getNewToken user._id, (err, token)->
|
2014-05-15 15:50:38 +00:00
|
|
|
if err then return callback(err)
|
|
|
|
emailOptions =
|
|
|
|
to : email
|
2014-05-15 16:58:25 +00:00
|
|
|
setNewPasswordUrl : "#{settings.siteUrl}/user/password/set?passwordResetToken=#{token}"
|
2014-08-08 10:41:54 +00:00
|
|
|
EmailHandler.sendEmail "passwordResetRequested", emailOptions, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback null, true
|
2014-05-15 15:20:23 +00:00
|
|
|
|
2014-10-08 16:18:24 +00:00
|
|
|
setNewUserPassword: (token, password, callback = (error, found) ->)->
|
2014-05-16 09:43:55 +00:00
|
|
|
PasswordResetTokenHandler.getUserIdFromTokenAndExpire token, (err, user_id)->
|
2014-05-15 15:20:23 +00:00
|
|
|
if err then return callback(err)
|
|
|
|
if !user_id?
|
2014-10-08 16:18:24 +00:00
|
|
|
return callback null, false
|
|
|
|
AuthenticationManager.setUserPassword user_id, password, (err) ->
|
|
|
|
if err then return callback(err)
|
|
|
|
callback null, true
|