Authentication
Authentication Methods
- Authorization Code Flow with PKCE
- Client Credentials Flow
- Implicit Grant Flow
- Mixed Server and Client Side Authentication
We do auto-token refresh when expired and a refresh token is available.
Picking an Authentication Method
If you’re building a browser based application, you should use Authorization Code Flow with PKCE. This is the most secure way to authenticate your users and handles the redirection from your app to Spotify and back. Your server side code will not have access to the Spotify API with user access scopes, but you can use the SDK to perform client side requests with the users access token.
Calling any of the methods on the SDK will automatically perform any redirects/refreshes that are necessary.
If you’re building a server side application, you should use Client Credentials Flow, and is the correct choice when you have both your Client ID and Client Secret available. This flow is not available in the browser (as you should not embed your Client Secret in Client Side web applications), so should only be used from Node.js.
Mixed Server and Client Side Authentication is a special case, and is covered in the section below. This is useful if you want to perform requests with a users access token from your server side code.
Mixed Server and Client Side Authentication
There’s capabilities in the client if you want to interact with Spotify from your Node.js server, but perform a client side Authorization Code Flow with PKCE. You might want to do this if you want your server side SDK instance to be authorized “as a specific user” to interact with user data.
You’ll need to do three things.
- Perform Authorization Code Flow with PKCE using some special helper functions
- Expose a URL from your Node.js application that accepts a token post-back
- Initilise an instance of the SDK with the posted-back token
Setup:
Client Side
These functions will work as usual, triggering a client side redirect to grant permissions, along with verifying the response and performing token exchange.
Server Side
Check out the blog post for more examples using ES Modules or CommonJS