aboutsummaryrefslogtreecommitdiff
path: root/auth
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-06-16 13:04:37 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-06-16 13:04:37 +0530
commit0c0f4f31f291fd266f86384da4af1a3755c6e588 (patch)
tree0490bfce9f732ca2f6f8b0fc3f5ad2e1f019c982 /auth
parent1d72b87bca4042d36cea1f1e775803a5252cd224 (diff)
asking to delte client id on logout now. useful if user wants to login agian
Diffstat (limited to 'auth')
-rw-r--r--auth/auth.go9
-rw-r--r--auth/input.go40
-rw-r--r--auth/server.go28
3 files changed, 57 insertions, 20 deletions
diff --git a/auth/auth.go b/auth/auth.go
index a8d28c7..95ccd48 100644
--- a/auth/auth.go
+++ b/auth/auth.go
@@ -70,12 +70,17 @@ func openInBrowser(url string) {
}
if err != nil {
- fmt.Println("There was an error while launching your browser.", err)
+ fmt.Println("There was an error while launching your browser.")
fmt.Println("Please manually copy and paste the above URL into your web browser.")
+ fmt.Println(err)
}
}
func Logout() {
- deleteClientId()
deleteToken()
+ fmt.Println("Deleted user credentials.")
+ if confirmInput("Delete your Client ID? [y/N] ") {
+ fmt.Println("Deleting Client ID...")
+ deleteClientId()
+ }
}
diff --git a/auth/input.go b/auth/input.go
index 9f75fab..aa56f52 100644
--- a/auth/input.go
+++ b/auth/input.go
@@ -22,6 +22,7 @@ import (
"os"
"fmt"
"errors"
+ "strings"
p "github.com/manifoldco/promptui"
)
@@ -55,3 +56,42 @@ func secretInput(label, errMessage string) string {
return res
}
+
+// ask yes/no with no as default
+func confirmInput(label string) bool {
+ validResponses := []string{"y", "yes", "n", "no", ""}
+
+ validate := func(input string) error {
+ lowerInput := strings.ToLower(input)
+ for _, i := range validResponses {
+ if lowerInput == i {
+ return nil
+ }
+ }
+ return errors.New("answer can only be y(es) or n(o)")
+ }
+
+ template := &p.PromptTemplates {
+ Valid: "{{ . | cyan }}",
+ Invalid: "{{ . | cyan }}",
+ Success: "{{ . | blue }}",
+ }
+
+ prompt := p.Prompt {
+ Label: label,
+ Templates: template,
+ Validate: validate,
+ }
+
+ res, err := prompt.Run()
+ if err != nil {
+ fmt.Println("Failed to run confirm prompt.", err.Error(), err)
+ os.Exit(1)
+ }
+
+ if res == "y" || res == "yes" {
+ return true
+ }
+
+ return false
+}
diff --git a/auth/server.go b/auth/server.go
index 5a18a36..02b9382 100644
--- a/auth/server.go
+++ b/auth/server.go
@@ -29,25 +29,17 @@ import (
func listen(clientId, verifier string) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
- query := r.URL.Query()
+ code, codeExists := r.URL.Query()["code"]
- code, codePresent := query["code"]
- if !codePresent {
- // TODO: check if error message present
- fmt.Println("Error: response from MyAnimeList doesn't contain required code.")
- os.Exit(1)
- }
-
- accessToken, refreshToken, expiresIn := requestToken(clientId, verifier, code[0])
-
- if accessToken != "" {
- w.WriteHeader(200)
- w.Write([]byte("<h1>You have successfully logged into macli.</h1>"))
- }
-
- setToken(accessToken)
- setRefreshToken(refreshToken)
- setExpiresIn(expiresIn)
+ if codeExists {
+ accessToken, refreshToken, expiresIn := requestToken(clientId, verifier, code[0])
+ setToken(accessToken)
+ setRefreshToken(refreshToken)
+ setExpiresIn(expiresIn)
+ fmt.Println("\x1b[32mYou have successfully logged into macli.\x1b[0m")
+ fmt.Println("\x1b[32mYou can close the web browser tab now.\x1b[0m")
+ os.Exit(0)
+ }
})
err := http.ListenAndServe(":8000", nil)