cjam / react-native-spotify-remote

React Native wrapper around the Spotify Remote SDK
MIT License
255 stars 101 forks source link

Renew Session with Refresh Token #42

Open donghee214 opened 4 years ago

donghee214 commented 4 years ago

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

cjam commented 4 years ago

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

donghee214 commented 4 years ago

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

cjam commented 4 years ago

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

MrBuggySan commented 4 years ago

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()?

cjam commented 4 years ago

@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.

MrBuggySan commented 3 years ago

@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

reinhardholl commented 3 years ago

@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.

cjam commented 3 years ago

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.

dylancom commented 3 years ago

@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

bumpingChris commented 1 year ago

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.