B4A Library FirebaseAuth - Authenticate your users

Status
Not open for further replies.
This library requires B4A v6+.

It allows the users to sign in to your app with their Google account (more identity providers will be added in the future).

upload_2016-6-13_16-31-40.png


Like all Firebase services it is quite simple to integrate this service in your app.

1. Follow this tutorial and make sure to add the Firebase Auth manifest snippet: https://www.b4x.com/android/forum/threads/integrating-firebase-services.67692/

2. This service requires that the signing key SHA1 signature is set in the Firebase project settings:

SS-2016-06-13_16.36.56.png


You can find this value under Tools - Private Sign Key, in the signature field:

SS-2016-06-13_16.38.08.png


Initialize the FirebaseAuth object and call SignInWithGoogle to sign in. Note that the user will be signed in automatically when the program restarts.
B4X:
Sub Process_Globals
   Private auth As FirebaseAuth
End Sub

Sub Globals
   Private lblName As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     auth.Initialize("auth")
   End If
   Activity.LoadLayout("1")
   If auth.CurrentUser.IsInitialized Then Auth_SignedIn(auth.CurrentUser)
End Sub

Sub btnSignIn_Click
   auth.SignInWithGoogle
End Sub

Sub btnSignOut_Click
   auth.SignOutFromGoogle
   lblName.Text = "Goodbye!"
End Sub

Sub Auth_SignedIn (User As FirebaseUser)
   Log("SignedIn: " & User.DisplayName)
   lblName.Text = "Hello: " & User.DisplayName
End Sub

Next:
- Combine Google and Facebook: https://www.b4x.com/android/forum/t...seauth-to-support-facebook.67954/#post-430482
- Server verification of signed in users: https://www.b4x.com/android/forum/t...end-authentication-for-signed-in-users.68672/

FirebaseAuth is an internal library now. It is preinstalled with the IDE.

V2.01 is attached. It adds a SignError (Error As Exception) event.
 

Attachments

  • FirebaseAuth_Example.zip
    11.7 KB · Views: 2,727
  • FirebaseAuth.zip
    11.1 KB · Views: 1,139
Last edited:

DonManfred

Expert
Licensed User
Longtime User
FireaseAuthEx

New in V1.12
- signInAnonymously
- createUserWithEmailAndPassword
- SignInWithEmailAndPassword
- sendPasswordResetEmail

New Methods for the firebaseuser object
- updateEmail
- updatePassword
- delete
 
Last edited:

johndb

Active Member
Licensed User
Longtime User
FirebaseAuth
Version:
1.1
  • FirebaseAuth
    Events:
    • SignedIn (User As FirebaseUser)
    • TokenAvailable (User As FirebaseUser, Success As Boolean, TokenId As String)
    • UserCreated (User As FirebaseUser)
    Methods:
    • GetUserTokenId (User As FirebaseUserWrapper, ForceRefresh As Boolean)
      Retrieves the token id. This token can be sent to your backend server. The server can use it to verify the user.
      The TokenAvailable event will be raised in the current module.
    • Initialize (EventName As String)
      Initializes the object. The SignedIn event will be raised if there is already a signed in user.
    • SignInWithEmailAndPassword (email As String, password As String)
    • SignInWithGoogle
      Start the sign in process.
    • SignOutFromGoogle
      Sign outs from Firebase and Google.
    • createUserWithEmailAndPassword (email As String, password As String)
    • signInAnonymously
    Properties:
    • CurrentUser As FirebaseUserWrapper [read only]
      Returns the current signed in user. Returns an uninitialized object if there is no user.
  • FirebaseUser
    Methods:
    • IsInitialized As Boolean
    • sendEmailVerification
    Properties:
    • Anonymous As Boolean [read only]
    • DisplayName As String [read only]
    • Email As String [read only]
    • EmailVerified As Boolean [read only]
    • PhotoUrl As String [read only]
    • ProviderData As List [read only]
    • ProviderId As String [read only]
    • Providers As List [read only]
    • Uid As String [read only]

New in V1.1
- signInAnonymously
- createUserWithEmailAndPassword
- SignInWithEmailAndPassword

