18

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.

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

9

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.

Comments

1

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

AuthenticationClient.clearCookies(getApplication());

2 Comments

What is the AuthorizationClient referencing here? Or getApplication(). Not a lot of context.
Agreed, no conext. Also you cannot clear cookies you don't own.
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.

Comments

0

after you hit the logout, you can set a slight delay by setting a callback, in the timeout callback function you can redirect your app to your home page.

app.get('logout', (req,res) => {
setTimeout(() =>{    redirect('/');},2000); 
)};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.