From edc122f63c2e01a0fd96db92beb2f8f404ea9347 Mon Sep 17 00:00:00 2001 From: MIMIZA Date: Fri, 19 Jun 2020 12:38:52 +0700 Subject: [PATCH] Improve user.recall() Wrap JSON.parse() in a try{}catch{} --- sea.js | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/sea.js b/sea.js index df43f647..dbade2bb 100644 --- a/sea.js +++ b/sea.js @@ -825,12 +825,12 @@ return gun; } // now that we have created a user, we want to authenticate them! - User.prototype.auth = function(){ - const alias = typeof arguments[0] === 'string' ? arguments[0] : null - const pass = alias && typeof arguments[1] === 'string' ? arguments[1] : null - const pair = typeof arguments[0] === 'object' && (arguments[0].pub || arguments[0].epub) ? arguments[0] : typeof arguments[1] === 'object' && (arguments[1].pub || arguments[1].epub) ? arguments[1] : null - const cb = Array.prototype.slice.call(arguments).filter(arg => typeof arg === 'function')[0] || function(){} // cb now can stand anywhere, after alias/pass or pair - const opt = arguments && arguments.length > 1 && typeof arguments[arguments.length-1] === 'object' ? arguments[arguments.length-1] : {} // opt is always the last parameter which typeof === 'object' and stands after cb + User.prototype.auth = function(...args){ + const alias = typeof args[0] === 'string' ? args[0] : null + const pass = alias && typeof args[1] === 'string' ? args[1] : null + const pair = typeof args[0] === 'object' && (args[0].pub || args[0].epub) ? args[0] : typeof args[1] === 'object' && (args[1].pub || args[1].epub) ? args[1] : null + const cb = args.filter(arg => typeof arg === 'function')[0] || function(){} // cb now can stand anywhere, after alias/pass or pair + const opt = args && args.length > 1 && typeof args[args.length-1] === 'object' ? args[args.length-1] : {} // opt is always the last parameter which typeof === 'object' and stands after cb var gun = this, cat = (gun._), root = gun.back(-1); @@ -1002,15 +1002,18 @@ opt = opt || {}; if(opt && opt.sessionStorage){ if(SEA.window){ - try{var sS = {}; - sS = window.sessionStorage; - if(sS){ - (root._).opt.remember = true; - ((gun.back('user')._).opt||opt).remember = true; - if(sS.recall || sS.pair){ - root.user().auth(JSON.parse(sS.pair), cb); // pair is more reliable than alias/pass + try{ + var sS = {}; + sS = window.sessionStorage; + if(sS){ + (root._).opt.remember = true; + ((gun.back('user')._).opt||opt).remember = true; + if(sS.recall || sS.pair){ + try{ + root.user().auth(JSON.parse(sS.pair), cb); // pair is more reliable than alias/pass + }catch(e){} + } } - } }catch(e){} } return gun;