iOS Tutorial FirebaseAuth - Authenticate your users (Google + Facebook)

Erel

Administrator
Staff member
Licensed User
Start with this tutorial: https://www.b4x.com/android/forum/threads/firebase-integration.68623/

FirebaseAuth allows your app to identify the user based on their Google or Facebook accounts.

We will start with Google integration.

upload_2016-7-4_15-22-44.png


1. Make sure that Google is enabled under Authentication - Sign-in methods:



2. Open GoogleService-Info.plist with a text editor and copy the key that is under REVERSED_CLIENT_ID:



3. Add this value with:
B4X:
#UrlScheme: com.googleusercontent.apps.250280117927-3n8kkn0a8oveo2olg2tcnuefmntv5toi
4. Handle Application_OpenUrl and call FirebaseAuth.OpenUrl. Note that the signature of Application_OpenUrl has changed in v2.80:

B4X:
Private Sub Application_OpenUrl (Url As String, Data As Object, SourceApplication As String) As Boolean
   If auth.OpenUrl(Url, Data, SourceApplication) Then Return True
   Return False
End Sub
See the attached project. Don't forget to download GoogleService-Info.plist to Files\Special.
 

Attachments

Erel

Administrator
Staff member
Licensed User
Now we will add support for signing in with Facebook.

upload_2016-7-4_15-40-8.png


1. Create a Facebook app and add an iOS platform:



Note that you can use the same Facebook app with B4A as well.

2. Enable Facebook in Firebase console and set the app id and app secret fields:



3. Add the OAuth redirect URI from Firebase to Facebook Login product:



4. Add the following attributes (change the number in UrlScheme to match your Facebook app id):
B4X:
#QueriesSchemes: fbapi
#QueriesSchemes: fb-messenger-api
#QueriesSchemes: fbauth2
#QueriesSchemes: fbshareextension
#UrlScheme: fb3333333333333
5. Call Facebook.OpenUrl from Application_OpenUrl:
B4X:
Private Sub Application_OpenUrl (Url As String, Data As Object, SourceApplication As String) As Boolean
   If facebook.OpenUrl(Url, Data, SourceApplication) Then Return True
   If auth.OpenUrl(Url, Data, SourceApplication) Then Return True
   Return False
End Sub
See the attached example.
 

Attachments

jazzzzzzz

Active Member
Licensed User
First of all thank you Erel for complete firebase support. Authentication is working perfectly

B4X:
Error signing in: Error Domain=com.google.GIDSignIn Code=-5 "The user canceled the sign-in flow." UserInfo={NSLocalizedDescription=The user canceled the sign-in flow.}
Am getting this message in the logs when users cancels login by not signin in.Can I get this event back to b4i? So that my app knows that user cancels the login page.

It will be helpfull for hiding progress dialog.
 

jazzzzzzz

Active Member
Licensed User
For a slow internet connection after clicking login in internel webview it will take some time to get callback. So i put a progress dialog for user to know some thing is happening​
 

tufanv

Expert
Licensed User
The ifacebook lib is for the auth i think. But when i declare it it says facebook sdk. To advertise and promote our apps with facebook for CPI campaigns , facebook sdk is required. With this , can we also achieve this or this is only for the auth ?
Now we will add support for signing in with Facebook.

View attachment 45735

1. Create a Facebook app and add an iOS platform:



Note that you can use the same Facebook app with B4A as well.

2. Enable Facebook in Firebase console and set the app id and app secret fields:



3. Add the OAuth redirect URI from Firebase to Facebook Login product:



4. Add the following attributes (change the number in UrlScheme to match your Facebook app id):
B4X:
#QueriesSchemes: fbapi
#QueriesSchemes: fb-messenger-api
#QueriesSchemes: fbauth2
#QueriesSchemes: fbshareextension
#UrlScheme: fb3333333333333
5. Call Facebook.OpenUrl from Application_OpenUrl:
B4X:
Private Sub Application_OpenUrl (Url As String, Data As Object, SourceApplication As String) As Boolean
   If facebook.OpenUrl(Url, Data, SourceApplication) Then Return True
   If auth.OpenUrl(Url, Data, SourceApplication) Then Return True
   Return False
