Created
October 1, 2019 12:32
-
-
Save yunusemredilber/38798b5a5eeb4e50058e04a020afd4b6 to your computer and use it in GitHub Desktop.
Turbolinks Android - Fix Session Issues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JsBridge { | |
private Context context; | |
public static android.webkit.CookieManager cookieManager = CookieManager.getInstance(); | |
public JsBridge(Context context){ | |
this.context = context; | |
} | |
@JavascriptInterface | |
public void clearSession() { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
cookieManager.removeAllCookies(new ValueCallback<Boolean>() { | |
// A callback which is executed when the cookies have been removed | |
@Override | |
public void onReceiveValue(Boolean aBoolean) { | |
Log.d(TAG, "Cookie removed: " + aBoolean); | |
} | |
}); | |
} | |
else cookieManager.removeAllCookie(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AndroidBridge.clearSession(); // Run this command from web when logging out. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity implements TurbolinksAdapter { | |
// ... | |
protected void onCreate(Bundle savedInstanceState) { | |
// ... | |
// Add JS Interface | |
TurbolinksSession.getDefault(this).getWebView().addJavascriptInterface(new JsBridge(this),"AndroidBridge"); | |
// ... | |
} | |
@Override | |
public void visitCompleted() { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
JsBridge.cookieManager.flush(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment