16

Im using Spotify Web API to get list of playlist and track of user. Authorization is working fine. Also i do get track details. But after that I want to logout user from spotify and allow new login . There is a session time till the user auto logout from spotify account. But user might not have patience to wait so long and re try with different spotify account.

Is there any API to logout user from spotify.

What can be done. Please help.

4 Answers4

15

While an application using the Spotify Web API can't log a user out, it can force the permissions dialog to be shown during the login process. For that, use the show_dialog query parameter and set it to true when calling the https://accounts.spotify.com/authorize endpoint.

If there is a user already logged in, there will be a (Not you?) link to change the user.

This applies to the 'Authorization Code' and 'Implicit Grant' flows. You can read the documentation about the show_dialog parameter on the Spotify Web API Authorization Guide.

José M. Pérez
  • 3,053
  • 19
  • 36
5

As addition to José M. Pérez his answer, if you really want to log out, it seems the only way you could achieve this is by opening the Spotify log out URL in the user's browser: https://www.spotify.com/logout/

To prevent CORB protection this could for example be achieved by opening a pop up window with JavaScript and close it after 2 seconds:

const url = 'https://www.spotify.com/logout/'                                                                                                                                                                                                                                                                               
const spotifyLogoutWindow = window.open(url, 'Spotify Logout', 'width=700,height=500,top=40,left=40')                                                                                                
setTimeout(() => spotifyLogoutWindow.close(), 2000)

UPDATE: https://accounts.spotify.com/en/logout could also be used, this will redirect to a login page instead of the Spotify main page which is nicer IMHO.

jpoppe
  • 1,878
  • 22
  • 23
2

You want to simply logout from "spotify web api" then you should terminate your spotify session through clear authentication token like:

AuthenticationClient.clearCookies(getApplication());
Dhruv Raval
  • 4,578
  • 2
  • 27
  • 33
1

I solved this using the asnwer @jpoppe provided. I didn't like the idea of having a popup window so I used an iframe instead.

<iframe style={{display: 'none'}} src="https://spotify.com/logout"></iframe> 

One problem I found with this was if the user had signed in with facebook, when logging in on another account using facebook, they would automatically be signed in when they clicked the 'sign in with facebook' button. I couldn't find a simple way to solve this.

James Webb
  • 151
  • 2
  • 16