How to find if a user already exists when registering with OTP phone auth in firebase android?
I'm developing a social media application. In that, I need to check whether the user already exists or not when he login with otp (like in WhatsApp).
I have found similar questions like this in stackoverflow, but none of the questions have correct answer. Help me!
add a comment |
I'm developing a social media application. In that, I need to check whether the user already exists or not when he login with otp (like in WhatsApp).
I have found similar questions like this in stackoverflow, but none of the questions have correct answer. Help me!
add a comment |
I'm developing a social media application. In that, I need to check whether the user already exists or not when he login with otp (like in WhatsApp).
I have found similar questions like this in stackoverflow, but none of the questions have correct answer. Help me!
I'm developing a social media application. In that, I need to check whether the user already exists or not when he login with otp (like in WhatsApp).
I have found similar questions like this in stackoverflow, but none of the questions have correct answer. Help me!
asked Jul 26 '18 at 8:52
SooryaSoorya
72110
72110
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
You can just use the admin SDK and lookup the user by phone number:
https://firebase.google.com/docs/auth/admin/manage-users#retrieve_user_data
admin.auth().getUserByPhoneNumber(phoneNumber)
.then(function(userRecord) {
// User found.
})
.catch(function(error) {
// User not found.
});
add a comment |
If you try to create an user that already exists, Firebase will throw you an exception. You can handle that exception to do what you want.
Hope this helps!
1
This is working for email and password and other login types. But not working for phone authentication. But some how i did it with a different method. I used document.isexist () . Any how thanks for ur answer
– Soorya
Jul 26 '18 at 12:20
add a comment |
First store the Firebase Phone Auth in FirebaseDatabase after every successful registration, then add a check when user try and enter the phone number if it already exist in database, If present then you got your answer otherwise register the user.
I hope the concept is clear. Will update you with the code soon.
add a comment |
after a research, I found this answer.
In firebase, each phone number have a unique user id. So, if a user signs up from different devices, or reinstalls the application with a same phone number, he/she will get the same user Id. So with that, we can check our database like this:
mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
final FirebaseUser user = task.getResult().getUser();
String uid = user.getUid();
final FirebaseFirestore db = FirebaseFirestore.getInstance();
final DocumentReference docRef = db.collection("users").document(uid);
docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(final DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists()) {
//redirect to home page
}
else{
//redirect to sign up page
}
}
Hope this will help someone :)
add a comment |
@Soorya. If you want to make your application like WhatsApp then you have to simply authenticate the user using phone number and OTP. When the user gets verified successfully then check whether user details are available in Firebase Database or FireStore. If available then simply redirect the user to home screen otherwise redirect the user to the registration page. For that refer below code snippet.
mFirebaseAuth
.signInWithCredential(phoneAuthCredential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
goToNextActivity(((AuthResult) task.getResult()).getUser());
return;
}
}
});
private void goToNextActivity(FirebaseUser firebaseUser) {
FirebaseDatabase
.getInstance()
.getReference()
.child("your table name")
.child("user id details")
.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot == null || dataSnapshot.getChildrenCount() <= 0) {
goToRegisterDetails(firebaseUser);
return;
}
User user = (User) dataSnapshot.getValue(User.class);
if (user == null
|| user.getUser_name() == null
|| user.getUser_name().isEmpty()
|| user.getUser_mobile() == null
|| user.getUser_mobile().isEmpty()) {
goToRegisterDetails(firebaseUser);
} else {
goToMainActivity(user);
}
}
public void onCancelled(DatabaseError databaseError) {
OTPVerificationActivity.this.hideProgressDialog();
Logger.e(databaseError.getMessage());
}
});
}
I hope that you will get your answer. You can easily achieve that you want to do in your application.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51534789%2fhow-to-find-if-a-user-already-exists-when-registering-with-otp-phone-auth-in-fir%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can just use the admin SDK and lookup the user by phone number:
https://firebase.google.com/docs/auth/admin/manage-users#retrieve_user_data
admin.auth().getUserByPhoneNumber(phoneNumber)
.then(function(userRecord) {
// User found.
})
.catch(function(error) {
// User not found.
});
add a comment |
You can just use the admin SDK and lookup the user by phone number:
https://firebase.google.com/docs/auth/admin/manage-users#retrieve_user_data
admin.auth().getUserByPhoneNumber(phoneNumber)
.then(function(userRecord) {
// User found.
})
.catch(function(error) {
// User not found.
});
add a comment |
You can just use the admin SDK and lookup the user by phone number:
https://firebase.google.com/docs/auth/admin/manage-users#retrieve_user_data
admin.auth().getUserByPhoneNumber(phoneNumber)
.then(function(userRecord) {
// User found.
})
.catch(function(error) {
// User not found.
});
You can just use the admin SDK and lookup the user by phone number:
https://firebase.google.com/docs/auth/admin/manage-users#retrieve_user_data
admin.auth().getUserByPhoneNumber(phoneNumber)
.then(function(userRecord) {
// User found.
})
.catch(function(error) {
// User not found.
});
answered Jul 30 '18 at 7:40
bojeilbojeil
12.7k11725
12.7k11725
add a comment |
add a comment |
If you try to create an user that already exists, Firebase will throw you an exception. You can handle that exception to do what you want.
Hope this helps!
1
This is working for email and password and other login types. But not working for phone authentication. But some how i did it with a different method. I used document.isexist () . Any how thanks for ur answer
– Soorya
Jul 26 '18 at 12:20
add a comment |
If you try to create an user that already exists, Firebase will throw you an exception. You can handle that exception to do what you want.
Hope this helps!
1
This is working for email and password and other login types. But not working for phone authentication. But some how i did it with a different method. I used document.isexist () . Any how thanks for ur answer
– Soorya
Jul 26 '18 at 12:20
add a comment |
If you try to create an user that already exists, Firebase will throw you an exception. You can handle that exception to do what you want.
Hope this helps!
If you try to create an user that already exists, Firebase will throw you an exception. You can handle that exception to do what you want.
Hope this helps!
answered Jul 26 '18 at 12:18
daan_coverdaan_cover
112
112
1
This is working for email and password and other login types. But not working for phone authentication. But some how i did it with a different method. I used document.isexist () . Any how thanks for ur answer
– Soorya
Jul 26 '18 at 12:20
add a comment |
1
This is working for email and password and other login types. But not working for phone authentication. But some how i did it with a different method. I used document.isexist () . Any how thanks for ur answer
– Soorya
Jul 26 '18 at 12:20
1
1
This is working for email and password and other login types. But not working for phone authentication. But some how i did it with a different method. I used document.isexist () . Any how thanks for ur answer
– Soorya
Jul 26 '18 at 12:20
This is working for email and password and other login types. But not working for phone authentication. But some how i did it with a different method. I used document.isexist () . Any how thanks for ur answer
– Soorya
Jul 26 '18 at 12:20
add a comment |
First store the Firebase Phone Auth in FirebaseDatabase after every successful registration, then add a check when user try and enter the phone number if it already exist in database, If present then you got your answer otherwise register the user.
I hope the concept is clear. Will update you with the code soon.
add a comment |
First store the Firebase Phone Auth in FirebaseDatabase after every successful registration, then add a check when user try and enter the phone number if it already exist in database, If present then you got your answer otherwise register the user.
I hope the concept is clear. Will update you with the code soon.
add a comment |
First store the Firebase Phone Auth in FirebaseDatabase after every successful registration, then add a check when user try and enter the phone number if it already exist in database, If present then you got your answer otherwise register the user.
I hope the concept is clear. Will update you with the code soon.
First store the Firebase Phone Auth in FirebaseDatabase after every successful registration, then add a check when user try and enter the phone number if it already exist in database, If present then you got your answer otherwise register the user.
I hope the concept is clear. Will update you with the code soon.
answered Jul 26 '18 at 12:47
Shubham AgrawalShubham Agrawal
179212
179212
add a comment |
add a comment |
after a research, I found this answer.
In firebase, each phone number have a unique user id. So, if a user signs up from different devices, or reinstalls the application with a same phone number, he/she will get the same user Id. So with that, we can check our database like this:
mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
final FirebaseUser user = task.getResult().getUser();
String uid = user.getUid();
final FirebaseFirestore db = FirebaseFirestore.getInstance();
final DocumentReference docRef = db.collection("users").document(uid);
docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(final DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists()) {
//redirect to home page
}
else{
//redirect to sign up page
}
}
Hope this will help someone :)
add a comment |
after a research, I found this answer.
In firebase, each phone number have a unique user id. So, if a user signs up from different devices, or reinstalls the application with a same phone number, he/she will get the same user Id. So with that, we can check our database like this:
mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
final FirebaseUser user = task.getResult().getUser();
String uid = user.getUid();
final FirebaseFirestore db = FirebaseFirestore.getInstance();
final DocumentReference docRef = db.collection("users").document(uid);
docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(final DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists()) {
//redirect to home page
}
else{
//redirect to sign up page
}
}
Hope this will help someone :)
add a comment |
after a research, I found this answer.
In firebase, each phone number have a unique user id. So, if a user signs up from different devices, or reinstalls the application with a same phone number, he/she will get the same user Id. So with that, we can check our database like this:
mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
final FirebaseUser user = task.getResult().getUser();
String uid = user.getUid();
final FirebaseFirestore db = FirebaseFirestore.getInstance();
final DocumentReference docRef = db.collection("users").document(uid);
docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(final DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists()) {
//redirect to home page
}
else{
//redirect to sign up page
}
}
Hope this will help someone :)
after a research, I found this answer.
In firebase, each phone number have a unique user id. So, if a user signs up from different devices, or reinstalls the application with a same phone number, he/she will get the same user Id. So with that, we can check our database like this:
mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
final FirebaseUser user = task.getResult().getUser();
String uid = user.getUid();
final FirebaseFirestore db = FirebaseFirestore.getInstance();
final DocumentReference docRef = db.collection("users").document(uid);
docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(final DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists()) {
//redirect to home page
}
else{
//redirect to sign up page
}
}
Hope this will help someone :)
answered Jul 26 '18 at 12:48
SooryaSoorya
72110
72110
add a comment |
add a comment |
@Soorya. If you want to make your application like WhatsApp then you have to simply authenticate the user using phone number and OTP. When the user gets verified successfully then check whether user details are available in Firebase Database or FireStore. If available then simply redirect the user to home screen otherwise redirect the user to the registration page. For that refer below code snippet.
mFirebaseAuth
.signInWithCredential(phoneAuthCredential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
goToNextActivity(((AuthResult) task.getResult()).getUser());
return;
}
}
});
private void goToNextActivity(FirebaseUser firebaseUser) {
FirebaseDatabase
.getInstance()
.getReference()
.child("your table name")
.child("user id details")
.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot == null || dataSnapshot.getChildrenCount() <= 0) {
goToRegisterDetails(firebaseUser);
return;
}
User user = (User) dataSnapshot.getValue(User.class);
if (user == null
|| user.getUser_name() == null
|| user.getUser_name().isEmpty()
|| user.getUser_mobile() == null
|| user.getUser_mobile().isEmpty()) {
goToRegisterDetails(firebaseUser);
} else {
goToMainActivity(user);
}
}
public void onCancelled(DatabaseError databaseError) {
OTPVerificationActivity.this.hideProgressDialog();
Logger.e(databaseError.getMessage());
}
});
}
I hope that you will get your answer. You can easily achieve that you want to do in your application.
add a comment |
@Soorya. If you want to make your application like WhatsApp then you have to simply authenticate the user using phone number and OTP. When the user gets verified successfully then check whether user details are available in Firebase Database or FireStore. If available then simply redirect the user to home screen otherwise redirect the user to the registration page. For that refer below code snippet.
mFirebaseAuth
.signInWithCredential(phoneAuthCredential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
goToNextActivity(((AuthResult) task.getResult()).getUser());
return;
}
}
});
private void goToNextActivity(FirebaseUser firebaseUser) {
FirebaseDatabase
.getInstance()
.getReference()
.child("your table name")
.child("user id details")
.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot == null || dataSnapshot.getChildrenCount() <= 0) {
goToRegisterDetails(firebaseUser);
return;
}
User user = (User) dataSnapshot.getValue(User.class);
if (user == null
|| user.getUser_name() == null
|| user.getUser_name().isEmpty()
|| user.getUser_mobile() == null
|| user.getUser_mobile().isEmpty()) {
goToRegisterDetails(firebaseUser);
} else {
goToMainActivity(user);
}
}
public void onCancelled(DatabaseError databaseError) {
OTPVerificationActivity.this.hideProgressDialog();
Logger.e(databaseError.getMessage());
}
});
}
I hope that you will get your answer. You can easily achieve that you want to do in your application.
add a comment |
@Soorya. If you want to make your application like WhatsApp then you have to simply authenticate the user using phone number and OTP. When the user gets verified successfully then check whether user details are available in Firebase Database or FireStore. If available then simply redirect the user to home screen otherwise redirect the user to the registration page. For that refer below code snippet.
mFirebaseAuth
.signInWithCredential(phoneAuthCredential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
goToNextActivity(((AuthResult) task.getResult()).getUser());
return;
}
}
});
private void goToNextActivity(FirebaseUser firebaseUser) {
FirebaseDatabase
.getInstance()
.getReference()
.child("your table name")
.child("user id details")
.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot == null || dataSnapshot.getChildrenCount() <= 0) {
goToRegisterDetails(firebaseUser);
return;
}
User user = (User) dataSnapshot.getValue(User.class);
if (user == null
|| user.getUser_name() == null
|| user.getUser_name().isEmpty()
|| user.getUser_mobile() == null
|| user.getUser_mobile().isEmpty()) {
goToRegisterDetails(firebaseUser);
} else {
goToMainActivity(user);
}
}
public void onCancelled(DatabaseError databaseError) {
OTPVerificationActivity.this.hideProgressDialog();
Logger.e(databaseError.getMessage());
}
});
}
I hope that you will get your answer. You can easily achieve that you want to do in your application.
@Soorya. If you want to make your application like WhatsApp then you have to simply authenticate the user using phone number and OTP. When the user gets verified successfully then check whether user details are available in Firebase Database or FireStore. If available then simply redirect the user to home screen otherwise redirect the user to the registration page. For that refer below code snippet.
mFirebaseAuth
.signInWithCredential(phoneAuthCredential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
goToNextActivity(((AuthResult) task.getResult()).getUser());
return;
}
}
});
private void goToNextActivity(FirebaseUser firebaseUser) {
FirebaseDatabase
.getInstance()
.getReference()
.child("your table name")
.child("user id details")
.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot == null || dataSnapshot.getChildrenCount() <= 0) {
goToRegisterDetails(firebaseUser);
return;
}
User user = (User) dataSnapshot.getValue(User.class);
if (user == null
|| user.getUser_name() == null
|| user.getUser_name().isEmpty()
|| user.getUser_mobile() == null
|| user.getUser_mobile().isEmpty()) {
goToRegisterDetails(firebaseUser);
} else {
goToMainActivity(user);
}
}
public void onCancelled(DatabaseError databaseError) {
OTPVerificationActivity.this.hideProgressDialog();
Logger.e(databaseError.getMessage());
}
});
}
I hope that you will get your answer. You can easily achieve that you want to do in your application.
answered Nov 14 '18 at 6:03
Pradeep TrivediPradeep Trivedi
1918
1918
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51534789%2fhow-to-find-if-a-user-already-exists-when-registering-with-otp-phone-auth-in-fir%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown