• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Can I use Android WebView object for Google Authentication?

acenspades

Lurker
Sep 21, 2021
1
0
I'm using Android Studio to update a login page that authenticates via Google. In the onCreate method, I have a function, called loadWebViewPage(), that loads up our authentication page that uses Google for auth. When loadWebViewPage() is called I have the following:

private void loadWebViewPage()
{

CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
String userAgent = "Mozilla/5.0 (Linux; Android 4.1.1; Galaxy Nexus Build/JRO03C) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Mobile Safari/535.19";
//Set WebView
WebView webView =(WebView)findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setUserAgentString(userAgent);
//Set WebSettings
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}

webView.loadUrl("the url that redirects to sso page");

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

LocalBroadcastManager.getInstance(getApplicationContext())
.registerReceiver(loginBroadcastReceiver,
new IntentFilter(LoginHelper.LOGIN_MESSAGE));

}

The onCreate method works fine; I get the page to load, users can enter their email and password. However when the users clicks the login button it does not want to load in Google authentication where they should be able to select which Google account they want to log in with.

This line webView.loadUrl("the url that redirects to sso page") loads up a vendor hosted page that handles the sso authentication. If user enters an invalid email or incorrect email/password combination it does not allow them to proceed forward and presents a "login error' message.
If user enters a valid Google email that is in the system linked by a userID, then it is supposed to present them with the Google option to either enter their password or select the appropriate Google account if they are logged into multiple Google accounts.

I tried looking for a solution and what I've seen is the User Agent or Google doesn't support Webviews anymore to I need to setup a browser that handles auth then go back into app on successful login.

I'm thinking the easiest solution would be to update the the User Agent variable. However, am I missing something or should be approaching this differently? I don't want to load an actual browser as we don't want users messing with the url address or having to worrying about the navigation buttons.

Thanks
 
Last edited:

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones