react-native-spotify-remote icon indicating copy to clipboard operation
react-native-spotify-remote copied to clipboard

Renew Session with Refresh Token

Open donghee214 opened this issue 4 years ago • 10 comments

Is this left up for our own implementation at the moment or supposed to be renewed with the getSession method? Just curious if I was setting up the server incorrectly or not.

Thank you

donghee214 avatar May 15 '20 21:05 donghee214

hey @donghee214 , I haven't had a chance to look at getting the refresh token stuff set up. I've added this is an enhancement. Thanks

cjam avatar May 16 '20 22:05 cjam

Not too comfortable with native development so unsure if renewSession() would involve some work on that end for proper implementation, but just made a basic helper method to serve as a renewSession() if anyone else needs it.

private async renewSession(refresh_token: string){
       const response = await fetch(`${SPOTIFY_SERVER}/refresh`, {
           method: 'POST',
           headers: {
               Accept: 'application/json',
               'Content-Type': 'application/json'
           },
           body: JSON.stringify({
               refresh_token
           })
       })
       const json = await response.json()
       this.setState((state) => ({
           ...state,
           token: json.access_token
       }))
   }
}

This was added to the context Component in example/AppContext.ts, and you can just pass this as a prop like the other methods to access it throughout your child components

donghee214 avatar May 17 '20 19:05 donghee214

cool! Hopefully something can get incorporated into this package soon for this.

cjam avatar May 17 '20 19:05 cjam

Hey @donghee214 & @cjam just trying to understand the gaps are for refreshing the access token.

Looking at the iOS doc (https://spotify.github.io/ios-sdk/html/Classes/SPTConfiguration.html && https://developer.spotify.com/documentation/ios/guides/token-swap-and-refresh/) it seems like it supports tokenSwap & tokenRefresh, the sdk would call your server urls when it needs a fresh accessToken.

As for Android reference, https://spotify.github.io/android-sdk/auth-lib/docs/, it doesn't mention tokenSwap & tokenRefresh, so I'm assuming Android SDK doesn't support refresh.

Question for @cjam , I see this bit of code here: https://github.com/cjam/react-native-spotify-remote/blob/c0a81d7fb4dae4686b713268aa51471188729562/android/src/main/java/com/reactlibrary/RNSpotifyRemoteAuthModule.java#L45 . Have you tried putting in redirect/refresh url values using the method setCustomParam() from https://spotify.github.io/android-sdk/auth-lib/docs/ from AuthenticationRequest.Builder?

Question for @donghee214 on this post:

Not too comfortable with native development so unsure if renewSession() would involve some work on that end for proper implementation, but just made a basic helper method to serve as a renewSession() if anyone else needs it.

private async renewSession(refresh_token: string){
       const response = await fetch(`${SPOTIFY_SERVER}/refresh`, {
           method: 'POST',
           headers: {
               Accept: 'application/json',
               'Content-Type': 'application/json'
           },
           body: JSON.stringify({
               refresh_token
           })
       })
       const json = await response.json()
       this.setState((state) => ({
           ...state,
           token: json.access_token
       }))
   }
}

This was added to the context Component in example/AppContext.ts, and you can just pass this as a prop like the other methods to access it throughout your child components

For your Android side impl, how did you get the refresh_token? since refresh_token is not returned from SpotifyAuth.authorize() or SpotifyAuth.initialize()?

MrBuggySan avatar Jul 22 '20 23:07 MrBuggySan

@MrBuggySan Interesting thought on the setCustomParam, their docs are pretty slim on that method. Any ideas on what it does?

Just linking this issue with #67 and #68 as they seem to be related.

cjam avatar Sep 06 '20 23:09 cjam

@cjam never mind about using the setCustomParams() it seems like getting the refresh token to work with Spotify Android SDK is still in suggestion phase: https://github.com/spotify/android-sdk/issues/255 We'll just have to refresh the session manually on our own via the POST /refresh of this repo's sample server

MrBuggySan avatar Nov 24 '20 07:11 MrBuggySan

@MrBuggySan

We'll just have to refresh the session manually on our own via the POST /refresh of this repo's sample server

That is what we did; Worked well.

reinhardholl avatar Apr 09 '21 19:04 reinhardholl

It would be great to show put an example of this into the example app. @reinhardholl @MrBuggySan do you guys have a simple bit of code that I could add to the example app and then point to from the docs? It seems to be a common question/need. I haven't had the chance to get this setup in my apps but it's definitely on my road map.

cjam avatar Apr 15 '21 14:04 cjam

@cjam might be an idea to suggest using a general OAuth lib to authorize / refresh? I'm using: https://github.com/FormidableLabs/react-native-app-auth which also features a Spotify example: https://github.com/FormidableLabs/react-native-app-auth/blob/main/docs/config-examples/spotify.md

dylancom avatar Aug 26 '21 09:08 dylancom

I'm using: https://github.com/FormidableLabs/react-native-app-auth which also features a Spotify example: https://github.com/FormidableLabs/react-native-app-auth/blob/main/docs/config-examples/spotify.md

Hi @dylancom Were you able to get this to work well? Did you encounter issues with iOS backgrounding the Spotify app so that reconnecting to it is difficult? Thanks.

bumpingChris avatar Jan 31 '23 02:01 bumpingChris