且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

关于自定义的登录机制在SAP Spartacus服务器端渲染(s-s-r)实施过程中遇到的问题

更新时间:2022-08-28 19:42:42

问题背景

某客户使用了第三方的Authentication service来实现Spartacus用户的登录机制:关于自定义的登录机制在SAP Spartacus服务器端渲染(s-s-r)实施过程中遇到的问题使用token,调用另一个API,获取user信息。


We found out that the problem was caused by using this.windowAdapter.getWindow().sessionStorage.* without previously checking if the sessionStorage is actually available. In s-s-r it was undefined.


If you wrap all the calls in an if (this.windowAdapter.getWindow().sessionStorage) {...} the PLP pages are being s-s-red correctly.


As an additional, browser’s storage (sessionStorage, localStorage) API is not available in s-s-r, therefore code defensively.


自开发的用户认证:


关于自定义的登录机制在SAP Spartacus服务器端渲染(s-s-r)实施过程中遇到的问题sessionStorage和localStorage都无法在s-s-r模式下工作。关于自定义的登录机制在SAP Spartacus服务器端渲染(s-s-r)实施过程中遇到的问题This is the reason of s-s-r not working on the pages where these objects are being used.


If window or localStorage objects are required to be used with s-s-r then include the library @ng-toolkit/universal .


An example of using window and localStorage objects with s-s-r can be referred at this article


***实践

***不要在s-s-r模式下进行用户认证(user Authentication)相关的逻辑:


关于自定义的登录机制在SAP Spartacus服务器端渲染(s-s-r)实施过程中遇到的问题解决方案

I can suggest you to try to inject the @Inject(PLATFORM_ID) protected platform: any (from @angular/core) to your custom-login.component.ts, and then check if the platform is a browser or a server with isPlatformBrowser() or isPlatformServer() ( both coming from @angular/common).