Firebase-ios-sdk: FR: Firebase Auth support for watchOS

Created on 8 Jan 2020  路  8Comments  路  Source: firebase/firebase-ios-sdk

Please add support for Firebase Auth for watchOS apps/targets. Thanks!
Linking to umbrella issue https://github.com/firebase/firebase-ios-sdk/issues/269 and Firestore for watchOS https://github.com/firebase/firebase-ios-sdk/issues/4559

auth feature request watchOS

Most helpful comment

Sign-in with email and password works with changes in https://github.com/hohteri/firebase-ios-sdk/commit/1eebd5746099c3ecb108451814bf59494cd82b20

Also sign-in with email link work if you send fcmToken as parameter (don't know about security implications) and handle the Firebase backend redirection in Firebase Functions

In app:

let fcmToken = result.token
                            let settings = ActionCodeSettings()
                            settings.url = URL(string: "https://yourprojectehere.cloudfunctions.net/signIn?fcmToken=\(fcmToken)")
                            settings.handleCodeInApp = true
                            settings.setIOSBundleID("com.yourpackage.here")
                            settings.setAndroidPackageName("com.yourpackage.here", installIfNotAvailable: true, minimumVersion: nil)

                            Auth.auth().sendSignInLink(toEmail: self.email, actionCodeSettings: settings) { (error) in
                                if let error = error {
                                    NSLog("Signin link sending error: \(error)")
                                } else {
                                    NSLog("Signin link sent")
                                }
                            }

In functions:

exports.signIn = functions.https.onRequest((req, resp) => {
    const ua = req.get("User-Agent")
    // watchOS browser declares itself as an iPhone, this I guess is not an issue as an iPhone would handle the firebase URL Scheme, watchOS does not support URL Schemes
    if (ua.includes("iPhone")) { 
        const payload = {
            notification: {
                title: "Sign in",
                body: "Tap this notification to sign in",
                click_action: "SIGN_IN_CATEGORY"
            },
           data: {
                oobCode: req.query.oobCode,
                mode: req.query.mode,
                apiKey: req.query.apiKey,
                lang: req.query.lang
           }
        }
        return admin.messaging().sendToDevice([req.query.fcmToken], payload).then( res => {
           return resp.send("ok, notif sent")
        })
    } else {
        resp.send("OK, not an apple watch")
    }
})

This however shows a weird open in app screen from Firebase backend, which I guess could be avoided with the below commit and supporting the the notification sending in the Firebase backend natively:

https://github.com/hohteri/firebase-ios-sdk/commit/946afcb0585a071ce00c7932b6c11ba0d899af0c

Finally the sign in is handled in the app:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let content = response.notification.request.content
        switch (content.categoryIdentifier) {
        case "SIGN_IN_CATEGORY":
            if (response.actionIdentifier == "SIGN_IN_ACTION") {
                let link = "https://domain/?oobCode=\(content.userInfo["oobCode"]!)&mode=\(content.userInfo["mode"]!)&apiKey=\(content.userInfo["apiKey"]!)&lang=\(content.userInfo["lang"]!)"
                Auth.auth().signIn(withEmail: "[email protected]", link: link) { (result, error) in
                    completionHandler()
                }
                return
            }
        default:
            ...
        }
    }

All 8 comments

Sign-in with email and password works with changes in https://github.com/hohteri/firebase-ios-sdk/commit/1eebd5746099c3ecb108451814bf59494cd82b20

Also sign-in with email link work if you send fcmToken as parameter (don't know about security implications) and handle the Firebase backend redirection in Firebase Functions

In app:

let fcmToken = result.token
                            let settings = ActionCodeSettings()
                            settings.url = URL(string: "https://yourprojectehere.cloudfunctions.net/signIn?fcmToken=\(fcmToken)")
                            settings.handleCodeInApp = true
                            settings.setIOSBundleID("com.yourpackage.here")
                            settings.setAndroidPackageName("com.yourpackage.here", installIfNotAvailable: true, minimumVersion: nil)

                            Auth.auth().sendSignInLink(toEmail: self.email, actionCodeSettings: settings) { (error) in
                                if let error = error {
                                    NSLog("Signin link sending error: \(error)")
                                } else {
                                    NSLog("Signin link sent")
                                }
                            }

In functions:

exports.signIn = functions.https.onRequest((req, resp) => {
    const ua = req.get("User-Agent")
    // watchOS browser declares itself as an iPhone, this I guess is not an issue as an iPhone would handle the firebase URL Scheme, watchOS does not support URL Schemes
    if (ua.includes("iPhone")) { 
        const payload = {
            notification: {
                title: "Sign in",
                body: "Tap this notification to sign in",
                click_action: "SIGN_IN_CATEGORY"
            },
           data: {
                oobCode: req.query.oobCode,
                mode: req.query.mode,
                apiKey: req.query.apiKey,
                lang: req.query.lang
           }
        }
        return admin.messaging().sendToDevice([req.query.fcmToken], payload).then( res => {
           return resp.send("ok, notif sent")
        })
    } else {
        resp.send("OK, not an apple watch")
    }
})

This however shows a weird open in app screen from Firebase backend, which I guess could be avoided with the below commit and supporting the the notification sending in the Firebase backend natively:

https://github.com/hohteri/firebase-ios-sdk/commit/946afcb0585a071ce00c7932b6c11ba0d899af0c

Finally the sign in is handled in the app:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let content = response.notification.request.content
        switch (content.categoryIdentifier) {
        case "SIGN_IN_CATEGORY":
            if (response.actionIdentifier == "SIGN_IN_ACTION") {
                let link = "https://domain/?oobCode=\(content.userInfo["oobCode"]!)&mode=\(content.userInfo["mode"]!)&apiKey=\(content.userInfo["apiKey"]!)&lang=\(content.userInfo["lang"]!)"
                Auth.auth().signIn(withEmail: "[email protected]", link: link) { (result, error) in
                    completionHandler()
                }
                return
            }
        default:
            ...
        }
    }

Thanks for the exploratory implementation! @renkelvin WDYT?

@hohteri Thanks! We need a PR with a signed CLA to evaluate the proposed changes.

@paulb777 afaik Firebase backend is not public code. I would still like to test Sign-In with Apple before doing a PR. The above was more or less conversation opener if the sign with link feature could be implemented as proposed on watchOS.

@hohteri Yes, the backend is not public code. And we definitely need to do the testing before merging a PR.

However, a PR with a signed CLA is the best way to enable Googlers to participate in a conversation about code.

Hi All,
Is FirebaseAuth now available in watchOS 6+ ?

Sorry, but we haven't yet finished the testing on #5585 and haven't yet been able to merge the support. We've been swamped with other tasks, but hopefully can get back to this in the next month or so.

This was addressed with #6260 - thanks again for the contribution!

Was this page helpful?
0 / 5 - 0 ratings