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

30 lines
1.2 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")
TokenGenerator = require("./TokenGenerator")
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-05-15 11:50:38 -04:00
generateAndEmailResetToken:(email, callback)->
UserGetter.getUser email:email, (err, user)->
2014-05-15 11:20:23 -04:00
if err then return callback(err)
2014-05-15 11:50:38 -04:00
if !user?
2014-05-15 13:08:21 -04:00
logger.err email:email, "user could not be found for password reset"
2014-05-15 11:20:23 -04:00
return callback("no user found")
2014-05-15 11:50:38 -04:00
TokenGenerator.getNewToken user._id, (err, token)->
if err then return callback(err)
emailOptions =
to : email
2014-05-15 12:58:25 -04:00
setNewPasswordUrl : "#{settings.siteUrl}/user/password/set?passwordResetToken=#{token}"
2014-05-15 11:50:38 -04:00
EmailHandler.sendEmail "passwordResetRequested", emailOptions, callback
2014-05-15 11:20:23 -04:00
2014-05-15 11:50:38 -04:00
setNewUserPassword: (token, password, callback)->
2014-05-15 11:20:23 -04:00
TokenGenerator.getUserIdFromToken token, (err, user_id)->
if err then return callback(err)
if !user_id?
2014-05-15 13:26:00 -04:00
logger.err user_id:user_id, "token for password reset did not find user_id"
2014-05-15 11:20:23 -04:00
return callback("no user found")
AuthenticationManager.setUserPassword user_id, password, callback