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

Discussion in 'iOS Tutorials' started by Erel, Jul 4, 2016.

  1. Erel

    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:

    [​IMG]

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

    [​IMG]

    3. Add this value with:
    Code:
    #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:

    Code:
    Private Sub Application_OpenUrl (Url As String, Data As Object, SourceApplication As StringAs 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.
     

    Attached Files:

    Ohanian likes this.
  2. Erel

    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:

    [​IMG]

    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:

    [​IMG]

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

    [​IMG]

    4. Add the following attributes (change the number in UrlScheme to match your Facebook app id):
    Code:
    #QueriesSchemes: fbapi
    #QueriesSchemes: fb-messenger-api
    #QueriesSchemes: fbauth2
    #QueriesSchemes: fbshareextension
    #UrlScheme: fb3333333333333
    5. Call Facebook.OpenUrl from Application_OpenUrl:
    Code:
    Private Sub Application_OpenUrl (Url As String, Data As Object, SourceApplication As StringAs 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.
     

    Attached Files:

  3. jazzzzzzz

    jazzzzzzz Active Member Licensed User

    First of all thank you Erel for complete firebase support. Authentication is working perfectly

    Code:
    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.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Currently there is no cancel event. I'm not sure why you are showing a progress dialog, however you can hide it in Application_Active or OpenUrl.
     
  5. jazzzzzzz

    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​
     
  6. tufanv

    tufanv Well-Known Member 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 ?
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Currently only the log-in feature is implemented.
     
  8. microcacti

    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
     
  9. Keith Yong

    Keith Yong Member Licensed User

    is there anyway to get the birthdate and contact number after login?
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    No. You only get the email and name.
     
  11. pedrocam

    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:

    Code:
    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

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

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

    Any ideas? Thanks!
     
    valentino s likes this.
  12. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread in the questions forum. Note that the PushToken event has nothing to do with Firebase authentication.
     
  13. andyr00d

    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?
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    Is Application_OpenUrl raised?

    What is the output of:
    Code:
    Log(auth.OpenUrl(Url, Data, SourceApplication))
     
  15. andyr00d

    andyr00d Member Licensed User

    The output is True, but somehow it's not reaching the SignedIn Sub.
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    Are you using a local builder or the hosted builder?
     
  17. andyr00d

    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?
     
  18. Erel

    Erel Administrator Staff Member Licensed User

    My guess is that there is a configuration error on your side. However to rule out issues with the SDK please test it with the hosted builder. I've sent you a temporary account.
     
  19. andyr00d

    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.
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    I recommend you to start from scratch with a new Firebase project with a different package name.
     
Loading...