Sign In anonymously with Firebase | SwiftUI

In this tutorial, we will use Firebase Auth SDK to generate a unique identifier without asking any information for our user.

Sullivan De Carli
Swift Productions
4 min readAug 30, 2022

--

Firebase and SwiftUI logo with an iPhone screenshot
The screen we are going to build

Difficulty: Beginner | Easy | Normal | Challenging

Environment : Xcode 13, iOS 15, Firebase 9.0.0 & SwiftUI

Reference: Firebase documentation

Create a new project

Open Xcode > App template > Select SwiftUI Interface and Life Cycle > call your project FirebaseLogin.

Screenshot of the Xcode application
Xcode, new project

Connect to the Firebase SDK with SPM

Please, follow this tutorial to create a Firebase project and connect an iOS application to it.

During the process, make sure to include theFirebase Auth module when using Swift Package manager:

Screenshot of Swift Package Manager selecting FirebaseAuth module
SPM > add package

Once you have successfully connected Firebase to your application with the Firebase Auth framework, we can proceed with the Firebase console set up and coding.

Set up Firebase Authentication

Go to your Firebase console and select the Authentication section, click on the “Get started” button:

Then enable Anonymous Authentication under native provider:

Firebase authentication > anonymous provider

Et voilà! You are now ready to send and receive information to and from Firebase, we can now code.

Create the functions

It’s now time to code, go ahead and create a new Swift file, call it AuthViewModel and copy/paste the following code:

Here what we have done:

  • We made a reference to our user model, which is provide by the Firebase Auth SDK (FIRUser), no need to create one.
  • listenToAuthState We have a function to determine if the user is logged in or has terminated the session, we will use it to display information later.
  • SignInAnonymously We implement the sign in anonymously function to sign up to Firebase and create a user identifier.
  • SignOut We implement the sign-out function to terminate the session.

Now, I invite you to observe this View Model at the App startup, in the FirebaseLoginApp.swift file, add next to ContentView(), add the following: .environmentObject(AuthViewModel())

@mainstruct FirebaseLoginApp: App {  @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate var body: some Scene {  WindowGroup {   NavigationView {  ContentView().environmentObject(AuthViewModel())   } 
}
}
}

This will alllow us to check the user status when the application is starting.

Now that we have the necessary function, we can go back to our ContentView.swift file and build the UI. First, let’s import the framework:

Then, replace the code you have in ContentView.swift file to the following one:

ContentView.swift

Great! We have done a few things here:

  • We made a reference to our View Model
  • We are displaying the user identifier as a String and performing a session check with listenToAuthState
  • We implement a button to sign up anonymously
  • We implement a button to terminate the session

You can now run the application and click on Sign in anonymously:

Screenshots of Xcode and the Firebase console, authenticate section
Final result

Great! It is working as expected, our user is registered on the Firebase dashboard and both the identifier displayed in our App match with the one in the dashboard.

Also, when we logout, the identifier disappear because we are listening the user status in real time thanks to addStateDidChangeListener . Therefore, when logged out, the session is terminated.

You can now onboard your user with anonymous sign-in for your next project, it is a great way to get a user identifier without requiring any information.

I also recommend you to implement a conversion from anonymous account to a permanent one once your users are using the app enough, more information in the Firebase documentation.

Thanks for reading! I’m always happy to have a chat and collaborate at hello@sullivandecarli.com. Consider subscribing to enjoy unlimited access to my articles and all of Medium through my referral link

--

--

Sullivan De Carli
Swift Productions

Consultant in iOS Dev at Deloitte. I write about Design & App development. Let’s have a chat at hello@sullivandecarli.com