overleaf/services/web/app/coffee/Features/PasswordReset/PasswordResetHandler.coffee

33 lines
1.4 KiB
CoffeeScript
Raw Normal View History

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 =
generateAndEmailResetToken:(email, callback = (error, exists) ->)->
2014-05-15 11:50:38 -04:00
UserGetter.getUser email:email, (err, user)->
2014-05-15 11:20:23 -04:00
if err then return callback(err)
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"
return callback(null, false)
OneTimeTokenHandler.getNewToken user._id, (err, token)->
2014-05-15 11:50:38 -04:00
if err then return callback(err)
emailOptions =
to : email
setNewPasswordUrl : "#{settings.siteUrl}/user/password/set?passwordResetToken=#{token}&email=#{encodeURIComponent(email)}"
EmailHandler.sendEmail "passwordResetRequested", emailOptions, (error) ->
return callback(error) if error?
callback null, true
2014-05-15 11:20:23 -04:00
setNewUserPassword: (token, password, callback = (error, found, user_id) ->)->
OneTimeTokenHandler.getValueFromTokenAndExpire token, (err, user_id)->
2014-05-15 11:20:23 -04:00
if err then return callback(err)
if !user_id?
return callback null, false, null
AuthenticationManager.setUserPassword user_id, password, (err) ->
if err then return callback(err)
callback null, true, user_id