Additional the given Firebaseuser now has a few more propteries.
Thank you @DonManfred. I added the eMail & Password Authentication to my app (for testing purposes). This works well if a user/password is manually added to USERS using the Firebase console. It also works when createUserWithEmailAndPassword is used to create the user within the App. However, the email/user management methods are not currently implemented in the update FirebaseAuth V1.1. These include sendEmailVerification, updatePassword, delete, to mention a few of the methods. I'm not really sure how these can be implemented but may be worth considering? I do appreciate you adding email/password and Anonymous Authentication methods as it does add more functionality to Firebase Authentication. Thank you :)

John
 

DonManfred

Expert
Licensed User
Longtime User
Last edited:

DonManfred

Expert
Licensed User
Longtime User

DonManfred

Expert
Licensed User
Longtime User
please create a new library named FirebaseAuthEx with your changes
I´ve updated my post above and posted the new library.

I´m not sure if this is the right solution but it works.
It is basically a copy of the original but without GoogleSignIn and with my changes. And a different FirebaseUser-Object (FirebaseAuthUser) as it provides more properties and also new methods... Maybe the name is a bit irritating now as i read it here...
 

johndb

Active Member
Licensed User
Longtime User
I´ve updated my post above and posted the new library.

I´m not sure if this is the right solution but it works.
It is basically a copy of the original but without GoogleSignIn and with my changes. And a different FirebaseUser-Object (FirebaseAuthUser) as it provides more properties and also new methods... Maybe the name is a bit irritating now as i read it here...
Seems a bit awkward to me having two libraries for FirebaseAuth! There is a problem with the "... CurrentUser.EmailVerified" method. See below.

java.lang.NoSuchMethodError: No virtual method isEmailVerified()Z in class Lcom/google/firebase/auth/FirebaseUser; or its super classes (declaration of 'com.google.firebase.auth.FirebaseUser' appears in /data/app/com.scsoftstudios.timeslicer-1/base.apk)
at de.donmanfred.FirebaseAuthExWrapper$FirebaseAuthUserWrapper.getEmailVerified(FirebaseAuthExWrapper.java:194)
at com.scsoftstudios.timeslicer.main._signedin(main.java:1730)
at com.scsoftstudios.timeslicer.main._activity_resume(main.java:1622)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:340)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at com.scsoftstudios.timeslicer.main.afterFirstLayout(main.java:108)
at com.scsoftstudios.timeslicer.main.access$000(main.java:17)
at com.scsoftstudios.timeslicer.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
java.lang.RuntimeException: java.io.EOFException
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:165)
at anywheresoftware.b4a.BA$2.run(BA.java:328)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:77)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:333)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
... 8 more
 

DonManfred

Expert
Licensed User
Longtime User
Seems a bit awkward to me having two libraries for FirebaseAuth!
You need to decide for one i guess

If you only want to use my lib (and NOT FirebaseAuth too) then you need to add the two dependencies from the firebaseAuth xml to the xml from my lib.

Or you add
B4X:
#additionaljar: com.google.firebase:firebase-auth
#additionaljar: com.google.android.gms:play-services-auth
to your project

You also can check both libs and just use my if you want... The dependencies will be filled with marking FirebaseAuth in the libs tab...
 

johndb

Active Member
Licensed User
Longtime User
You need to decide for one.

If you only want to use my lib (and NOT FirebaseAuth too) then you need to ad the two dependencies from the firebaseAuth xml to the xml from my lib.
Or you add
B4X:
#additionaljar: com.google.firebase:firebase-auth
#additionaljar: com.google.android.gms:play-services-auth
to your project
I will be using both giving the user the option to use various methods for authentication. All worked well when FirebaseAuth was one library. Nonetheless, what is the problem with the CurrentUser.EmailVerified method?

Thanks for your hard work!

John
 

desof

Well-Known Member
Licensed User
Longtime User
Nothing works for me ever comes the code flow there!
I put a user and nothing happens.
What can it be?
I have Google Sign-In enabled in Firebase


B4X:
 Sub Auth_SignedIn (Usuario Como FirebaseUser)
    Log ( "SignedIn:" & User.DisplayName)
    lblName.Text = "Hola" y User.DisplayName
