spotify / spotify-web-api-ts-sdk

A Typescript SDK for the Spotify Web API with types for returned data.
Other
373 stars 74 forks source link

window object is not accessible while using sdk on server #108

Closed athulanilthomas closed 7 months ago

athulanilthomas commented 7 months ago

Environment

Describe the bug

The problem arises when utilizing the SDK on a Node.js server environment, where access is restricted to web APIs and not the 'window' object. Consequently, when implementing a server API within Nuxt, the absence of a 'window' object renders certain functionalities ineffective. It appears that the redirectOrVerifyToken function was designed with client-side usage in mind. Hence, its inclusion within the withUserAuthorization method raises questions. In essence, the issue arises when attempting to use the SDK within a Node server environment.

Additional context

None

Logs

[nuxt] [request error] [unhandled] [500] window is not defined
  at AuthorizationCodeWithPKCEStrategy.redirectOrVerifyToken (./node_modules/@spotify/web-api-ts-sdk/src/auth/AuthorizationCodeWithPKCEStrategy.ts:51:48)  
  at <anonymous> (./node_modules/@spotify/web-api-ts-sdk/src/auth/AuthorizationCodeWithPKCEStrategy.ts:31:42)  
  at InMemoryCachingStrategy.getOrCreate (./node_modules/@spotify/web-api-ts-sdk/src/caching/GenericCache.ts:31:36)  
  at process.processTicksAndRejections (node:internal/process/task_queues:95:5)  
  at AuthorizationCodeWithPKCEStrategy.getOrCreateAccessToken (./node_modules/@spotify/web-api-ts-sdk/src/auth/AuthorizationCodeWithPKCEStrategy.ts:28:23)  
  at SpotifyApi.makeRequest (./node_modules/@spotify/web-api-ts-sdk/src/SpotifyApi.ts:79:33)  
  at PlayerEndpoints.getRequest (./node_modules/@spotify/web-api-ts-sdk/src/endpoints/EndpointsBase.ts:8:16)  
  at Object.handler (./server/api/spotify/status.get.ts:10:1)
athulanilthomas commented 7 months ago

@fostertheweb can you please take a look into this ?

fostertheweb commented 7 months ago

Only withAccessToken is available on the server, which requires using the mixed auth pattern documented in the readme.

athulanilthomas commented 7 months ago

@fostertheweb In my scenario, there isn't a client, so I need the server to manage the entire authentication process, including redirection.

for example: In a scenario where I need to pass user-related information (user will remain constant) to the client, and the client should not need to know about Spotify authentication or other details, then there will be only one service (which is the server) right ?

fostertheweb commented 7 months ago

This is a client library, so it does not serve that use case. If you don't want to start from scratch using the Spotify Web API, you could try using spotify-node-wrapper

athulanilthomas commented 7 months ago

@fostertheweb Thank you for the suggestion. However, currently, the Node.js wrapper only supports the client credentials model for authentication, which doesn't allow access to personal information. I've devised a workaround to make this SDK compatible with server-side operations by customizing the existing AuthenticateWithPKCE strategy.

For implementation details, please visit: Endpoint