File tree Expand file tree Collapse file tree 3 files changed +34
-7
lines changed
commonMain/kotlin/com.adamratzman.spotify
jsMain/kotlin/com/adamratzman/spotify/utils Expand file tree Collapse file tree 3 files changed +34
-7
lines changed Original file line number Diff line number Diff line change @@ -624,15 +624,16 @@ open class SpotifyClientApi internal constructor(
624
624
/* *
625
625
* Whether the current access token allows access to scope [scope]
626
626
*/
627
- fun hasScope (scope : SpotifyScope ): Boolean = hasScopes(scope)
627
+ fun hasScope (scope : SpotifyScope ): Boolean? = hasScopes(scope)
628
628
629
629
/* *
630
630
* Whether the current access token allows access to all of the provided scopes
631
631
*/
632
- fun hasScopes (scope : SpotifyScope , vararg scopes : SpotifyScope ): Boolean =
633
- ! isTokenValid(false ).isValid &&
634
- token.scopes.contains(scope) &&
635
- scopes.all { token.scopes.contains(it) }
632
+ fun hasScopes (scope : SpotifyScope , vararg scopes : SpotifyScope ): Boolean? =
633
+ if (token.scopes == null ) null
634
+ else ! isTokenValid(false ).isValid &&
635
+ token.scopes?.contains(scope) == true &&
636
+ scopes.all { token.scopes?.contains(it) == true }
636
637
}
637
638
638
639
Original file line number Diff line number Diff line change @@ -32,9 +32,9 @@ data class Token(
32
32
@Transient
33
33
var expiresAt: Long = getCurrentTimeMs() + expiresIn * 1000
34
34
@Transient
35
- var scopes: List <SpotifyScope > = scopeString?.let { str ->
35
+ var scopes: List <SpotifyScope >? = scopeString?.let { str ->
36
36
str.split(" " ).mapNotNull { scope -> SpotifyScope .values().find { it.uri.equals(scope, true ) } }
37
- } ? : listOf ()
37
+ }
38
38
39
39
fun shouldRefresh (): Boolean = getCurrentTimeMs() > expiresAt
40
40
Original file line number Diff line number Diff line change
1
+ package com.adamratzman.spotify.utils
2
+
3
+ import com.adamratzman.spotify.SpotifyImplicitGrantApi
4
+ import com.adamratzman.spotify.models.Token
5
+ import org.w3c.dom.url.URLSearchParams
6
+ import kotlin.browser.window
7
+
8
+ /* *
9
+ * Parse the current url into a valid [Token] to be used when instantiating a new [SpotifyImplicitGrantApi]
10
+ */
11
+ fun parseSpotifyCallbackHashToToken () = parseSpotifyCallbackHashToToken(window.location.hash.substring(1 ))
12
+
13
+ /* *
14
+ * Parse the hash string into a valid [Token] to be used when instantiating a new [SpotifyImplicitGrantApi]
15
+ *
16
+ * @param hashString The Spotify hash string containing access_token, token_type, and expires_in.
17
+ */
18
+ fun parseSpotifyCallbackHashToToken (hashString : String ): Token {
19
+ val hash = URLSearchParams (hashString)
20
+
21
+ return Token (
22
+ hash.get(" access_token" ) ? : throw IllegalStateException (" access_token is not part of the hash!" ),
23
+ hash.get(" token_type" ) ? : throw IllegalStateException (" token_type is not part of the hash!" ),
24
+ hash.get(" expires_in" )?.toIntOrNull() ? : throw IllegalStateException (" expires_in is not part of the hash!" )
25
+ )
26
+ }
You can’t perform that action at this time.
0 commit comments