Skip to content

Commit 7ab0093

Browse files
committed
Sign in failing for android
1 parent 56d2757 commit 7ab0093

File tree

3 files changed

+56
-29
lines changed

3 files changed

+56
-29
lines changed

android/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ A hybrid mobile app built with **Next.js (TypeScript)** frontend, **Firebase bac
55
Right now it's just a webview wrapper around the web application, but in the future it may
66
contain native code as well.
77

8+
/!\ This is still a work in progress, as google sign in does not work yet. Most of the issue is that Google doesn't allow to sign in inside the webview for security reasons.
9+
10+
We could sign in in a browser with signInWithRedirect and then redirect to the webview, but I haven't found a way to make it come back to the app and store the token.
11+
12+
We could also use a native sign in with SocialLogin, but it only runs on local assets, not on the webview that loads the remote sign-in page. So one way would be to force the webview to load the local assets, but it's not a clean solution and I haven't found a way to do it yet.
13+
14+
Third solution is to implement a native app, not using webview, in React Native.
15+
16+
If you can make it work, please contribute!
17+
18+
819
This document describes how to:
920
1. Build and run the web frontend and backend locally
1021
2. Sync and build the Android WebView wrapper
@@ -101,7 +112,7 @@ npx cap sync android
101112

102113
### Load from site
103114

104-
During local development, open Android Studio project and run the app on an emulator or your physical device.
115+
During local development, open Android Studio project and run the app on an emulator or your physical device. Note that right now you can't use a physical device for the local web version (`10.0.2.2:3000 time out` )
105116

106117
```
107118
npx cap open android

android/app/src/main/java/com/compass/app/MainActivity.java

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.compass.app;
22

33
import android.content.Intent;
4-
import android.net.Uri;
54
import android.os.Bundle;
65
import android.util.Log;
6+
import android.webkit.JavascriptInterface;
77
import android.webkit.WebView;
8-
import android.webkit.WebViewClient;
98

109
import com.getcapacitor.BridgeActivity;
10+
import com.getcapacitor.BridgeWebViewClient;
1111
import com.getcapacitor.Plugin;
1212
import com.getcapacitor.PluginHandle;
1313

@@ -16,43 +16,58 @@
1616
import ee.forgr.capacitor.social.login.SocialLoginPlugin;
1717

1818
public class MainActivity extends BridgeActivity implements ModifiedMainActivityForSocialLoginPlugin {
19+
public class NativeBridge {
20+
@JavascriptInterface
21+
public boolean isNativeApp() {
22+
return true;
23+
}
24+
}
25+
26+
private static final String LOCAL_URL = "file:///android_asset/public/server/pages";
27+
private static final String REMOTE_URL = "https://www.compassmeet.com";
28+
29+
// Optional helper for future use
30+
public void loadLocalContent() {
31+
Log.i("CompassApp", "Loading local assets...");
32+
this.bridge.getWebView().loadUrl(LOCAL_URL);
33+
}
34+
35+
public void loadRemoteContent() {
36+
Log.i("CompassApp", "Loading remote content...");
37+
this.bridge.getWebView().loadUrl(REMOTE_URL);
38+
}
39+
1940
@Override
2041
public void onCreate(Bundle savedInstanceState) {
2142
super.onCreate(savedInstanceState);
22-
WebView.setWebContentsDebuggingEnabled(true);
2343

2444
WebView webView = this.bridge.getWebView();
45+
webView.setWebViewClient(new BridgeWebViewClient(this.bridge));
46+
47+
WebView.setWebContentsDebuggingEnabled(true);
2548

2649
// Set a recognizable User-Agent (always reliable)
2750
webView.getSettings().setUserAgentString(
2851
webView.getSettings().getUserAgentString() + " CompassAppWebView"
2952
);
3053

31-
// To know in JS if we are running from an APK
32-
webView.evaluateJavascript("window.IS_APK = true;", null);
54+
webView.getSettings().setJavaScriptEnabled(true);
55+
webView.addJavascriptInterface(new NativeBridge(), "AndroidBridge");
3356

34-
webView.setWebViewClient(new WebViewClient() {
35-
@Override
36-
public void onPageFinished(WebView view, String url) {
37-
super.onPageFinished(view, url);
38-
// This runs once the document exists
39-
view.evaluateJavascript("window.IS_APK = true;", null);
40-
}
41-
42-
@Override
43-
public boolean shouldOverrideUrlLoading(WebView view, String url) {
44-
// Load only compassmeet.com URLs in WebView
45-
if (url.startsWith("https://www.compassmeet.com")) {
46-
view.loadUrl(url);
47-
return true;
48-
}
49-
50-
// All other URLs open externally
51-
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
52-
startActivity(intent);
53-
return true; // handled externally
54-
}
55-
});
57+
// Allow remote URLs to still have access to Capacitor bridge
58+
// webView.setWebViewClient(new BridgeWebViewClient(this.bridge) {
59+
// @Override
60+
// public boolean shouldOverrideUrlLoading(android.webkit.WebView view, String url) {
61+
// if (url.startsWith("https://www.compassmeet.com")) {
62+
// view.loadUrl(url);
63+
// return true;
64+
// }
65+
// return super.shouldOverrideUrlLoading(view, url);
66+
// }
67+
// });
68+
//
69+
// // Load your remote site instead of local assets
70+
// this.bridge.getWebView().loadUrl("https://www.compassmeet.com");
5671
}
5772

5873
@Override

capacitor.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const config: CapacitorConfig = {
99
webDir: 'web/.next',
1010
server: {
1111
url: LOCAL_ANDROID ? "http://10.0.2.2:3000" : 'https://compassmeet.com',
12-
cleartext: true
12+
cleartext: true,
13+
"allowNavigation": ["www.compassmeet.com"],
1314
},
1415
};
1516

0 commit comments

Comments
 (0)