Android phone authentication - java

Getcurrentuser() return null
I facing a problem with getcurrentuser() after signout
Getinstance().signout
In phone authentication
Now currentuser return null how to overcome this?
I use phone authentication to login into the android app. Everything was working fine until when I use the "sign out" button using getinstance().signout
And after that, when I login again using phone number, I could log into the app, but now I am getting error to get current, it returns null.
How to overcome this problem? I am stuck in it.

When you call signOut() on an instance of FirebaseAuth:
Signs out the current user and clears it from the disk cache.
Which basically means that the FirebaseUser object becomes null. So what you're experiencing is normal behavior. So each time you want to use a FirebaseUser object make sure the user is authenticated. In code you should check that object against nullity. If you however want to take some action when a user signs out, then I recommend you to use an AuthStateListener, as explained in my answer from the following post:
One time login in app - FirebaseAuth

Related

Wicket: double login required due to extended browser info

I have a Wicket 8.6 application. Currently, when logging in to the application, mostly (does not always happen) the user has to login twice. After the first login (after entering the credentials and clicking the submit button) a white page appears saying "If you see this, it means that both javascript and meta-refresh are not support by your browser configuration. Please click this link to continue to the original destination." This is the BrowserInfoPage. After a few seconds the user is redirected to the login page again where he/she has to enter his/her credentials again and press the login button. This time, the user logs in successfully. My question is, how do I prevent that the user hast to enter his/her credentials twice.
From my research I know that it has something to do with the collection of extended browser info. In the init method of my WicketApplication class, I had the following code:
getRequestCycleSettings().setGatherExtendedBrowserInfo(true);
However, I already commented out this code several month ago. For some reason, the described effect occurs for every new deploy now. Maybe a newly added package in the application is the reason for it. I don't know. Is there a possibility to prevent this second login maybe by creating a customized bowser info page which forwards the login? Please point me in the right direction. Thanks.
After some research, I came up with a work around. It is probably not very efficient but it works for me so far. In my custom Session class which inherits from AuthenticatedWebSession, I added the following code.
#Override
protected WebPage newBrowserInfoPage() {
final Request request = RequestCycle.get().getRequest();
if(request.getUrl().toString().contains("LoginPage")) {
if(!isSignedIn()) {
signIn(username, password);
}
PageParametersEncoder encoder = new PageParametersEncoder();
PageParameters parameters = encoder.decodePageParameters(request.getUrl());
String url = parameters != null && parameters.get("originUrl") !=null && !parameters.get("originUrl").isNull() && !parameters.get("originUrl").isEmpty()?
parameters.get("originUrl").toString("pages/home"):"pages/home";
String finalUrl=url.startsWith("pages/")?url.substring("pages/".length()):url;
throw new RedirectToUrlException(finalUrl);
}
return super.newBrowserInfoPage();
}
Some explanation to the code. As mentioned in the question, I want to prevent the user from logging in multiple times. Thus, I check if the request comes from the LoginPage and perform my work around only in that case.
During my implementation, I realized, that the method newBrowserInfoPage is called in the process when I call session.signIn(username,password); on my LoginPage. In this signIn process the authenticate method of my custom Session is called but the signedIn flag in the AuthenticatedWebSession is not changed (keeps false on successfull authentication). Is this a bug? Thus, I have to login again to set the flag to true.
Finally, I read the URL of the LoginPage where I have stored the target URL and forward the user to the target URL.
I am aware this is probably not the best approach but it is the only solution I came up with. If someone has a better idea, I am happy to hear it.

Firebase Authentication returning specific user when app is uninstalled and Installed again

I have an issue where I am using google sign in for my application + Firebase.
Suppose there are 3 users X,Y and Z with whom I am login in my application at one instance of time.
My code for checking the user is logged in or not:
FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser!=null)
{
//user logged in and get user detail from currentUser and go to HomePage
}
else{
//Show sign in button
}
Scenario :
I signed it with multiple accounts and logged out. everything working fine.
But when I delete the application and again install it. The Y user is automatically return by mAuth.getCurrentUser().
I tried login with X and Uninstall the application.
Still, if I install it again it directly takes me to Y user.
I tried login with Z and Uninstall the application.
Still, if I install it again it directly takes me to Y user.
I even tried login out and Delete the application. Again if I install, it returns Y user without even asking for sign in.
NOTE : I havent cleared cache or data . If I do that everything works fine.
The issue is just unistalling the app.
What must be the reason ?
From the docs:
When a user signs up or signs in, that user becomes the current user of the Auth instance. The Firebase Auth instance persists the user's state, so that refreshing the page (in a browser) or restarting the application doesn't lose the user's information.
When the user signs out, the Auth instance stops keeping a reference to the User object and no longer persists its state; there is no current user. However, the user instance continues to be completely functional: if you keep a reference to it, you can still access and update the user's data.
So to solve this, the best way is to create a button and sign out the user. That way that user won't be logged in when you restart the application.
FirebaseAuth.getInstance().signOut();
More info here: https://firebase.google.com/docs/auth/users
Also this question related to ios (but same idea): Firebase - Deleting and reinstalling app does not un-authenticate a user
Some other alternatives also:
adding android:allowBackup="false" in your <application> in manifest.
android:allowBackup
Whether to allow the application to participate in the backup and restore infrastructure. If this attribute is set to false, no backup or restore of the application will ever be performed, even by a full-system backup that would otherwise cause all application data to be saved via adb. The default value of this attribute is true.
Do this as a Test:
Delete Cache and Data
Login with User Y and Logout
Login with User X and Logout
Uninstall the application
Install the application login with user X.
It is important to have FirebaseAuth.getInstance().signOut(); when logging out.

Android Using Retrofit and OkHttp to get Endpoints

I have a project setup. https://github.com/Axiom1999/Iron-Banner-Companion/blob/master/app/src/main/java/me/axiom/aapp/ironbannercompanion/
And I have a button which I need to check with the API if the username on the platform exists and if it can get information then start the MainActivity intent.
I do not know how to initially start the connection.
You can use LoginActivity for checking if user already exists. Check this out in onCreate() (i think the best way keep this data in SharedPreferences). If user exists try to login and show LoginActivity with ProgressBar. If user does not exists simply show Login/SignUpActivity

Facebook login problems

So for the past few days I've been trying to get my head around the Facebook SDK for android. I've managed to get the user to log in but only by using
loginBtn.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
This means every time the user goes to the activity containing the login button they are forced to re-enter their details (username and password) every time. I've followed the tutorials provided on the facebook developers site however I still can't manage to get a simple one time login working. The whole point of this is that I'm trying to get a very simple straight forward image upload button. Press button - check if logged in, if not, login - once logged in post image. But I'm just having trouble with keeping a constant login state, I have managed to get the upload image working however like I said, once the user goes to a different activity they are forced to login again. Surely it should only force them to once on the button click.
Check if they are logged in already:
facebook.isSessionValid()
Better way to do this:
public boolean isLoggedIn() {
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
return true;
} else {
return false;
}
}

bringing up different page when logged in

let me start off by saying happy holidays to everyone!
ok i really just need confirmation and correction if needed.
what im trying to do:
Im using google and facebook "Log-in" feature to sign in for my app to retrieve the data needed like name email etc.
where i Need help:
after signing in i want another activity to be the forefront everytime app opens unless the user signs out then of course, it takes them back to the original main page to sign back in.
now im assuming this takes place in maybe the lifecycle right?
somthing like:
#override
OnResume
{
//if user is signed in cast an Intent to automatically go to another activity?
}
am i on the right track on no? thanks in advance guys
I'm not sure off the top of my head how Google and Facebook's login is implemented... do they have a sample project you're using?
And yep, you're on the right track! Generally speaking you should be able to have a "Main" activity (MainActivity for this example) which checks to see if the login was successful, and if so, kicks you to the activity you want (LoggedInActivity).
This would be in the onCreate() or onResume() method of MainActivity.java
onResume() { // onCreate() should work, too.
if (loggedIn) {
startActivity(new Intent(this, LoggedInActivity.class));
} else {
// send them to login
}
}
If there's not a good way to check if they're logged in, you could save a boolean value or api token using SharedPreferences once the login is successful, and check that value (that'd be the value of loggedIn) every time at launch. You'd obviously need to clear that value any time you logged out.

Categories