How to Logout user from Spotify after authorization and Web API call is over
Asked Answered
F

4

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.

Freelance answered 25/6, 2014 at 12:16 Comment(0)
R
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.

Ragtime answered 7/7, 2014 at 13:22 Comment(0)
O
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.

Orthocephalic answered 7/6, 2018 at 10:5 Comment(0)
C
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());
Carolecarolee answered 9/4, 2016 at 7:14 Comment(2)
What is the AuthorizationClient referencing here? Or getApplication(). Not a lot of context.Parole
Agreed, no conext. Also you cannot clear cookies you don't own.Spiv
S
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.

Spiv answered 7/4, 2020 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.