Improve user.recall()

Wrap JSON.parse() in a try{}catch{}
This commit is contained in:
MIMIZA 2020-06-19 12:38:52 +07:00
parent e88a120a4e
commit edc122f63c

31
sea.js
View File

@ -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;