... where can I find the strFirebaseSignedInUserToken?
I´ll capture the thread here to give a impression of the lib i wrapped so far... Basically it is working. Did not made much tests with it for sure...
As of now i dont know how - in b4a - i could work with the result given from the log.
Ok, here we go.
B4X:Sub Globals 'These global variables will be redeclared each time the activity is created. 'These variables can only be accessed from this module. Dim realtime As FirebaseDatabase End Sub Sub Activity_Create(FirstTime As Boolean) 'Do not forget to load the layout file created with the visual designer. For example: 'Activity.LoadLayout("Layout1") realtime.Initialize("Realtime") realtime.PersistenceEnabled = True realtime.goOnline Dim ref As DatabaseReference ref.Initialize("Reference",realtime.getReferencefromUrl("https://tactical-patrol-603.firebaseio.com/")) ref.addChildEventListener ref.addListenerForSingleValueEvent ref.addValueEventListener etc etc etc etc ;) End Sub
Please note that i created the database in the console with importing a json file to the database....
Attached you can find a json export from the firebase console...
dim m as map
m.initialize
m.put("fieldname", value)
[,,]
ref.UpdateChilds(m)
package com.google.firebase.quickstart.database;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.quickstart.database.models.User;
public class SignInActivity extends BaseActivity implements View.OnClickListener {
private static final String TAG = "SignInActivity";
private DatabaseReference mDatabase;
private FirebaseAuth mAuth;
private EditText mEmailField;
private EditText mPasswordField;
private Button mSignInButton;
private Button mSignUpButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
mDatabase = FirebaseDatabase.getInstance().getReference();
mAuth = FirebaseAuth.getInstance();
// Views
mEmailField = (EditText) findViewById(R.id.field_email);
mPasswordField = (EditText) findViewById(R.id.field_password);
mSignInButton = (Button) findViewById(R.id.button_sign_in);
mSignUpButton = (Button) findViewById(R.id.button_sign_up);
// Click listeners
mSignInButton.setOnClickListener(this);
mSignUpButton.setOnClickListener(this);
}
@Override
public void onStart() {
super.onStart();
// Check auth on Activity start
if (mAuth.getCurrentUser() != null) {
onAuthSuccess(mAuth.getCurrentUser());
}
}
private void signIn() {
Log.d(TAG, "signIn");
if (!validateForm()) {
return;
}
showProgressDialog();
String email = mEmailField.getText().toString();
String password = mPasswordField.getText().toString();
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signIn:onComplete:" + task.isSuccessful());
hideProgressDialog();
if (task.isSuccessful()) {
onAuthSuccess(task.getResult().getUser());
} else {
Toast.makeText(SignInActivity.this, "Sign In Failed",
Toast.LENGTH_SHORT).show();
}
}
});
}
private void signUp() {
Log.d(TAG, "signUp");
if (!validateForm()) {
return;
}
showProgressDialog();
String email = mEmailField.getText().toString();
String password = mPasswordField.getText().toString();
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "createUser:onComplete:" + task.isSuccessful());
hideProgressDialog();
if (task.isSuccessful()) {
onAuthSuccess(task.getResult().getUser());
} else {
Toast.makeText(SignInActivity.this, "Sign Up Failed",
Toast.LENGTH_SHORT).show();
}
}
});
}
private void onAuthSuccess(FirebaseUser user) {
String username = usernameFromEmail(user.getEmail());
// Write new user
writeNewUser(user.getUid(), username, user.getEmail());
// Go to MainActivity
startActivity(new Intent(SignInActivity.this, MainActivity.class));
finish();
}
private String usernameFromEmail(String email) {
if (email.contains("@")) {
return email.split("@")[0];
} else {
return email;
}
}
private boolean validateForm() {
boolean result = true;
if (TextUtils.isEmpty(mEmailField.getText().toString())) {
mEmailField.setError("Required");
result = false;
} else {
mEmailField.setError(null);
}
if (TextUtils.isEmpty(mPasswordField.getText().toString())) {
mPasswordField.setError("Required");
result = false;
} else {
mPasswordField.setError(null);
}
return result;
}
// [START basic_write]
private void writeNewUser(String userId, String name, String email) {
User user = new User(name, email);
mDatabase.child("users").child(userId).setValue(user);
}
// [END basic_write]
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_sign_in:
signIn();
break;
case R.id.button_sign_up:
signUp();
break;
}
}
}
// [START basic_write]
private void writeNewUser(String userId, String name, String email) {
User user = new User(name, email);
mDatabase.child("users").child(userId).setValue(user);
}
// [END basic_write]
private void writeNewPost(String userId, String username, String title, String body) {
// Create new post at /user-posts/$userid/$postid and at
// /posts/$postid simultaneously
String key = mDatabase.child("posts").push().getKey();
Post post = new Post(userId, username, title, body);
Map<String, Object> postValues = post.toMap();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/posts/" + key, postValues);
childUpdates.put("/user-posts/" + userId + "/" + key, postValues);
mDatabase.updateChildren(childUpdates);
}
private void postComment() {
final String uid = getUid();
FirebaseDatabase.getInstance().getReference().child("users").child(uid)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get user information
User user = dataSnapshot.getValue(User.class);
String authorName = user.username;
// Create new comment object
String commentText = mCommentField.getText().toString();
Comment comment = new Comment(uid, authorName, commentText);
// Push the comment, it will appear in the list
mCommentsReference.push().setValue(comment);
// Clear the field
mCommentField.setText(null);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Firebase auth is needed to use the examples, since the authentication runs with access tokens.
If you already have a project with a Firebase setup than you easily import the following modules:
testfiba.bas
fibadbhelp.bas
and don't forget to set the needed variables in starter (e.g. "strFirebaseSignedInUserToken")
As said above "If you recognize serious security problems or performance issues please let me know."
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("testfiba")
Starter.auth.Initialize("Auth")
End Sub
Sub Button4_Click ' SIGN IN
Log("#-Button4_Click")
If Starter.auth.CurrentUser.IsInitialized Then
Auth_SignedIn(Starter.auth.CurrentUser )
End If
'
End Sub
For x=0 To lista.Size-1
Log("uploading to firebase="&lista.Get(x))
Storage.UploadFile(File.DirRootExternal,"/DCIM/Camera/"&lista.Get(x),$"/Public/${Pais& DateTime.Date(DateTime.Now)&"/" &lista.Get(x)}"$)
ref.Initialize("Reference",realtime.getReferencefromUrl("https://sorryITisAlittleSECRET.firebaseio.com/"& Pais& DateTime.Date(DateTime.now) &"/" & x ))
ref.setValue(lista.Get(x),"I don't know")
Next
ref.Initialize("Reference",realtime.getReferencefromUrl("https://sorryITisAlittleSECRET.firebaseio.com"))
ref.addChildEventListener
ref.addListenerForSingleValueEvent
ref.addValueEventListener
Log("ref imagesizes = "&ref.Child("ve08"))
...But obviously it never initialize...
and I can't excecute these lines to sign in...
Is there an errormessage if you look in the filtered log?
Did you had a working test with Erels example here?
Do you have your "Auth" setup complete in the Firebase console?
Sub Button4_Click ' SIGN IN
Log("#-Button4_Click")
If Starter.auth.CurrentUser.IsInitialized Then
Auth_SignedIn(Starter.auth.CurrentUser )
Else
Starter.auth.SignInWithGoogle
End If
'
End Sub
No I have just checked the logs and there is no error message
this line is properly executed : Starter.auth.Initialize("Auth")
But this one gives "false" : If Starter.auth.CurrentUser.IsInitialized Then
I must do something wrong but where ?
I am running in debug mode
Thanks for your help anyway !
Hi Daniel,
the value for strFirebaseSignedInUserToken will be automatically assigned by the Sub auth_TokenAvailable after the user has successfully signed in.
View attachment 46754
In Service starter <-------
Sub Service_Create
'This is the program entry point.
'This is a good place to load resources that are not specific to a single activity.
auth.Initialize("auth")
If auth.CurrentUser.IsInitialized Then Auth_SignedIn(auth.CurrentUser)
End Sub
Sub Auth_SignedIn (User As FirebaseUser)
Log("SignedIn: " & User.DisplayName)
' lblName.Text = "Hello: " & User.DisplayName
End Sub
and in activity testfiba <----
Sub Button4_Click ' SIGN IN
Log("#-Button4_Click")
auth.SignInWithGoogle 'I only added it:D
End Sub
LoL, we are in the same page in the concurrent time . I noticed too. mmm the firebase database synchronized our minds too lolYes I have found where it fails !
this sub must be modified like this :
B4X:Sub Button4_Click ' SIGN IN Log("#-Button4_Click") If Starter.auth.CurrentUser.IsInitialized Then Auth_SignedIn(Starter.auth.CurrentUser ) Else Starter.auth.SignInWithGoogle End If ' End Sub
I will now check the interesting part : yours !
...synchronized...
@Daniel-White and @freedom2000 be aware, the example from #28 uses the "REST api" and not the "Realtime lib".
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?