From 4e5539f703b1d3c824a0a446e9ad0733fd0afed6 Mon Sep 17 00:00:00 2001 From: Hugo Lageneste Date: Sat, 4 Apr 2020 17:16:26 +0200 Subject: [PATCH] Remove credentials when they are wrong --- modules/spotify/authentication.go | 17 ++++++++++++++--- modules/start/spotify.go | 3 ++- server/websocket.go | 1 + 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/modules/spotify/authentication.go b/modules/spotify/authentication.go index e3350a2..bbadbff 100644 --- a/modules/spotify/authentication.go +++ b/modules/spotify/authentication.go @@ -2,7 +2,6 @@ package spotify import ( "fmt" - "log" "net/http" "os" "time" @@ -52,6 +51,16 @@ func LoginSpotify(token string) string { go func() { authenticationToken := <-tokenChannel + // If the token is empty reset the credentials of the user + if *authenticationToken == (oauth2.Token{}) { + user.ChangeUserInformation(token, func(information user.Information) user.Information { + information.SpotifyID = "" + information.SpotifySecret = "" + + return information + }) + } + // Save the authentication token user.ChangeUserInformation(token, func(information user.Information) user.Information { information.SpotifyToken = authenticationToken @@ -92,12 +101,14 @@ func CompleteAuth(w http.ResponseWriter, r *http.Request) { if err != nil { http.Error(w, "Couldn't get token", http.StatusForbidden) - log.Fatal(err) + tokenChannel <- &oauth2.Token{} + return } if st := r.FormValue("state"); st != state { http.NotFound(w, r) - fmt.Printf("State mismatch: %s != %s\n", st, state) + tokenChannel <- &oauth2.Token{} + return } // Use the token to get an authenticated client diff --git a/modules/start/spotify.go b/modules/start/spotify.go index 5a0ead7..031ad64 100644 --- a/modules/start/spotify.go +++ b/modules/start/spotify.go @@ -22,7 +22,8 @@ func CheckSpotifyLogin(token string) { client := spotify.RenewSpotifyToken(token) // Test to search a track to see if the user is well logged in - _, err := _modules.SearchTrack(client, "test") + _, err := _modules.SearchTrack(client, "In Your Eyes") + // If an error is present, login the user to spotify if err != nil { SetMessage(spotify.LoginSpotify(token)) diff --git a/server/websocket.go b/server/websocket.go index 0e7ef4f..121a079 100644 --- a/server/websocket.go +++ b/server/websocket.go @@ -72,6 +72,7 @@ func SocketHandle(w http.ResponseWriter, r *http.Request) { Tag: "start module", Information: user.GetUserInformation(request.Token), } + fmt.Println(user.GetUserInformation(request.Token)) bytes, err := json.Marshal(response) if err != nil {