@@ -11,16 +11,50 @@ import com.adamratzman.spotify.spotifyImplicitGrantApi
11
11
import com.spotify.sdk.android.auth.AuthorizationClient
12
12
import com.spotify.sdk.android.auth.AuthorizationRequest
13
13
import com.spotify.sdk.android.auth.AuthorizationResponse
14
+ import com.spotify.sdk.android.auth.LoginActivity
14
15
16
+ /* *
17
+ * Wrapper around spotify-auth's [LoginActivity] that allows configuration of the authentication process, along with
18
+ * callbacks on successful and failed authentication. Pair this with [SpotifyDefaultCredentialStore] to easily store credentials.
19
+ *
20
+ * @property state The state to use to verify the login request.
21
+ * @property clientId Your application's Spotify client id.
22
+ * @property clientId Your application's Spotify client secret.
23
+ * @property redirectUri Your application's Spotify redirect id - NOTE that this should be an android scheme (such as spotifyapp://authback)
24
+ * and that this must be registered in your manifest.
25
+ * @property useDefaultRedirectHandler Disable if you will not be using [useDefaultRedirectHandler] but will be setting [SpotifyDefaultAuthHelper.activityBack].
26
+ *
27
+ */
15
28
public abstract class AbstractSpotifyLoginActivity : Activity () {
16
29
public abstract val state: Int
17
30
public abstract val clientId: String
18
31
public abstract val redirectUri: String
19
- public abstract val useDefaultRedirectHandler: Boolean
32
+ public open val useDefaultRedirectHandler: Boolean = true
33
+
34
+ /* *
35
+ * Return the scopes that you are going to request from the user here.
36
+ */
20
37
public abstract fun getRequestingScopes (): List <SpotifyScope >
21
38
39
+ /* *
40
+ * Override this to define what to do after authentication has been successfully completed. A valid, usable
41
+ * [spotifyApi] is provided to you. You may likely want to use [SpotifyDefaultCredentialStore] to store/retrieve this token.
42
+ *
43
+ * @param spotifyApi Valid, usable [SpotifyImplicitGrantApi] that you can use to make requests.
44
+ */
22
45
public abstract fun onSuccessfulAuthentication (spotifyApi : SpotifyImplicitGrantApi )
46
+
47
+ /* *
48
+ * Override this to define what to do after authentication has failed. You may want to use [SpotifyDefaultCredentialStore] to remove any stored token.
49
+ */
23
50
public abstract fun onAuthenticationFailed (errorMessage : String )
51
+
52
+
53
+ /* *
54
+ * Override this to define what to do after [onSuccessfulAuthentication] has run.
55
+ * The default behavior is to finish the activity, and redirect the user back to the activity set on [SpotifyDefaultAuthHelper.activityBack]
56
+ * only if [guardValidSpotifyApi] has been used or if [SpotifyDefaultAuthHelper.activityBack] has been set.
57
+ */
24
58
public open fun redirectAfterOnSuccessAuthentication () {
25
59
if (useDefaultRedirectHandler && SpotifyDefaultAuthHelper .activityBack != null ) {
26
60
startActivity(Intent (this , SpotifyDefaultAuthHelper .activityBack))
@@ -35,15 +69,21 @@ public abstract class AbstractSpotifyLoginActivity : Activity() {
35
69
triggerLoginActivity()
36
70
}
37
71
72
+ /* *
73
+ * Trigger the actual spotify-auth login activity to authenticate the user.
74
+ */
38
75
public fun triggerLoginActivity () {
39
- val authorizationRequest =
40
- AuthorizationRequest . Builder (clientId, AuthorizationResponse . Type . TOKEN , redirectUri )
41
- .setScopes(getRequestingScopes().map { it.uri }.toTypedArray ())
42
- .setState(state.toString() )
43
- .build()
76
+ val authorizationRequest = AuthorizationRequest . Builder (clientId, AuthorizationResponse . Type . TOKEN , redirectUri)
77
+ .setScopes(getRequestingScopes().map { it.uri }.toTypedArray() )
78
+ .setState(state.toString ())
79
+ .build( )
80
+
44
81
AuthorizationClient .openLoginActivity(this , state, authorizationRequest)
45
82
}
46
83
84
+ /* *
85
+ * Processes the result of [LoginActivity], invokes callbacks, then finishes.
86
+ */
47
87
override fun onActivityResult (requestCode : Int , resultCode : Int , intent : Intent ? ) {
48
88
super .onActivityResult(requestCode, resultCode, intent)
49
89
if (requestCode == state) {
@@ -58,9 +98,7 @@ public abstract class AbstractSpotifyLoginActivity : Activity() {
58
98
val api = spotifyImplicitGrantApi(
59
99
clientId = clientId,
60
100
token = token
61
- ) {
62
- refreshTokenProducer = { throw SpotifyException .ReAuthenticationNeededException () }
63
- }
101
+ )
64
102
65
103
onSuccessfulAuthentication(api)
66
104
redirectAfterOnSuccessAuthentication()
0 commit comments