需求是:guest user点击site link之后,popup page之后输入邮箱和code验证之后,切换tab不用重新输入。
方案1: 使用UserInfo.getSessionId() ,记住guest user的sessionid是唯一的,然后加载的时候根据sessionid去判断
JS:
connectedCallback(){
getAccessFlag()
.then(result => {
if (result) {
this.verifyPageDisplayFlag = true;
} else {
this.pupUpFlag =false;
}
})
.catch(error => {
console.log(JSON.stringify(error));
if(error.body.pageErrors)
this.errMsg = error.body.pageErrors[0].message;
this.showAlert = true;
this.error = error;
this.dispatchEvent(new ShowToastEvent({
title: 'Error',
message: 'System error,please connect adminstrator',
variant: 'error'
}));
});
}
APEX:
@AuraEnabled(cacheable=false)
public static Boolean getAccessFlag() {
System.debug('>>>>>>>getUserId'+UserInfo.getSessionId());**---guestuser的时候-取得是空**
Boolean isAccessFlag = true;
String userType = UserInfo.getUserType();
System.debug('>>>>>>>getUserId'+UserInfo.getUserId());
if (userType != 'Guest') {
isAccessFlag = false;
}
return isAccessFlag;
}
发现guest user返回的UserInfo.getSessionId()是空,其它user正常。
相关文档也有记述:https://help.salesforce.com/s/articleView?id=000387009&type=1
此方案pass掉
方案2:后台使用Site.login方式强制登录一个user
apex:
String startUrl = Site.getPathPrefix();
String strUserName = 'x'x'xxxx';
String strpwd = 'x'xxx@';
Site.login(strUserName, strpwd, startUrl);
User user = [SELECT Id, UserName FROM User where UserName = :strUserName limit 1];
System.debug('>>>>>>>UserName'+user.UserName);
System.debug('>>>>>>>id'+user.id);
System.debug('>>>>>>>UserInfo.getuserId()'+UserInfo.getuserId());
```
userinfo没有使用site.loginuser的信息,userinfo,getssionid 依旧为空。此方案pass掉。
参考连接:https://developer.salesforce.com/forums/?id=9060G0000005RVbQAM
方案3:直接使用userid试下
切换浏览器重新输入同样的邮箱登录或者输入不同的邮箱发现id是一样的,由此看来guestuser.id 是一个。
此方案pass掉
方案4:
尝试给给当前guest user 插入一个flag ,
刚验证成功----user表上记录flag为true,
connectcallback中判断这个flag是否为true,true就默认登录
不同页面进去 guest userid是唯一的,此方案pass掉
**方案5:使用document.cookie
```js
connectedCallback(){
console.log(' 21connectedCallback >>>>>>>cookie===>' + document.cookie);
setTimeout(() => {
console.log(' 23connectedCallback >>>>>>>cookie===>' + document.cookie); }, 10000);
}
结果发现lwc会拦截一部分cookie,尝试加完settimeout也显示不全,如下图:
以为lwc考虑到安全问题,故意设置的cookie不让显示全。
此方案pass掉
方案6:使用sessionStorage.setItem('email','123');
1.首先在浏览器中set然后get如下图:
2.其次刷新浏览器,再尝试get,还是能取到值
所以使用sessionstorage也不行
localStorage同样也不行
他俩的区别请参考:
方案7:apex中call apexpage试试,使用ApexPages.currentPage()呢
参考例子:
也不行
方案8:在vf page中使用** {!$Api.Session_ID} **
参考link:https://www.salesforcecodecrack.com/2019/11/session-id-of-current-user-in-lightning-context.html
vfpage:
```js
<apex:page contentType="application/json">
'----------------' + {!$Api.Session_ID} + '-----------------'
</apex:page>
权限给与完,之后验证:
admin可以获取到sessionid
guest user获取不到sessionid
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。