Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion app/src/main/java/com/zionhuang/music/ui/screens/LoginScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.viewinterop.AndroidView
import androidx.datastore.preferences.core.edit
import androidx.navigation.NavController
import com.zionhuang.innertube.YouTube
import com.zionhuang.innertube.utils.parseCookieString
import com.zionhuang.music.LocalPlayerAwareWindowInsets
import com.zionhuang.music.R
import com.zionhuang.music.constants.AccountChannelHandleKey
Expand All @@ -30,11 +32,13 @@ import com.zionhuang.music.constants.InnerTubeCookieKey
import com.zionhuang.music.constants.VisitorDataKey
import com.zionhuang.music.ui.component.IconButton
import com.zionhuang.music.ui.utils.backToMain
import com.zionhuang.music.utils.dataStore
import com.zionhuang.music.utils.rememberPreference
import com.zionhuang.music.utils.reportException
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlin.collections.contains

@SuppressLint("SetJavaScriptEnabled")
@OptIn(ExperimentalMaterial3Api::class, DelicateCoroutinesApi::class)
Expand All @@ -59,8 +63,15 @@ fun LoginScreen(
webViewClient = object : WebViewClient() {
override fun doUpdateVisitedHistory(view: WebView, url: String, isReload: Boolean) {
if (url.startsWith("https://music.youtube.com")) {
innerTubeCookie = CookieManager.getInstance().getCookie(url)
val youtubeCookieString = CookieManager.getInstance().getCookie(url)
GlobalScope.launch {
if ("SAPISID" in parseCookieString(youtubeCookieString)) { // if logged in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you use parseCookieString here?

To my understanding this should be enough:
if ("SAPISID" in youtubeCookieString) { // if logged in

since youtubeCookieString is already a string and we can simply check if it contains the substring "SAPISID".

I do not see a reason to parse the cookies here as this would only take longer.
Was there a reason why you added it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, if we do not parse the string, then how "SAPISID" in youtubeCookieString will work?
I probably can use youtubeCookieString.contains("SAPISID").

Anyway I will check it one time (and documentation too), and change if necessary.
Thank you for checking!

Copy link
Contributor

@gechoto gechoto Nov 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how "SAPISID" in youtubeCookieString will work?

it checks if youtubeCookieString contains "SAPISID"

From Kotlin docs: https://kotlinlang.org/docs/operator-overloading.html#in-operator

a in b translates into b.contains(a)

so you have both options:

  1. youtubeCookieString.contains("SAPISID")
  2. "SAPISID" in youtubeCookieString

use whatever you think looks better - they both work

innerTubeCookie = youtubeCookieString
} else { // if logged out
context.dataStore.edit { settings ->
settings.remove(InnerTubeCookieKey)
}
}
YouTube.accountInfo().onSuccess {
accountName = it.name
accountEmail = it.email.orEmpty()
Expand All @@ -84,6 +95,15 @@ fun LoginScreen(
addJavascriptInterface(object {
@JavascriptInterface
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
}

if (newVisitorData != null) {
visitorData = newVisitorData
}
Expand Down
4 changes: 4 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ dependencyResolutionManagement {
}
}

plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version("0.4.0")
}

rootProject.name = "InnerTune"
include(":app")
include(":innertube")
Expand Down