carry out firebase authentication from a service class? - java

I have an activity for new users that consists of 3 edit texts, one for entering their email address, setting their password and entering receipt numbers. Before signing up using firebase users are supposed to pay for services through the banks and use the receipt number to confirm payment in the app, when the user clicks the sign up button the entered receipt number is sent to the private server, which sends back an ID. This all works, but I am trying to send the parsed ID number from the json response along with the users inputed email address and password to a service class, where a sign up method starts after 30 seconds. In the sign up method, the ID is sent to the server for verification and when a valid json response is received from the server, it creates a new firebase user using the string password and email address passed from the main activity. I however get a null pointer error in the same line where the email address and password is put in the firebase create new user method. Below is how I get the email address and password from the main activity
public Signupservice() {
String a;
String b;
}
......
#Override
public int onStartCommand(Intent intent, int flags , int startId){
a=intent.getStringExtra(FILENAMED);
b=intent.getStringExtra(FILEEXD);
......
firebaseAuth.createUserWithEmailAndPassword(a, b).addOnCompleteListener( new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toast.makeText(getApplicationContext(), "signupunsuccessful: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),"suucessfull",Toast.LENGTH_SHORT).show();
However I receive the following error from the logcat in reference to the first line of code from the method create user with email and password
2019-10-02 20:40:15.539 20184-20184/com.chomba.haroldking.kupa E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.chomba.haroldking.kupa, PID: 20184
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.firebase.auth.FirebaseAuth.createUserWithEmailAndPassword(java.lang.String, java.lang.String)' on a null object reference
at com.chomba.haroldking.kupa.Signupservice.parseData(Signupservice.java:159)
Below is how string is sent to the service class
Intent startServiceIntent=new Intent(Signupuser.this,Signupservice.class);
startServiceIntent.putExtra(FILENAMED,emailIDfed);
startServiceIntent.putExtra(FILEEX,gce);
startServiceIntent.putExtra(FILENAMEDD,paswdfed);
Please render assistance

firebaseAuth is null. It looks like you have not assigned it before first using it. Make sure to assign it first: firebseAuth = FirebaseAuth.getInstance().

Related

validate existing user's mobile number in firebase

I have a firebase project which allows users to login only if they are already exists in identifier in authentication .
I already added few users using my web app with mobile numbers.
Now, in android I have used the signInWithPhoneAuthCredential method to get the users login.
But in this method, it allows any users to login even if the user is first time entering the mobile number.
Is there any method to restrict this ?
Sample Code :
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = task.getResult().getUser();
// ...
} else {
// Sign in failed, display a message and update the UI
Log.w(TAG, "signInWithCredential:failure", task.getException());
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
// The verification code entered was invalid
}
}
}
});
}
According to the docs:
signInWithPhoneNumber:
Asynchronously signs in using a phone number. This method sends a code via SMS to the given phone number, and returns a firebase.auth.ConfirmationResult. After the user provides the code sent to their phone, call firebase.auth.ConfirmationResult.confirm with the code to sign the user in.
This is the default behavior in all applications that uses phone number as login. There is no method in the firebase docs that can restrict this.
you have to use firebase database also with this code to save user info, that way you can achieve your use case

Get user's displayname in firebase function once account is created

I have an Android app where the user registers for an account with their email, password, and displayName. I want to send a welcome email to the user after they create an account. This is how I create my account and set their display name.
// This happens in Android (RegisterActivity.java) where I create user's account
FirebaseAuth auth = FirebaseAuth.getInstance();
auth.createUserWithEmailAndPassword(email, password)
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
#Override
public void onSuccess(AuthResult authResult) {
FirebaseUser user = authResult.getUser();
user.updateProfile(new UserProfileChangeRequest.Builder()
.setDisplayName(name).build());
}
});
Once the user's account is created, a firebase function is triggered. Below is my code:
// This happens in Firebase Cloud Functions (index.js)
exports.sendWelcomeEmail = functions.auth.user().onCreate((user) => {
// send welcome email here
console.log(user.displayName) // displayName is null here
});
I believe that the firebase function gets triggered as soon as the account creation is successful, and I set user's displayname after the account is created and so displayName is null in the function. What can I do to solve the issue? I don't want to send an email to the user without their name in the email.
tl;dr: displayName is being called before is being set.
The onCreate function runs after the front end finishes createUserWithEmailAndPassword but before the displayName is set.
Either you run an onCall function after you update the displayName or as suggested on the firebase github thread on this issue you can add a profile parameter to the createUserWithEmailAndPassword so that we create new accounts with a pre-populated profile.

How to create unique token, in every users firebase android?

I want to generate and store token everytime users login. I tried for my code below, but it always generate same token when I login with another account.
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
AlertDialog.dismiss();
if (!task.isSuccessful()) {
if (password.length() < 6) {
new SweetAlertDialog(LoginActivity.this, SweetAlertDialog.ERROR_TYPE)
.setTitleText("Oops...")
.setContentText("Enter minimum 6 charachters !! ")
.show();
} else {
passwordInput.setText("");
new SweetAlertDialog(LoginActivity.this, SweetAlertDialog.ERROR_TYPE)
.setTitleText("Oops...")
.setContentText("Authentication failed !!")
.show();
}
} else {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
FirebaseUser users = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("users/"+ users.getUid());
String token = FirebaseInstanceId.getInstance().getToken();
Log.e("tokenid",""+token);
mDatabase.child("token_id").setValue(token);
finish();
}
}
});
}
});
please help, thanks..
When using the following line of code:
String token = FirebaseInstanceId.getInstance().getToken();
You are getting the Firebase Instance ID token, which is also known as FCM token, on the client side. You need to know that in Firebase there two different tokens:
A FCM Token/Instance ID token that identifies an installation of a specific application on a specific user device. It doesn't identify in any way a particular user.
An Auth ID token identifies a specific user of a specific application. It doesn't identify in any way a particular device.
The two tokens are quite different and serve different purposes. Please see the official documentation for Android regarding on how to retrieve ID tokens on clients.
The identity of the user is remembered indefinitely (or until you sign out). But their credentials (or rather, whether their token is still valid) are re-checked every hour.
FCM generates a registration token for the client app instance, hence It may happen that you'll get the same token for different users in your app. You can use forceRefresh to generate a new token every time. Register new token everytime user logins to any device and save it in DB and update with a new token on new login this way you will have a new token for each user on every login (If this fits your requirement)
Here is a good answer to understand how it works Firebase FCM force onTokenRefresh() to be called
Use UUID.randomUUID().toString()
You can read more here.
java docs - and here =)

Parseuser save in background still saving to it's local storage when exception is thrown

