From 09afba80dbd5b9726b530e317bdd01c0642f7735 Mon Sep 17 00:00:00 2001 From: ansuz Date: Tue, 14 Mar 2017 12:39:22 +0100 Subject: [PATCH] add support for logging in from register page --- www/common/login.js | 14 +++++++----- www/register/main.js | 52 +++++++++++++++++++++++++++++--------------- 2 files changed, 44 insertions(+), 22 deletions(-) diff --git a/www/common/login.js b/www/common/login.js index 99678b1dd..7576ff4d4 100644 --- a/www/common/login.js +++ b/www/common/login.js @@ -94,17 +94,21 @@ define([ res.realtime = rt.realtime; res.network = rt.network; + // they're registering... + res.userHash = opt.userHash; + res.userName = uname; + // they tried to just log in but there's no such user if (!isRegister && isProxyEmpty(rt.proxy)) { rt.network.disconnect(); // clean up after yourself return void cb('NO_SUCH_USER', res); } - // they're registering... - - res.userHash = opt.userHash; - res.userName = uname; - //res.displayName // TODO + // they tried to register, but those exact credentials exist + if (isRegister && !isProxyEmpty(rt.proxy)) { + rt.network.disconnect(); + return void cb('ALREADY_REGISTERED', res); + } cb(void 0, res); }); diff --git a/www/register/main.js b/www/register/main.js index c2ee41369..ef1ace07c 100644 --- a/www/register/main.js +++ b/www/register/main.js @@ -61,6 +61,25 @@ define([ var $register = $('button#register'); + var logMeIn = function (result) { + localStorage.User_hash = result.userHash; + Cryptpad.whenRealtimeSyncs(result.realtime, function () { + Cryptpad.login(result.userHash, result.userName, function () { + if (sessionStorage.redirectTo) { + var h = sessionStorage.redirectTo; + var parser = document.createElement('a'); + parser.href = h; + if (parser.origin === window.location.origin) { + delete sessionStorage.redirectTo; + window.location.href = h; + return; + } + } + window.location.href = '/drive/'; + }); + }); + }; + $register.click(function () { var uname = $uname.val(); var passwd = $passwd.val(); @@ -101,14 +120,27 @@ define([ Cryptpad.alert(Messages.login_invalPass); }); break; + case 'ALREADY_REGISTERED': + Cryptpad.removeLoadingScreen(function () { + Cryptpad.confirm(Messages.register_alreadyRegistered, function (yes) { + if (!yes) { return; } + result.proxy.login_name = uname; + + if (!result.proxy[Cryptpad.displayNameKey]) { + result.proxy[Cryptpad.displayNameKey] = uname; + } + Cryptpad.eraseTempSessionValues(); + logMeIn(result); + }); + }); + break; default: // UNHANDLED ERROR Cryptpad.errorLoadingScreen(Messages.login_unhandledError); } + return; } var proxy = result.proxy; - localStorage.User_hash = result.userHash; - Cryptpad.eraseTempSessionValues(); if (shouldImport) { sessionStorage.migrateAnonDrive = 1; @@ -118,21 +150,7 @@ define([ proxy[Cryptpad.displayNameKey] = uname; sessionStorage.createReadme = 1; - Cryptpad.whenRealtimeSyncs(result.realtime, function () { - Cryptpad.login(result.userHash, result.userName, function () { - if (sessionStorage.redirectTo) { - var h = sessionStorage.redirectTo; - var parser = document.createElement('a'); - parser.href = h; - if (parser.origin === window.location.origin) { - delete sessionStorage.redirectTo; - window.location.href = h; - return; - } - } - window.location.href = '/drive/'; - }); - }); + logMeIn(result); }); }, { ok: Messages.register_writtenPassword, //'I have written down my password, proceed',