前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【salesforce】community site popup page 如果登录一次,切换tab不用重新输入邮箱密码(LWC)

【salesforce】community site popup page 如果登录一次,切换tab不用重新输入邮箱密码(LWC)

原创
作者头像
Merry he
修改2023-05-11 17:22:19
3980
修改2023-05-11 17:22:19
举报
文章被收录于专栏:salesforce学习salesforce学习

需求是:guest user点击site link之后,popup page之后输入邮箱和code验证之后,切换tab不用重新输入。

方案1: 使用UserInfo.getSessionId() ,记住guest user的sessionid是唯一的,然后加载的时候根据sessionid去判断

JS:

代码语言:txt
复制
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:

代码语言:txt
复制
@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:

代码语言:txt
复制
          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());
```   				
代码语言:txt
复制

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

代码语言:javascript
复制
```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,还是能取到值

  1. 切换浏览器tab,尝试get 发现娶不到值。

所以使用sessionstorage也不行

localStorage同样也不行

他俩的区别请参考:

方案7:apex中call apexpage试试,使用ApexPages.currentPage()呢

参考例子:

https://salesforce.stackexchange.com/questions/298379/access-cookies-in-lwc-component#:~:text=On%20the%20client%20side%2C%20the%20LWC%20component%20is,is%20due%20to%20the%20%22Secure%22%20Locker%20API%3A%20https%3A%2F%2Fdeveloper.salesforce.com%2Fdocs%2Fcomponent-library%2Ftools%2Flocker-service-viewer.

也不行

方案8:在vf page中使用** {!$Api.Session_ID} **

参考link:https://www.salesforcecodecrack.com/2019/11/session-id-of-current-user-in-lightning-context.html

vfpage:

代码语言:javascript
复制
```js
<apex:page contentType="application/json">
'----------------' + {!$Api.Session_ID} + '-----------------'
</apex:page>

权限给与完,之后验证:

admin可以获取到sessionid

guest user获取不到sessionid

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档