End Sub
See the attached example.
 

microcacti

New Member
Licensed User
Works great for me, thanks!
Only wish the anonymous login was implemented also - Apple App Store guidelines/review mean we need anonymous access wherever possible, and so have had to do our own anonymous implementation that does not integrate with Firebase
 

pedrocam

Member
Licensed User
Hi B4X'rs

I am able to successfully log into firebase through B4A and B4i, however, in B4i I cannot seem to get the authentication to do anything useful such as access the secure database, and secure storage. I am only able to access the public storage and database. I think this might be due to the TOKEN which I don't know if i'm doing right. In B4A I am able to easily get a user token, but in B4i I think that I may not be implementing the token method correctly during the auth process..

Right now I use the following code to get the user token:

B4X:
Private Sub Application_PushToken (Success As Boolean, Token() As Byte)

Log("#PUSHTOKEN -auth_TokenAvailable, Success=" & Success ) '& ", TokenId=" & TokenId)
    If Success Then
        Log("Push TOken Sucess")
        strFirebaseSignedInUserToken = bc.HexFromBytes(Token)
    Else
        strFirebaseSignedInUserToken = ""
    End If

End Sub
However, when I try to access the authenticated database with

B4X:
job1.PutBytes(strFiBaDbRoot & strPath & ".json" & "?auth=" & Main.strFirebaseSignedInUserToken, strJsonToSend.GetBytes("UTF8"))
or an authentication required folder on Firebase storage with

B4X:
storage.UploadFile(File.DirDocuments, "ProfPic.jpg", "/ProfPic.jpg")
I get a not authorized error..

Any ideas? Thanks!
 

andyr00d

Member
Licensed User
Not sure what I've done wrong or haven't done, but I'm hitting the sign in button, the Google authentication page appears (Safari), I hit allow and it returns to my app and nothing happens or changes. I've added a line to log in the Auth_SignedIn Sub, and not even that is firing. I've double checked everything as mentioned above and in the initial setup - I've allowed Google signin (already successfully set this up for Android), copied the plist file to files\special, and added the #UrlScheme with my key. Has anyone else run into this problem?
 

andyr00d

Member
Licensed User
I'm using a local builder and I extracted all the files within the archive to the \libs folder and restarted the service. There was no "confirm file overwrite" though when I extracted... were these all new files, or some should be replacing older files?
 

andyr00d

Member
Licensed User
Thanks Erel - I switched to the hosted builder and the first time it didn't even raise the "true" flag as it did above (I literally changed nothing except the hosted builder), then tried it again after resetting everything and it came up with the sign in/authorize page but then in the log I got this:

Copying updated assets files (32)
Application_Start
The default app has not been configured yet.
Configuring the default app.
Application_Active
starting auth process
Application_Inactive
openURL: com.googleusercontent.apps.250927034461-qkbh3ma3ngdubal4v4jmt7rdtdXXXXXX:/oauth2callback?code=4/bB6-CzwMAggWD0nrgsyQ_rewq0j06TJ6XC871XXXXXX#, com.apple.SafariViewService, (null)
Application_Openurl
true
Application_Active
Error signing in: Error Domain=com.google.HTTPStatus Code=400 "(null)" UserInfo={data=<7b0a2022 6572726f 72223a20 22696e76 616c6964 5f677261 6e74222c 0a202265 72726f72 5f646573 63726970 74696f6e 223a2022 436f6465 20776173 20616c72 65616479 20726564 65656d65 642e220a 7d0a>, json={
error = "invalid_grant";
"error_description" = "Code was already redeemed.";
}}

If I hit sign in again after that, nothing happens. I tried it again and nothing happened also.
 
Top