End Sub
[/CÓDIGO]
 

DonManfred

Expert
Licensed User
Longtime User
Nothing works for me ever comes the code flow there!
the sub signature is definetively wrong

B4X:
Sub Auth_SignedIn (Usuario Como FirebaseUser)
    Log ( "SignedIn:" & User.DisplayName)
    lblName.Text = "Hola" y User.DisplayName
End Sub
I´m sure you´ll get an error in the ide when compiling.

It must be

B4X:
Sub Auth_SignedIn (user As FirebaseUser)
    Log ( "SignedIn:" & User.DisplayName)
    lblName.Text = "Hola" y User.DisplayName
End Sub
 

desof

Well-Known Member
Licensed User
Longtime User
the sub signature is definetively wrong

B4X:
Sub Auth_SignedIn (Usuario Como FirebaseUser)
    Log ( "SignedIn:" & User.DisplayName)
    lblName.Text = "Hola" y User.DisplayName
End Sub
I´m sure you´ll get an error in the ide when compiling.

It must be

B4X:
Sub Auth_SignedIn (user As FirebaseUser)
    Log ( "SignedIn:" & User.DisplayName)
    lblName.Text = "Hola" y User.DisplayName
End Sub

Can you give me more details please
My signature in the IDE? Can I put it in the DEMO version I'm using?
 

desof

Well-Known Member
Licensed User
Longtime User
This library requires B4A v6+.

It allows the users to sign in to your app with their Google account (more identity providers will be added in the future).

View attachment 44939

Like all Firebase services it is quite simple to integrate this service in your app.

1. Follow this tutorial and make sure to add the Firebase Auth manifest snippet: https://www.b4x.com/android/forum/threads/integrating-firebase-services.67692/

2. This service requires that the signing key SHA1 signature is set in the Firebase project settings:

SS-2016-06-13_16.36.56.png


You can find this value under Tools - Private Sign Key, in the signature field:

SS-2016-06-13_16.38.08.png


Initialize the FirebaseAuth object and call SignInWithGoogle to sign in. Note that the user will be signed in automatically when the program restarts.
B4X:
Sub Process_Globals
   Private auth As FirebaseAuth
End Sub

Sub Globals
   Private lblName As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     auth.Initialize("auth")
   End If
   Activity.LoadLayout("1")
   If auth.CurrentUser.IsInitialized Then Auth_SignedIn(auth.CurrentUser)
End Sub

Sub btnSignIn_Click
   auth.SignInWithGoogle
End Sub

Sub btnSignOut_Click
   auth.SignOutFromGoogle
   lblName.Text = "Goodbye!"
End Sub

Sub Auth_SignedIn (User As FirebaseUser)
   Log("SignedIn: " & User.DisplayName)
   lblName.Text = "Hello: " & User.DisplayName
End Sub

Next:
- Combine Google and Facebook: https://www.b4x.com/android/forum/t...seauth-to-support-facebook.67954/#post-430482
- Server verification of signed in users: https://www.b4x.com/android/forum/t...end-authentication-for-signed-in-users.68672/

Please I need help .
I just bought my license of the new version and I ask for help.
I stay in this step!
I have the code SHA1 but I do not know where to put it
It's not very clear to me
 

desof

Well-Known Member
Licensed User
Longtime User
[Quote = "Filippo, mensaje: 449055, miembro de: 103"], pero no puedo encontrar "SHA1", sólo tengo este punto de vista.

[ATTACH = completo] 47565 [/ attach] [/ quote]

Tengo this Mismo Problema ..

Como lo solucionaste ?
 

jtare

Active Member
Licensed User
Longtime User
Nothing works for me ever comes the code flow there!
I put a user and nothing happens.
What can it be?
I have Google Sign-In enabled in Firebase
ES- Al parecer estas traduciendo el código, el código solo funciona en ingles no puede ser traducido (todos los lenguajes de programacion que conozco solo corren en ingles).
EN- It seems that you are translating the code, the code only run in english(all programming languages I know only run in english)
 
Last edited:
Status
Not open for further replies.
Top