public void register(final String username, final String fullname, String email, String password) {
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(RegisterActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
pd.show();
FirebaseUser firebaseUser = auth.getCurrentUser();
String userID ="Queenlab#"+firebaseUser.getUid();
// circleIV.setImageDrawable(drawable2);
reference = FirebaseDatabase.getInstance().getReference().child("Users").child(userID);
HashMap<String, Object> map = new HashMap<>();
map.put("id",userID);
map.put("username", username.toLowerCase());
map.put("fullname", fullname);
map.put("imageurl", "https://th.bing.com/th/id/OIP.PXxWJplK9ObsWdxdFCqLOwHaE8?pid=ImgDet&rs=1");
map.put("bio", "");
map.put("instgram", "");
reference.setValue(map).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(RegisterActivity.this, "Register sccessful sending Email Verification", Toast.LENGTH_SHORT).show();
sendVerificationEmail();
auth.signOut();
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
});
} else {
pd.dismiss();
Toast.makeText(RegisterActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
The UID of a user is determined by the authentication provider. It can't be set from client-side application code, as that would be a security risk.
If you want to determine the UID of your users, you can implement your own provider. Note that this will include writing code that runs in a trusted environment, such as a server that you control, or Cloud Functions/Cloud Run.
Alternatively you can implement a mapping from the UID of the user to whatever ID system you want to use in your database.
Related
I'm trying to store details of user like name, email,age etc using Firebase in Android Studio, but it seems only the email and pasword is getting stored. Here's my code, where have I done mistakes? Only the real time database part of Firebase isn't somehow working here. Also, I couldn't post full code here because of "most of it is code add some more details" error.
regButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Email,Password etc are variables
final String Email= email.getText().toString().trim();
final String Password= password.getText().toString().trim();
final String FullName= fullName.getText().toString().trim();
final String Phone= phone.getText().toString().trim();
final String Gender= gender.getText().toString().trim();
final String Age= age.getText().toString().trim();
if (TextUtils.isEmpty(Email)){
email.setError("Email is required!");
return;
}
else{
loader.setMessage("Registration in process....");
loader.setCanceledOnTouchOutside(false);
loader.show();
mAuth.createUserWithEmailAndPassword(Email,Password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(!task.isSuccessful()){
String error =task.getException().toString();
Toast.makeText(SelectRegistrationActivity.this,
"Error occurred:" + error,Toast.LENGTH_SHORT).show();
}
else{
String currentUserId = mAuth.getCurrentUser().getUid();
userDatabaseRef= FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserId);
HashMap userInfo=new HashMap();
userInfo.put("Name",fullName);
userInfo.put("Email",email);
userInfo.put("Age",age);
userInfo.put("Gender",gender);
userInfo.put("Phone",phone);
userInfo.put("Type","Patient");
userDatabaseRef.updateChildren(userInfo).addOnCompleteListener(new OnCompleteListener() {
#Override
public void onComplete(#NonNull Task task) {
if (task.isSuccessful()){
Toast.makeText(SelectRegistrationActivity.this, "Details set Successful", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(SelectRegistrationActivity.this, task.getException().toString(), Toast.LENGTH_SHORT).show();
}
finish();
loader.dismiss();
}
});
}
}
});
im working on a project with firebase, and i have some problems creating users.
there is my code:
private void createAccount(String email, String password) {
Log.d(TAG, "createAccount:" + email);
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "createUserWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
}
else {
Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(getApplicationContext(), "Authentication failed.", Toast.LENGTH_SHORT).show();
}
}
});
}
I am going to insert my email and password into Firebase Authentication. However, the code that I found on the Internet does not work for me? Below is the code and when I click next button, it goes back to the previous page too, it does not proceed with the next page?
public void completeRegis() {
username1 = username.getText().toString().trim();
email1 = email.getText().toString().trim();
psd1 = psd.getText().toString().trim();
psd2 = reconpsd.getText().toString().trim();
mAuth.createUserWithEmailAndPassword(email1, psd1)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
//start profile activity here
User user = new User(username1, email1,psd1);
FirebaseDatabase.getInstance().getReference("Users")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
public void onComplete(#NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(RoleInfo1.this, "Registration successful.", Toast.LENGTH_SHORT).show();
startActivity(new Intent(RoleInfo1.this, HomePage.class ));
progressBar.setVisibility(View.GONE);
} else {
Toast.makeText(RoleInfo1.this, "Database not created", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}
});
} else {
Toast.makeText(RoleInfo1.this, "Registration not successful, please try again.", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
});
}
private void initializeUI() {
username = findViewById(R.id.usernameregister);
email = findViewById(R.id.emailregister);
psd = findViewById(R.id.psdregister);
reconpsd = findViewById(R.id.reconpsdregister);
progressBar = findViewById(R.id.progressBar);
}
}
I am very new to android but I recently made a signUp page successfully.
declare a firebase auth instance
private FirebaseAuth mAuth;
private EditText mName, mEmailField, mConfirmPass, mNewPass;
then in onCreate() I declared them as
mName = (EditText) findViewById(R.id.eName);
mEmailField = (EditText) findViewById(R.id.fieldEmail);
mConfirmPass = (EditText) findViewById(R.id.fieldConfirm);
mNewPass = (EditText) findViewById(R.id.fieldNew);
mAuth = FirebaseAuth.getInstance();
I added a button in the authentication page for signup. Clicking on it starts the signUp procedure. This is done in onCreate() method
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startSignUp();
}
});
Then I declare the startSignUp() method as below
public void startSignUp(){
String name = mName.getText().toString();
String email = mEmailField.getText().toString();
String newPass = mNewPass.getText().toString();
String conPass = mConfirmPass.getText().toString();
if(TextUtils.isEmpty(name) || TextUtils.isEmpty(email) || TextUtils.isEmpty(newPass) || TextUtils.isEmpty(conPass)){
Toast.makeText(SignUp.this, "Fields Empty" , Toast.LENGTH_LONG).show();
}
mAuth.createUserWithEmailAndPassword(email,newPass).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(! task.isSuccessful()){
Toast.makeText(SignUp.this, "SignUp Failed", Toast.LENGTH_LONG).show();
}else {
openAuthetication();
}
}
});
}
}
If signUp is successful, it will go back to the authentication page for signing in. This is done in the openAuthetication() method.
public void openAuthetication(){
Intent intent = new Intent(this, Authetication.class);
startActivity(intent);
}
I am trying to make a login and signup page using Firebase on android, and I can register users but not login, I can see the registered users in the database. The login method always goes to the else part. Here is the code
mLogin_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String email = mLoginEmail.getEditText().toString();
String password = mLoginPassword.getEditText().toString();
if(!TextUtils.isEmpty(email) && !TextUtils.isEmpty(password)){
mLoginProgress.setTitle("Loggin In");
mLoginProgress.setMessage("Please Wait!");
mLoginProgress.setCanceledOnTouchOutside(false);
mLoginProgress.show();
loginUser(email, password);
}
}
});
}
private void loginUser(String email, String password) {
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
mLoginProgress.dismiss();
Intent mainIntent = new Intent(LoginActivity.this, MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mainIntent);
finish();
} else{
mLoginProgress.hide();
Toast.makeText(LoginActivity.this, "nope, skipping to else", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Try this:
else{
mLoginProgress.hide();
Toasty.error(getApplicationContext(), "Authentication failed: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
Replace your toast with the toast above to be able to know why it is going to the else statement.
Change this:
String email = mLoginEmail.getEditText().toString();
String password = mLoginPassword.getEditText().toString();
to this:
String email = mLoginEmail.getText().toString();
String password = mLoginPassword.getText().toString();
I know how to create email and password authentication with firebase but that will create only email and id but how do i add name and more detail to that id for instance how I call user.getdisplayname?
Here is my code for creating authentication email and password
bv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final ProgressDialog pros=ProgressDialog.show(Register.this,"please wait..","registerring..",true);
mAuth.createUserWithEmailAndPassword(email.getText().toString(),password.getText().toString()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
pros.dismiss();
if(task.isSuccessful()){
Toast.makeText(Register.this,"sucsseful",Toast.LENGTH_LONG).show();
Intent i=new Intent(Register.this,login.class);
}else {
Log.e("ERROr",task.getException().toString());
Toast.makeText(Register.this,task.getException().getMessage(),Toast.LENGTH_LONG).show();
}
}
});
If you say to create database as well, then how I link it to the user authentication?
private FirebaseUser UserDetaill = FirebaseAuth.getInstance().getCurrentUser() ;
You can use the UserProfileChangeRequest like this
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName("XXX YYYY")
.setPhotoUri(URI)
.build();
UserDetaill.updateProfile(profileUpdates);
USING TASKS
FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
.continueWithTask(new Continuation<AuthResult, Task<? extends Object>>() {
#Override
public Task<? extends Object> then(#NonNull Task<AuthResult> task) throws Exception {
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName("XXX YYYY")
.setPhotoUri(URI)
.build();
return task.getResult().getUser().updateProfile(profileUpdates);
}
});