-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
46 lines (40 loc) · 1.01 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"fmt"
"log"
"net/http"
"github.com/joho/godotenv"
"github.com/zmb3/spotify/v2"
spotifyauth "github.com/zmb3/spotify/v2/auth"
)
const redirectURI = "http://localhost:8090/callback"
var (
auth *spotifyauth.Authenticator
ch = make(chan *spotify.Client)
state = "abc123"
)
func init() {
err := godotenv.Load()
if err != nil {
log.Fatal(err)
}
auth = spotifyauth.New(
spotifyauth.WithRedirectURL(redirectURI),
spotifyauth.WithScopes(spotifyauth.ScopePlaylistModifyPrivate),
)
}
func completeAuth(w http.ResponseWriter, r *http.Request) {
tok, err := auth.Token(r.Context(), state, r)
if err != nil {
http.Error(w, "Couldn't get token", http.StatusForbidden)
log.Fatal(err)
}
if st := r.FormValue("state"); st != state {
http.NotFound(w, r)
log.Fatalf("State mismatch: %s != %s\n", st, state)
}
client := spotify.New(auth.Client(r.Context(), tok), spotify.WithRetry(true))
w.Header().Set("Content-Type", "text/html")
fmt.Fprintf(w, "Login completed!")
ch <- client
}