Roughly working account-merge-by-email callback endpoint

This commit is contained in:
Shane Kilkelly 2018-09-11 09:54:13 +01:00
parent 958c7a19b6
commit 6cfe68db26
3 changed files with 43 additions and 6 deletions

View file

@ -1,5 +1,44 @@
UserGetter = require '../User/UserGetter'
UserUpdater = require '../User/UserUpdater'
OneTimeTokenHandler = require '../Security/OneTimeTokenHandler'
logger = require 'logger-sharelatex'
Settings = require 'settings-sharelatex'
module.exports = AccountMergeEmailController = module.exports = AccountMergeEmailController =
confirmMergeFromEmail: (req, res, next) -> confirmMergeFromEmail: (req, res, next) ->
return res.render('account_merge/finish', {}) token = req.query.token
if !token
return res.status(400).send()
OneTimeTokenHandler.getValueFromTokenAndExpire 'account-merge-email-to-ol', token, (err, data) ->
return next(err) if err?
if !data
return res.status(404).send()
if data.origin != 'sl'
logger.log {}, "Only sharelatex origin supported"
# TODO: better error handling
return res.status(501).send()
{ sl_id, v1_id, final_email } = data
UserGetter.getUser sl_id, {_id: 1, overleaf: 1}, (err, user) ->
return next(err) if err?
if !user?
logger.err {userId: sl_id}, 'SL user not found for account-merge'
# TODO: better error handling
return res.status(400).send()
if user?.overleaf?.id?
logger.err {userId: sl_id}, 'SL user is already linked to overleaf'
return res.status(400).send()
logger.log {sl_id, v1_id, final_email, origin: data.origin},
"[AccountMergeEmailController] about to merge sharelatex and overleaf-v1 accounts"
UserUpdater.updateUser sl_id, {
'$set': {
'overleaf.id': v1_id,
email: final_email
}
}, (err) ->
return next(err) if err?
res.render 'account_merge/finish', {
finalEmail: final_email,
continueUrl: "#{Settings.accountMerge.sharelatexHost}/user/login_to_ol_v2"
}

View file

@ -12,4 +12,6 @@ block content
div.container-fluid div.container-fluid
.row .row
.col-md-12 .col-md-12
p Test p New email is #{finalEmail}
p
a.btn.btn-lg.btn-success(href=continueUrl) Continue

View file

@ -8,7 +8,6 @@ define [
$scope.olEmail = "" $scope.olEmail = ""
$scope.errorCode = null $scope.errorCode = null
$scope.success = null $scope.success = null
console.log ">>>> here"
$scope.submitEmail = () -> $scope.submitEmail = () ->
return if !$scope.olEmail return if !$scope.olEmail
@ -16,15 +15,12 @@ define [
overleafEmail: $scope.olEmail overleafEmail: $scope.olEmail
_csrf: window.csrfToken _csrf: window.csrfToken
} }
console.log ">>>> sending email", data
$scope.errorCode = null $scope.errorCode = null
$http.post("/account-merge/email/overleaf", data) $http.post("/account-merge/email/overleaf", data)
.then (resp) -> .then (resp) ->
console.log ">>>> success", resp
$scope.errorCode = null $scope.errorCode = null
$scope.success = true $scope.success = true
.catch (resp) -> .catch (resp) ->
console.log ">>>> error", resp
$scope.errorCode = resp?.data?.errorCode || 'default_error' $scope.errorCode = resp?.data?.errorCode || 'default_error'
$scope.success = false $scope.success = false