Search code examples
swiftauthenticationtwitter-fabricfirebase-authenticationtwitter-digits

How can I authenticate Digits users via Firebase Twitter method in Swift?


I am trying to authenticate a Digits user via Firebase. I have heard on more than two occasions that you can auth Digits users through the twitter end-points. So I'm trying it now but I'm pretty much stuck. I am trying to send the session?.authToken to Firebase so that I can create a user. Digits looks really cool and so I'm pretty sure that I want to stick with it. If this is totally impossible I would also really appreciate being pointed in the direction of a BaaS that would be able to work with digits. Thanks in advance and here is my code:

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let authButton = DGTAuthenticateButton(authenticationCompletion: { (session: DGTSession?, error: NSError?) in
            if (session != nil) {
                // TODO: associate the session userID with your user model
                let message = "Phone number: \(session!.phoneNumber)"
                let alertController = UIAlertController(title: "You are logged in!", message: message, preferredStyle: .Alert)
                alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: .None))
                self.presentViewController(alertController, animated: true, completion: .None)
                DataService.FireBase.Surfer.authWithOAuthProvider("twitter", token: session!.authToken, withCompletionBlock: { error, newAuthData in
                    if error != nil {
                        print(error)

                    } else {
                        print(newAuthData)
                    }
                })

            } else {
                NSLog("Authentication error: %@", error!.localizedDescription)

            }
        })
        authButton.center = self.view.center
        self.view.addSubview(authButton)
        let button   = UIButton(type: UIButtonType.System) as UIButton
        button.frame = CGRectMake(100, 100, 100, 50)
        button.backgroundColor = UIColor.greenColor()
        button.setTitle("Test Button", forState: UIControlState.Normal)
        button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

        self.view.addSubview(button)

    }

    func buttonAction(sender:UIButton!)
    {
        print("Button tapped")
        print(authTkn)
        Digits.sharedInstance().logOut()
    }

and here is the DataService:

class DataService{

    static let FireBase = DataService()

    private var _REF_BASE = Firebase(url: "https://testingtoday.firebaseio.com")

    var Surfer: Firebase {
        return _REF_BASE
    }
}

Solution

  • Firebase does not support using Twitter Digits as an identity provider.

    That means the only way to use the Digits-authenticated user in Firebase is to mint a custom token, based on the Digits ID. So you let Digits handle the authentication, and then tell Firebase what the resulting uid is.

    The disadvantage of this approach is that you minting custom tokens requires use of your Firebase's secret. This means you should never mint tokens in a client-side app, since you'd be handing your secret to anyone with the smarts to look at your app's resources.