aboutsummaryrefslogtreecommitdiff
path: root/auth/auth.go
diff options
context:
space:
mode:
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.")
}