2014-05-15 11:20:23 -04:00
|
|
|
settings = require("settings-sharelatex")
|
|
|
|
async = require("async")
|
|
|
|
UserGetter = require("../User/UserGetter")
|
2015-05-27 10:06:36 -04:00
|
|
|
OneTimeTokenHandler = require("../Security/OneTimeTokenHandler")
|
2014-05-15 11:20:23 -04:00
|
|
|
EmailHandler = require("../Email/EmailHandler")
|
|
|
|
AuthenticationManager = require("../Authentication/AuthenticationManager")
|
2014-05-15 13:08:21 -04:00
|
|
|
logger = require("logger-sharelatex")
|
2014-05-15 11:20:23 -04:00
|
|
|
|
|
|
|
module.exports =
|
|
|
|
|
2014-08-08 06:41:54 -04:00
|
|
|
generateAndEmailResetToken:(email, callback = (error, exists) ->)->
|
2018-05-23 10:12:23 -04:00
|
|
|
UserGetter.getUserByMainEmail email, (err, user)->
|
2014-05-15 11:20:23 -04:00
|
|
|
if err then return callback(err)
|
2015-04-14 08:11:49 -04:00
|
|
|
if !user? or user.holdingAccount
|
2014-05-15 13:08:21 -04:00
|
|
|
logger.err email:email, "user could not be found for password reset"
|
2014-08-08 06:41:54 -04:00
|
|
|
return callback(null, false)
|
2015-05-26 10:24:09 -04:00
|
|
|
OneTimeTokenHandler.getNewToken user._id, (err, token)->
|
2014-05-15 11:50:38 -04:00
|
|
|
if err then return callback(err)
|
|
|
|
emailOptions =
|
|
|
|
to : email
|
2015-04-30 06:59:44 -04:00
|
|
|
setNewPasswordUrl : "#{settings.siteUrl}/user/password/set?passwordResetToken=#{token}&email=#{encodeURIComponent(email)}"
|
2014-08-08 06:41:54 -04:00
|
|
|
EmailHandler.sendEmail "passwordResetRequested", emailOptions, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
callback null, true
|
2014-05-15 11:20:23 -04:00
|
|
|
|
2015-12-11 06:30:06 -05:00
|
|
|
setNewUserPassword: (token, password, callback = (error, found, user_id) ->)->
|
2015-05-26 10:24:09 -04:00
|
|
|
OneTimeTokenHandler.getValueFromTokenAndExpire token, (err, user_id)->
|
2014-05-15 11:20:23 -04:00
|
|
|
if err then return callback(err)
|
|
|
|
if !user_id?
|
2015-12-11 06:30:06 -05:00
|
|
|
return callback null, false, null
|
2014-10-08 12:18:24 -04:00
|
|
|
AuthenticationManager.setUserPassword user_id, password, (err) ->
|
|
|
|
if err then return callback(err)
|
2015-12-11 06:30:06 -05:00
|
|
|
callback null, true, user_id
|