aboutsummaryrefslogtreecommitdiff
path: root/auth/auth.go
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-06-15 16:32:32 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-06-15 16:32:32 +0530
commit1d72b87bca4042d36cea1f1e775803a5252cd224 (patch)
tree04a0185a473c7f9acd5c92bc4091d7c66f102e6b /auth/auth.go
parent83e1ce2508ea035fb299a88fe89e82f34e609499 (diff)
now macli handles logging in by itself
Diffstat (limited to 'auth/auth.go')
-rw-r--r--auth/auth.go29
1 files changed, 27 insertions, 2 deletions
diff --git a/auth/auth.go b/auth/auth.go
index e75d228..a8d28c7 100644
--- a/auth/auth.go
+++ b/auth/auth.go
@@ -21,6 +21,9 @@ package auth
import (
"os"
"os/user"
+ "os/exec"
+ "runtime"
+ "errors"
"fmt"
)
@@ -42,14 +45,36 @@ func Login() {
clientId := askClientId()
challenge := codeChallenge()
link := generateLink(clientId, challenge)
- fmt.Println("Please open this link in the browser:")
- fmt.Println(link)
+
+ openInBrowser(link)
+ listen(clientId, challenge)
}
func generateLink(clientId, challenge string) string {
return "https://myanimelist.net/v1/oauth2/authorize?response_type=code&client_id=" + clientId + "&code_challenge=" + challenge
}
+func openInBrowser(url string) {
+ fmt.Println("Attempting to launch \x1b[36m" + url + "\x1b[0m in your default web browser. If it doesn't launch please manually copy-paste the link.")
+
+ var err error
+ switch runtime.GOOS {
+ case "linux":
+ err = exec.Command("xdg-open", url).Start()
+ case "windows":
+ err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
+ case "darwin":
+ err = exec.Command("open", url).Start()
+ default:
+ err = errors.New("<failed to detect platform>")
+ }
+
+ if err != nil {
+ fmt.Println("There was an error while launching your browser.", err)
+ fmt.Println("Please manually copy and paste the above URL into your web browser.")
+ }
+}
+
func Logout() {
deleteClientId()
deleteToken()