Lecture Material: Anonymous Login with Firebase

Add new view

Press the Command + N to make new view. And choose Cocoa Touch Class

Check the box on ‘Also create XIB file’ to seperate each files.

Of course, you can combine and place every view into a Main storyboard, but in that case, this file is going to be large and takes a lot of time in every time you open the storyboard file.

And I re-organised directory structures to separate between the view-related files and logic related files.

Set constraints and alignments

Drag the button with pressing Ctrl key, and drop to source code (LoginVC)

Name it the event handler. Make sure the type is ‘UIButton’ and the event is ‘Touch Up Inside’

Refer the official page in Firebase documentation

So, on your button press handler, implement a verification of authenticating as anonymous.

After you execute project in an emulator, you will face with an error

firebase-test[33757:651569] [boringssl] boringssl_metrics_log_metric_block_invoke(144) Failed to log metricsOptional(Error Domain=FIRAuthErrorDomain Code=17085 "ADMIN_ONLY_OPERATION" UserInfo={NSLocalizedDescription=ADMIN_ONLY_OPERATION, FIRAuthErrorUserInfoNameKey=ERROR_ADMIN_RESTRICTED_OPERATION})

This is because you haven’t turned on the anonymous login feature on the Firebase console yet.

Go to the console, and make it enable.

Your anonymous login is working well. And you can check the new auth of user on the Authentication menu.

So, after this, let’s implement the logout feature.

There are several ways to back to the previous screen. In this case, you use ‘dismiss’ method to close the current screen.

After you follow all, you still have to press the sign-in button in every time you opened the app.

To maintain a session and prevent to request login process with signed-in user, you have to check the auth state on the login screen.

To deal with the user auth state and information which is included on the auth. You can check this official doc.

You can see the checking process under the ‘addStateDidChangeListener’.

With this, you don’t need to press the ‘Sign-in’ button again, next time you open the app.

Dropout from Physics, self-taught and worked on the IT industry as a Dev/Design/Planning for 8 years. And I had run my Startup for 3 years. I fancy a ☔️ 🇬🇧