z-huang / InnerTune

A Material 3 YouTube Music client for Android
GNU General Public License v3.0
4.76k stars 287 forks source link

Issue#1686 YouTube cookie set to null by logout. #1694

Open zeratul47 opened 3 weeks ago

zeratul47 commented 3 weeks ago

Fixes z-huang/InnerTune#1686

  1. YouTube.cookie set to null by logout.
  2. Update settings.gradle.kts to auto download dependencies.
gechoto commented 3 weeks ago

Hi, thanks for working on this. I think there is room for improvement...

Your current version does not really clear the cookie in the saved preferences/settings. It does only prevent the cookie from loading in on startup but it is still saved. This means the cookie is still there and probably still in backups for example, which is not exactly great.

What do you think about moving this logic into LoginScreen.kt instead? Setting the cookie there should also correctly save it.

I guess this is where the cookie is updated from the webview: https://github.com/z-huang/InnerTune/blob/c5b2856126927b65ae2d264c1f6a0e712a84c703/app/src/main/java/com/zionhuang/music/ui/screens/LoginScreen.kt#L62

You could try to set the cookie to null in this place (if it does not contain SAPISID). (you have to test, I'm currently only writing down an idea)

zeratul47 commented 3 weeks ago

Thank you for the feedback. You are right. I will check it too. And the stuff with visitorData.

zeratul47 commented 3 weeks ago

Hi, I have updated the code according to your suggestions. Now cookie and visitorData should be removed from preference storage by logout.

gechoto commented 2 weeks ago

Hi @zeratul47 your current code still does not set the cookie to null in the settings. Now it is an empty string. This can be improved.

For example what do you think about this code in LoginScreen.kt:

val youtubeCookieString = CookieManager.getInstance().getCookie(url)
GlobalScope.launch {
    if ("SAPISID" in youtubeCookieString) { // if logged in
        innerTubeCookie = youtubeCookieString
    } else { // if logged out
        context.dataStore.edit { settings ->
            settings.remove(InnerTubeCookieKey)
        }
    }
    YouTube.accountInfo().onSuccess {
[...]

This should be much simpler. It does completely remove the cookie from the settings so it is really reset and read as null again (same as if the user never logged in).

With this you can remove your changes in App.kt.


As for visitorData I think it should not be set to an empty string. It should always be filled, just with something new and unrelated to the account.

You could just let it generate a new visitorData by finding a way to run this code again: https://github.com/z-huang/InnerTune/blob/c5b2856126927b65ae2d264c1f6a0e712a84c703/app/src/main/java/com/zionhuang/music/App.kt#L86-L89

Should be easy since it already automatically re-runs if the visitorData is null or "null". One way to solve this could be by also removing it from the settings:

fun onRetrieveVisitorData(newVisitorData: String?) {
    if (innerTubeCookie == "") { // clear visitorData after logout (this will be regenerated in App.kt)
        GlobalScope.launch {
            context.dataStore.edit { settings ->
                settings.remove(VisitorDataKey)
            }
        }
        return
    }
[...]
zeratul47 commented 2 weeks ago

I have updated code, by your advice. But It seems that Home page updating only on close - reopen of the app. Should it be so?