Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Remove credentials when they are wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
hugolgst committed Apr 4, 2020
1 parent faadd81 commit 4e5539f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
17 changes: 14 additions & 3 deletions modules/spotify/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package spotify

import (
"fmt"
"log"
"net/http"
"os"
"time"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion modules/start/spotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions server/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4e5539f

Please sign in to comment.