Solution to the BUG of Wails Unable to Use Cookies on MacOS

Publish: 2023-04-16 | Modify: 2023-04-16

Wails is a project that allows you to write desktop applications using Go and web technologies. Recently, while developing the AsBot client on MacOS, everything was working fine in development mode, but after compiling and packaging the application, I found that cookies were not working.

Before this, I had already packaged the Windows client and did not encounter this issue. It seems to be a bug. I have already reported the issue to the author and am waiting for further confirmation.

Debugging Wails

According to the official documentation of Wails, you can include the "-debug" parameter during packaging to preserve debug information in the application and allow the use of devtools in the application window.

The complete packaging command is:

wails build -debug

This way, you can continue using browser debugging mode to analyze and troubleshoot in the packaged files (not recommended to add the "-debug" parameter in production).

Reproducing the Issue

In the project, I used the "vue3-cookies" library initially, thinking it was the issue. So I switched to using native JavaScript methods:

document.cookie = "username=John Doe; expires=" + new Date(new Date().getTime() + (24 * 60 * 60 * 1000)).toUTCString() + "; path=/";

However, the following observations still persist (on MacOS):

  • In "wails dev" developer mode, everything works fine and cookies are set successfully.
  • In the compiled "wails build" package, cookies cannot be set or stored, and no errors are thrown.

Wails Environment and Version Information

The environment information where this bug occurred is as follows:

Wails CLI v2.4.1

Scanning system - Please wait (this may take a long time)...Done.

# System

OS           | MacOS   
Version      | 13.3.1  
ID           | 22E261  
Go Version   | go1.20.3
Platform     | darwin  
Architecture | amd64   

# Wails

Version | v2.4.1

# Dependencies

Dependency                | Package Name | Status    | Version  
Xcode command line tools  | N/A          | Installed | 2397     
npm                       | N/A          | Installed | 9.5.0    
*Xcode                    | N/A          | Available |          
*upx                      | N/A          | Installed | upx 4.0.2
*nsis                     | N/A          | Available |          
* - Optional Dependency

Reporting the Issue

So I opened an issue in the author's project, which can be found here: https://github.com/wailsapp/wails/issues/2590

Currently, the author has not provided a clear response, and I haven't figured out the exact reason either.

Workaround and Solution

For now, I have temporarily abandoned the use of cookies and switched to using the browser's built-in "localStorage" property to solve the issue. Compared to cookies, "localStorage" is easier to use, but it is not as flexible and secure as the cookies solution. For example, "localStorage" cannot set an expiration time or a "HttpOnly" attribute like cookies.

The usage of "localStorage" can be referred to here: Window localStorage Property

Conclusion

In general, if you are familiar with Golang and frontend development, Wails is a good tool for desktop application development. However, you may encounter some issues during usage, such as the inability to set cookies on MacOS. In such cases, we can use "localStorage" as an alternative solution. I hope this article can be helpful to Wails developers.


Comments