Skip to content

Commit

Permalink
add auth logic in main (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jul 15, 2023
1 parent 48d177d commit 3dd704f
Showing 1 changed file with 56 additions and 2 deletions.
58 changes: 56 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,65 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package main

import "github.com/JunNishimura/Chatify/cmd"
import (
"context"
"fmt"
"log"
"net/http"

"github.com/JunNishimura/Chatify/cmd"
"github.com/google/uuid"
"github.com/joho/godotenv"
"github.com/pkg/browser"
"github.com/zmb3/spotify/v2"
spotifyauth "github.com/zmb3/spotify/v2/auth"
)

const (
envFileName = ".env"
redirectURI = "http://localhost:8888/callback"
port = "8888"
)

var (
auth *spotifyauth.Authenticator
clientChannel = make(chan *spotify.Client)
server *http.Server
state string
)

func main() {
if err := godotenv.Load(envFileName); err != nil {
log.Fatalf("fail to load env file: %v\n", err)
}
auth = spotifyauth.New(
spotifyauth.WithRedirectURL(redirectURI),
spotifyauth.WithScopes(spotifyauth.ScopeUserReadPrivate),
)

server = &http.Server{Addr: fmt.Sprintf(":%s", port)}
state = uuid.New().String()
http.HandleFunc("/callback", completeAuth)
go func() {
if err := server.ListenAndServe(); err != http.ErrServerClosed {
log.Fatal(err)
}
}()

authURL := auth.AuthURL(state)
if err := browser.OpenURL(authURL); err != nil {
log.Fatalf("fail to open: %s\n", authURL)
}

client := <-clientChannel

user, err := client.CurrentUser(context.Background())
if err != nil {
log.Fatalf("fail to get current user: %v\n", err)
}
fmt.Printf("logged in as: %s\n", user.DisplayName)

cmd.Execute()
}

0 comments on commit 3dd704f

Please sign in to comment.