From f4c92504e80ae9b5ce3eed1f005e7b5a03309cd6 Mon Sep 17 00:00:00 2001 From: Yi-Ting Shih Date: Sun, 7 Dec 2025 18:56:31 +0800 Subject: [PATCH] Fix: login flow --- cmds/serve.go | 2 +- handlers/api/getAliases.go | 26 +++++++++++++++++++---- handlers/api/getImages.go | 15 +++++++++++++ handlers/api/handlers.go | 7 ++++-- handlers/auth/postGenLoginUrl.go | 2 +- implements/bunDatabase.go | 13 ++++++++++++ interfaces/database.go | 4 ++++ tests/{login_test.go => 01_login_test.go} | 8 ++++--- tests/02_getImages_test.go | 6 ++++++ 9 files changed, 72 insertions(+), 11 deletions(-) rename tests/{login_test.go => 01_login_test.go} (88%) create mode 100644 tests/02_getImages_test.go diff --git a/cmds/serve.go b/cmds/serve.go index e199dc8..aec0aa4 100644 --- a/cmds/serve.go +++ b/cmds/serve.go @@ -88,7 +88,7 @@ var serveCmd = &cobra.Command{ // Initialize handlers midHandlers := middlewares.NewHandlers(db) - apis := api.NewHandlers() + apis := api.NewHandlers(db) auths := auth.NewHandlers(db) // Initialize backend router diff --git a/handlers/api/getAliases.go b/handlers/api/getAliases.go index 15742af..5ef139f 100644 --- a/handlers/api/getAliases.go +++ b/handlers/api/getAliases.go @@ -1,9 +1,9 @@ package api import ( - "fmt" "net/http" + "gitea.konchin.com/go2025/backend/middlewares" "github.com/uptrace/bunrouter" ) @@ -24,7 +24,25 @@ func (self *Handlers) GetAliases( w http.ResponseWriter, req bunrouter.Request, ) error { // mock output - fmt.Fprintf(w, "%s", `[{"id": 1, "name": "yajiu"}]`) - return nil - // return utils.Success(w) + ctx := req.Context() + + aliases, err := self.db.GetAliases(ctx) + if err != nil { + return middlewares.HTTPError{ + StatusCode: http.StatusInternalServerError, + Message: "failed to get aliases", + OriginError: err, + } + } + + var output []getAliasesOutputAlias + + for _, alias := range aliases { + output = append(output, getAliasesOutputAlias{ + Id: alias.Id, + Name: alias.Name, + }) + } + + return bunrouter.JSON(w, output) } diff --git a/handlers/api/getImages.go b/handlers/api/getImages.go index 9f4ef03..adfd4ed 100644 --- a/handlers/api/getImages.go +++ b/handlers/api/getImages.go @@ -2,7 +2,9 @@ package api import ( "net/http" + "strings" + "gitea.konchin.com/go2025/backend/middlewares" "gitea.konchin.com/go2025/backend/utils" "github.com/uptrace/bunrouter" ) @@ -25,5 +27,18 @@ type getImagesOutputImage struct { func (self *Handlers) GetImages( w http.ResponseWriter, req bunrouter.Request, ) error { + // ctx := req.Context() + + images := strings.Split(req.Param("images"), ",") + aliases := strings.Split(req.Param("aliases"), ",") + + if (len(images) == 0 && len(aliases) == 0) || + (len(images) > 0 && len(aliases) > 0) { + return middlewares.HTTPError{ + StatusCode: http.StatusBadRequest, + Message: "images and aliases should exist exactly one", + } + } + return utils.Success(w) } diff --git a/handlers/api/handlers.go b/handlers/api/handlers.go index 36dc09c..ead77c8 100644 --- a/handlers/api/handlers.go +++ b/handlers/api/handlers.go @@ -1,8 +1,11 @@ package api +import "gitea.konchin.com/go2025/backend/interfaces" + type Handlers struct { + db interfaces.Database } -func NewHandlers() *Handlers { - return &Handlers{} +func NewHandlers(db interfaces.Database) *Handlers { + return &Handlers{db: db} } diff --git a/handlers/auth/postGenLoginUrl.go b/handlers/auth/postGenLoginUrl.go index 0a82a15..abc85de 100644 --- a/handlers/auth/postGenLoginUrl.go +++ b/handlers/auth/postGenLoginUrl.go @@ -58,7 +58,7 @@ func (self *Handlers) PostGenLoginUrl( return bunrouter.JSON(w, postGenLoginUrlOutput{ LoginUrl: viper.GetString("external-url") + - "/auth/login?" + + "/login?" + "token=" + token, }) } diff --git a/implements/bunDatabase.go b/implements/bunDatabase.go index 107079c..545b2d8 100644 --- a/implements/bunDatabase.go +++ b/implements/bunDatabase.go @@ -91,3 +91,16 @@ func (self *BunDatabase) UpsertLoginToken( } return token, nil } + +func (self *BunDatabase) GetAliases( + ctx context.Context, +) ([]models.Alias, error) { + var ret []models.Alias + err := self.db.NewSelect(). + Model(&ret). + Scan(ctx) + if err != nil { + return []models.Alias{}, err + } + return ret, nil +} diff --git a/interfaces/database.go b/interfaces/database.go index 77b9dd3..bab9baa 100644 --- a/interfaces/database.go +++ b/interfaces/database.go @@ -21,4 +21,8 @@ type Database interface { ctx context.Context, userId string, ) (string, error) + + GetAliases( + ctx context.Context, + ) ([]models.Alias, error) } diff --git a/tests/login_test.go b/tests/01_login_test.go similarity index 88% rename from tests/login_test.go rename to tests/01_login_test.go index e174b2b..50d756f 100644 --- a/tests/login_test.go +++ b/tests/01_login_test.go @@ -8,6 +8,8 @@ import ( "github.com/go-resty/resty/v2" ) +var client *resty.Client + type genLoginUrlPayload struct { LoginUrl string `json:"loginUrl"` } @@ -16,8 +18,8 @@ type loginPayload struct { Token string `json:"token"` } -func TestLogin(t *testing.T) { - client := resty.New() +func Test_01_Login(t *testing.T) { + client = resty.New() var payload genLoginUrlPayload resp, err := client.R(). @@ -37,7 +39,7 @@ func TestLogin(t *testing.T) { resp, err = client.R(). SetBody(loginPayload{Token: loginUrl.Query().Get("token")}). - Post(loginUrl.Scheme + "://" + loginUrl.Host + loginUrl.Path) + Post(loginUrl.Scheme + "://" + loginUrl.Host + "/auth/login") if err != nil || resp.StatusCode() != http.StatusOK { t.Fatal("failed to login") } diff --git a/tests/02_getImages_test.go b/tests/02_getImages_test.go new file mode 100644 index 0000000..a3effec --- /dev/null +++ b/tests/02_getImages_test.go @@ -0,0 +1,6 @@ +package main + +import "testing" + +func Test_02_GetImages(t *testing.T) { +}