nativescriptsfauthenticationsessionsafariservices

how do I use SFAuthenticationSession from nativescript?


I'm working on some SSO behavour in a nativescript application. I have the following Swift code that works correctly:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    var webAuthSession: SFAuthenticationSession?

    @IBAction func click(sender: AnyObject)
    {
        let authURL = URL(string: "http://localhost:5000/redirect.html");
        let callbackUrlScheme = "myApp://"

        self.webAuthSession = SFAuthenticationSession.init(url: authURL!, callbackURLScheme: callbackUrlScheme, completionHandler: { (callBack:URL?, error:Error?) in

            // handle auth response
            guard error == nil, let successURL = callBack else {
                return
            }

            print(successURL.absoluteString)
        })

        self.webAuthSession?.start()
    }
}

From this I've come up with the equivalent typescript code (in an .ios.ts file)

function callback(p1: NSURL, p2: NSError) {
    console.log('Got into the url callback');
    console.log(p1);
    console.log(p2);
};

public onTap() {
    const session = new SFAuthenticationSession(
        {
            URL: NSURL.URLWithString('localhost:3500/redirect.html'),
            callbackURLScheme: 'myApp://',
            completionHandler: callback,
        },);
    console.log('session created');

    console.log('the session is ', session);
    console.log('the start method is ', session.start);
    console.log('about to call it');
    session.start();    
    console.log('After calling start');
}

This all compiles and builds fine, but when run it crashes at the session.start() call after a second or so delay. I get the output before that, including the 'about to call it' method, but nothing after, not even an error message or stack dump.

Is there anything obvious I'm doing wrong here? Is there anything special that needs to be done to call from typescript to native ios shared library methods?


Solution

  • I found the problem today and it's an embarrassingly trivial mistake.

    when I did the translation i managed to drop the http:// from the original code.

    URL: NSURL.URLWithString('localhost:3500/redirect.html'),

    when run on a colleague's machine, we got more details in the log, and it turns out that the only schemes supported are http or https. It must be explicitly added to the url.

    so this fixes it (in addition to Manoj's change above)

    URL: NSURL.URLWithString('http://localhost:3500/redirect.html'),