The documentation for AuthInterface.openUri() does not explain how to receive the response (no return parameter is indicated). The examples (Tutorial etc) only show how to do this in electron (using electron.app.on('open-url',...)).
So I would like to know how to pick up the auth URI response when I call AuthInterface.openUri() from a non-electron desktop app (not DOM API).
The authenticator will call an executable with the URI as an argument. With customExecPath this executable and the arguments can be configured. If customExecPath is ['C:\main.exe'], then the authenticator will call C:\main.exe <URI response>. Itās then up to the executable to read the URI.
This is also the reason my minimum viable example is a little complex.
In short: The Authenticator spawns an executable with the URI response as an argument.
Thanks, I have all that (thanks to your help last week) but I still donāt know how to pick up the response in the app.
It may be I need to use the single instance thingy @hunterlester pointed to last week but Iām not sure yet.
UPDATEā¦
Ah no, hunterās thing is also an electron function:
const shouldQuit = app.makeSingleInstance((commandLine, workingDirectory) => {
const uri = commandLine[commandLine.length - 1];
if (commandLine.length >= 2 && uri) {
sendResponse(uri);
}
// Someone tried to run a second instance, we should focus our window.
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
}
})
So I guess I need a similar thing for my app. Going back to your reply above, Iām not seeing my app being called by repeatedly being started again and again, recursively after each openUri() (although I think at one time last week that did happen but I think that was when I was still doing stuff in electron).
So I think that the mechanism you describe there @bzee is not yet calling the executable after a successful auth.
What you need to understand is that the script will actually be run twice. The first time, when you execute it, and the second time when itās run by the Authenticator.
My example runs the first time, calling openUri, then closes. The Authenticator then calls the script a second time (with the URI!) and it can continue connecting to the SAFE Network.
Now, this seems inconvenient, as you might want to run a single instance that runs inside of the cmd.exe or whatever that you call it from.
Yes, thatās why Electron is very much suited, as it can communicate with the āmain processā to pass the URI when itās called again by the Authenticator, while the first instance just keeps running to wait for that responseā¦
Thanks Benno, I understand the issues much better now, just not how to solve them. Also, as noted in my update above, Iām not sure my āscriptā is being called although it might just be that Iām not seeing it as all it does is output to the console. If it is āworkingā I guess Iāve just spawned an infinitely running chain!
Ok, I see how you are trying to do this via ipc, is that working?
Just a note for anyone follwing this topic. @bzee has solved this so expect more from him at some point, and I shall try to create a simple CLI boilerplate which anyone can use to build cross platform CLI apps that authenticate with SAFE Browser (based on JavaScript and nodejs - so very a powerful platform, just like other SAFE Apps).
Later maybe someone will I hope write an authenticator module which extends this ability to headless apps. Any takers?
@bzee Iāve created a stripped down cli boilerplate which includes just the code I need from @decorum-lib (so no longer dependent on @decorum-lib itself).
In doing so Iāve discovered that while your fix works, weāre not finding the libsafe_app.so in the package, but retrieving it from disk (in ./node_modules) so for now Iāve made this part of my ./dist output.
Iām not sure if your fix was meant to work like this. It is workable but would be better if we could package the dll and get them from the executable. This is supposed to be possible I think, by specifying āassetsā for pkg in the package.json but I have not managed to get this to work. Using pkg -d I see lots of stuff being packaged but never the libsafe_app.so no matter what I try, and I see others have had problems with this from pkg github issue #102 which was sorted, but by mods to pkg itself! Others had success with the āassetsā setting such as in issue #216 but Iāve tried and canāt seem to get libsafe_app.so to be packaged (at least it never appears in the '-d' output).
Iāve pushed my stuff here in case you want to take a look. Iāll update if I get it working:
UPDATE: I now have it being packaged but it is not being picked found when I run the exe. I hadnāt realised you need to include package.json in the pkg command line for it to find the āassetsā entries!
Yes, I did this on purpose because I could not get pkg to work on embedding the assets into the executable. Also, I didnāt really want to make a pkg specific solution per se. Perhaps there are other packagers out there, and this seems like a generic solution (loading it from the filesystem).
Your post did encourage me to dive a little deeper into why the executable built by pkg is not able to load the library. I think it has to do with how node-ffi works (which is what safe-node-app uses to load the SAFE library). node-ffi loads the file from disk, with platform specific Dynamic Library functions (LoadLibraryW for Windows).
The following release of SAFE CLI Boilerplate is a stand alone command line application which includes code which solves the problem presented in the OP, courtesy of @bzee: How to pick up openUrl auth response in a non-electron desktop app?