adds trace log to http, fixes login flow #14

Merged
abs3nt merged 2 commits from fix2 into main 2023-03-07 19:22:19 +00:00
Showing only changes of commit 11abbc57cd - Show all commits

View File

@ -3,7 +3,6 @@ package auth
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -57,42 +56,30 @@ func GetClient(ctx *gctx.Context) (*spotify.Client, error) {
), ),
) )
if _, err := os.Stat(filepath.Join(configDir, "gospt/auth.json")); err == nil { if _, err := os.Stat(filepath.Join(configDir, "gospt/auth.json")); err == nil {
authFile, err := os.Open(filepath.Join(configDir, "gospt/auth.json")) authFilePath := filepath.Join(configDir, "gospt/auth.json")
authFile, err := os.Open(authFilePath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer authFile.Close() defer authFile.Close()
authValue, err := io.ReadAll(authFile) tok := &oauth2.Token{}
err = json.NewDecoder(authFile).Decode(tok)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var tok *oauth2.Token authClient := auth.Client(ctx, tok)
err = json.Unmarshal(authValue, &tok) client := spotify.New(authClient)
if err != nil {
return nil, err
}
client := spotify.New(auth.Client(ctx, tok))
new_token, err := client.Token() new_token, err := client.Token()
if err != nil { if err != nil {
return nil, err return nil, err
} }
if new_token != tok {
out, err := json.MarshalIndent(new_token, "", " ") out, err := json.MarshalIndent(new_token, "", " ")
if err != nil { if err != nil {
panic(err.Error()) return nil, err
} }
err = os.WriteFile(filepath.Join(configDir, "gospt/auth.json"), out, 0o644) err = os.WriteFile(authFilePath, out, 0o644)
if err != nil { if err != nil {
panic("FAILED TO SAVE AUTH") return nil, fmt.Errorf("failed to save auth")
}
}
out, err := json.MarshalIndent(tok, "", " ")
if err != nil {
panic(err.Error())
}
err = os.WriteFile(filepath.Join(configDir, "gospt/auth.json"), out, 0o644)
if err != nil {
panic("FAILED TO SAVE AUTH")
} }
return client, nil return client, nil
} }