Matdweb / Jamming

This project allows you 👥 to create and customize 🎨 playlists 🎼 and send them to your Spotify account using API technology.
https://jamming-sooty.vercel.app
MIT License
0 stars 1 forks source link

Retrieve access token from spotify api #22

Closed Matdweb closed 1 year ago

Matdweb commented 1 year ago

About this Pull Request

As explain in the #20 ticket we need an access-token to start making other requests to the Spotify API. This, after we already have an authenticated user and a valid session-token. Learn more about the app state manager in #21 pull request.

Here you will find the step by step of:

How do we retrieve an access token?

First, as explained previously we need a valid session-token. This is accomplish using the useSession() from nextAuth, learn about it here, and learn about this app authentication with #14 PR.

Once we have the session-token:

api/spotify/accessToken

This endpoint is created to make requests in the backend because of security and best practices. In here we:

Storage in SpotifyContext

Now, everytime the getAccessToken() is called anyware. This checks if there's an access-token. If there's not, then request it. So, by making the call to this function, in the client, the state should always be updated with a valid value.

How to retrieve the access token in the client?

  1. retrive the session using the useSession() method
  2. retrieve the session-token in the session, like this: session?.token.accessToken
  3. use the spotifyContext using the useContext(), learn more here
  4. retrieve the getAccessToken() form the Context
  5. call the previous function using the refresh-token as parameter

This should look like this: image

Test

As established in ticket #20 a test for this functionality was created with the 'Save this in Spotify' Button IMPORTANT: the token expiration-time was reduced for test purposes, but in the final code we set the right time expiration

retrieveAccessToken

vercel[bot] commented 1 year ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
jamming ✅ Ready (Inspect) Visit Preview Sep 14, 2023 4:21am