ospfranco / sol

MacOS launcher & command palette
MIT License
1.92k stars 89 forks source link

Unable to deep link into application (e.g., authentication) #44

Closed karimbutt closed 2 years ago

karimbutt commented 2 years ago

Hey @ospfranco - thank you SO much for your work on Sol. What an incredible application - finally able to tweak and customize the command palette like I could not do with other apps like Alfred.

I have a question for you - I'm looking to extend Sol a bit by adding authentication (right now using Auth0). Auth requires essentially the ability to send the user out of the Sol application and then back in with an access_token (i.e., similar to most MacOS apps).

For example, when I go into Safari and type in raycast:// it will open the command palette, indicating that it is responsive to any routing INTO the app that is happening.

With Sol, I've done the following:

func applicationDidFinishLaunching(_ aNotification: Notification) {
    // https://github.com/microsoft/react-native-macos/pull/1145/commits/d1d0a9f55071736a77efb6413943c0f70771d3bb
    NSAppleEventManager.shared().setEventHandler(
            self,
            andSelector: #selector(getURL(_:withReplyEvent:)),
            forEventClass: AEEventClass(kInternetEventClass),
            andEventID: AEEventID(kAEGetURL))
   // Rest of initialization here
}

  //    https://github.com/microsoft/react-native-macos/issues/1128
  @objc func getURL(_ event: NSAppleEventDescriptor?, withReplyEvent reply: NSAppleEventDescriptor?) {
      print("getURL function hit")
      RCTLinkingManager.getUrlEventHandler(event, withReplyEvent: reply)
  }

  func application(_ application: NSApplication, open urls: [URL]) {
    print("application function hit")
    print(urls[0])
//      RCTLinkingManager.initialUrl = urls[0]
  }

However, I'm not able to hit any of my logging points here when I try hitting sol:// in Safari. The URL is recognized (i.e., it sees that there is an app that has that scheme registered); however, none of my logging points are hit AND Sol does not reopen in the same way that Raycast does. My gut is telling me that it is likely because the application is hidden/in the background that none of these triggers are hit and that I need to do something like Raycast where it launches when called.

Essentially, the end goal is to be able to catch the details of the URL as a "deep link" because it will contain information to log users in via services like Auth0 or others where you have to route the user out to web auth. Do you know what I'm doing wrong here?

Super appreciative in advance.

ospfranco commented 2 years ago

The main app window is hidden/closed by default. In the app delegate you probably need to call showWindow(). initialUrl will probably not work since that is thought for mobile apps where the app might be silently killed in the background, whereas for macOS sol will not work if it is not actually running.

karimbutt commented 2 years ago

@ospfranco thanks! Where would you call showWindow()? You have that in the appDelegate at the end of the applicationDidFinishLaunching . Can't seem to get any URL data to feed into the app or to even trigger making it visible.

Makes sense re: initialURL use case being very different.

ospfranco commented 2 years ago

I don't quite understand what you want, you should call showWindow wherever you are handling the notification. This is what the official documentation says for iOS:

Screenshot 000635

I haven't built anything for the JS side to handle URLs yet. You seem to be going for the RN APIs, so calling showWindow is the first step to get the app to be visible, and then setting a listener with some of the RN APIs seems the way to go. The official documentation sets up this listener:

Screenshot 000636

In any case, if you really want oauth then you are building this yourself from scratch

karimbutt commented 2 years ago

Thanks @ospfranco ! Managed to resolve the issue so closing this. The initial code did work; it's just I had two versions of the app running with same URL scheme. I'll look into a way to get it working in the task bar to see the instances of the app running next :) Thanks again for your help!

ospfranco commented 2 years ago

You just need to remove the applicationIsUIAgent in the info.plist