aboutsummaryrefslogtreecommitdiff
path: root/auth/auth.go
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.com>2024-05-20 18:44:28 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.com>2024-05-20 18:44:28 +0530
commit775b8a4a1c8b73a38ecc7e75495361a6e9d74be1 (patch)
treee5bb74bc4fd18b390cfea0e0560225007e3c1701 /auth/auth.go
parentfab5bf455c92c371c3a89fbbf18b978cde70eb03 (diff)
using the maltoken package to log in nowmaltoken
Diffstat (limited to 'auth/auth.go')
-rw-r--r--auth/auth.go22
1 files changed, 13 insertions, 9 deletions
diff --git a/auth/auth.go b/auth/auth.go
index b75c392..9374c72 100644
--- a/auth/auth.go
+++ b/auth/auth.go
@@ -1,6 +1,6 @@
/*
macli - Unofficial CLI-Based MyAnimeList Client
-Copyright © 2022 Vidhu Kant Sharma <vidhukant@vidhukant.xyz>
+Copyright © 2022-2024 Vidhu Kant Sharma <vidhukant@vidhukant.xyz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package auth
import (
+ mt "vidhukant.com/maltoken"
"errors"
"fmt"
"github.com/spf13/viper"
@@ -42,6 +43,10 @@ func init() {
os.Exit(1)
}
userName = currentUser.Username
+
+ mt.SuccessHTML = fmt.Sprintf(HTMLTemplate, "Login successful.", "Thanks for using macli. You may close this tab now.")
+ mt.BadRequestHTML = fmt.Sprintf(HTMLTemplate, "Invalid request.", "Required query parameters are missing.")
+ mt.ErrHTML = fmt.Sprintf(HTMLTemplate, "An error occoured.", "%s")
}
// asks for all the details
@@ -83,15 +88,16 @@ func Login(tk, clientId string, storeClientId bool) {
return
}
- challenge := codeChallenge()
- link := generateLink(clientId, challenge)
+ challenge, link := mt.GetChallengeLink(clientId)
openInBrowser(link)
- listen(clientId, challenge)
-}
+ res, err := mt.Listen(clientId, challenge, 8000)
+ if err != nil {
+ fmt.Printf("\x1b[1;31mAn error occoured:\x1b[0m %s\nExiting...\n", err.Error())
+ os.Exit(1)
+ }
-func generateLink(clientId, challenge string) string {
- return "https://myanimelist.net/v1/oauth2/authorize?response_type=code&client_id=" + clientId + "&code_challenge=" + challenge
+ setToken(res["access_token"].(string))
}
func openInBrowser(url string) {
@@ -119,8 +125,6 @@ func openInBrowser(url string) {
func Logout() {
existingToken, _ := keyring.Get(serviceName, userName)
deleteToken()
- deleteExpiresIn()
- deleteRefreshToken()
if existingToken != "" {
fmt.Println("Deleted user credentials.")
}