Solving X11 connection rejected problem with snap

If you have connected a remote server while SSH XForwarding enabled and try to run a X11 program which was installed with snap, you may get a X11 connection reject error like this:

$ scrcpy
scrcpy 1.24 <https://github.com/Genymobile/scrcpy>
/usr/local/share/scrcpy/scrcpy-server: 1 file pushed. 2.7 MB/s (41159 bytes in 0.015s)
X11 connection rejected because of wrong authentication.
Segmentation fault (core dumped)

The solution is simple but it is hard to find it anywhere else. Just give the XAUTHORITY environment variable before starting the command like in the example below:

$ XAUTHORITY=~/.Xauthority scrcpy
...