I wanted to allow the user to update their email and it works just fine when they enter everything correctly but when they don't something weird happens
So for this example (which is the one that I have been testing) the current user email is email#gmail.com and they want to replace it with h as their email.
Here's the code for updating user email
// get current user
currentUser = ParseUser.getCurrentUser();
// get user input (shortened to show result)
final String email = "h";
// reset user email
currentUser.setUsername(email);
currentUser.setEmail(email);
// save progress
currentUser.saveInBackground(new SaveCallback()
{
#Override
public void done(ParseException e)
{
if (null == e)
{
// success
// tell user of success
Toast.makeText(getActivity(), "new email is " + email,
Toast.LENGTH_SHORT).show();
// restart activity to update information
restartActivity();
}
else
{
// update failed
// look at the ParseException to see what happened.
errorMessageDialog("Oops!", e.getMessage());
}
}
});
So obviously there will be a parse exception thrown and it will be that it is not a valid email address and when I check the database that is the case. Nothing was updated in any of the rows; however, if I check the current email later on then something unexpected happens
// get current user
currentUser = ParseUser.getCurrentUser();
// get username
currentUser.getUsername();
// get email
currentUser.getEmail();
When I run that code, it says my email and username are h but if I check the database it's still example#gmail.com
It appears that parse still updates it's local cache even when there was an exception thrown from the database? I don't know what the problem is perhaps saveInBackground(new SaveCallback() is the wrong method to use?

SNS aws not working properly

Hello all i wrote an application grabbing photos from facebook. I did that successfully. Now i wrote a notification service using SNS in java. Basically sending out subscription for first time users who log into my application and also when a pictured has been deleted from the repository. My first problem is when i download the pics and user info from facebook, i want to check if its a new user or not. If a new user send out a subscription and if not(basically user exist in mongoDb dont send out email for subscription) but my code keeps sending out email to everyuser. And lastly when a user deletes a photo they get a notification but when i tested it i failed to get an email. Below is my code could someone tell me what im doing wrong.
public class EmailNotifications {
private static final String accessKey = "****************";
private static final String secretAccess ="***********************";
//Notification when user gets info from facebook app for first time.
public static void SignUP(String email, String Topic){
AmazonSNSClient snsClient = new AmazonSNSClient(new BasicAWSCredentials(accessKey, secretAccess));
snsClient.setRegion(Region.getRegion(Regions.US_WEST_1));
//create a Topic
CreateTopicRequest createTopicRequest = new CreateTopicRequest().withName(Topic);
CreateTopicResult createTopicResult = snsClient.createTopic(createTopicRequest);
//subscribes to a topic
SubscribeRequest subscribeRequest = new SubscribeRequest().withTopicArn(createTopicResult.getTopicArn())
.withProtocol("email").withEndpoint(email);
snsClient.subscribe(subscribeRequest);
}
//Notification when photo is deleted
public static void deletePic(String email, String topic, String message){
AmazonSNSClient snsClient = new AmazonSNSClient(new BasicAWSCredentials(accessKey,secretAccess));
snsClient.setRegion(Region.getRegion(Regions.US_WEST_1));
CreateTopicRequest create = new CreateTopicRequest(topic);
CreateTopicResult result = snsClient.createTopic(create);
System.out.println(result);
//String msg = "My text published to SNS topic with email endpoint";
PublishRequest publishRequest = new PublishRequest(result.getTopicArn(), message);
publishRequest.setSubject("Deleted Pic");
/*PublishResult pu= */snsClient.publish(publishRequest);
}
}
Below is my implementation of both delete and grabbing data for first assuming mongodb is empty:
Delete photo implementation:
#Override
//deletes photo from mongoDB... but doesn't send out an email stating phootid
public String deletePhoto(String id, String PhotoId){
String mssg="";
if(accountRepo.exists(id)){
UserAccounts userAccounts=accountRepo.findById(id);
UserPhotos photos = photoRepo.findByPhotoId(PhotoId);
mssg="The picture "+photos.getPhotoId()+" has been deleted from the application";
EmailNotifications.deletePic(userAccounts.getEmail(),topic,mssg);
photoRepo.delete(PhotoId);
return "Photo is deleted";
}
else
return "Photo does not exist";
}
Grabbing photo from face for the first time. The user should get only one notification max. But i keep getting several messages.
#Override
public UserAccounts create(FacebookClient facebookClient){
User me = facebookClient.fetchObject("me", User.class);
UserAccounts userAccounts = new UserAccounts();
userAccounts.setEmail(me.getEmail());
userAccounts.setGender(me.getGender());
userAccounts.setId(me.getId());
userAccounts.setName(me.getName());
accountRepo.save(userAccounts);
EmailNotifications.SignUP(me.getEmail(), topic);
return userAccounts;
}
Could some one assist me on this
Judging by your description and the code, it would guess the email you keep getting when you download for a user is the subscription confirmation email because all EmailNotifications.SignUp does is subscribe the email address.
I would guess that the reason you haven't getting any email when you delete a picture is because you haven't confirmed the subscription. In the subscription confirmation emails, there should be a link you can click on to confirm the subscription.
As for why you keep getting the email every time you download, I can't tell from your code, but in the create method you show there is not if block around calling SignUp to check if the user already existed, which I imagine is your problem.
As an aside, if your application is interacting with users and you want a good email experience, you would probably be better off using SES, which allows you to completely control the formatting and branding of your email.

Categories

Resources