Handle Firebase & Google authentication on macOS
Firebase can be quite a good, easy and affordable solution for anyone wanting to provide account & cloud storage functionality to their app. For iOS it is available via Cocoapods (https://github.com/firebase/firebase-ios-sdk). There is sadly no Swift Package support yet.
In case you want to enable user authentication via a Google account, there are numerous tutorials on the web that describe how to configure Firebase to allow a user to create/log in with a Google account in your Firebase database.
But in case you want to make your app available to macOS via Catalyst you are in for a surprise. Apart from some components of Firebase not being compilable for macOS (such as FirebaseAnalytics) you will have problem with the user authentication itself.
If you followed the Google-official way to handle this in iOS, you used the pod GoogleSignIn (https://cocoapods.org/pods/GoogleSignIn). But after enabling the macOS flag in your Xcode project and trying to run it on your Mac you will encounter compile errors. GoogleSignIn is simply not meant to run on macOS.
But despair not because where Google does not bother to act, 3rd-party libraries come to the rescue! GAppAuth (https://github.com/elsesiy/GAppAuth) can be used to authenticate a user via a Google account. There are example apps in the repository to provide basic functionality to your app.
But there is a crucial, additional step required for your Firebase solution to become aware of the login process performed by GAppAuth and the code below makes all the bits fall into place:
After GAppAuth has finished logging in, there are several tokens available for us. We only need the ID token and the access token so that we can construct a GoogleAuth credential. With it we sign in with FirebaseAuth and now our Firebase service is aware of our logged in user!
Overall, it is a good thing that we can easily replace Google’s official solution when developing in macOS but it is a shame that Google hasn’t fixed this officially yet…