From 6603d3ea5a54647e8eda8ec253835eb7a36e5eb2 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 28 Jul 2024 02:11:02 +0900 Subject: backend: openapi --- backend/api/handlers.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 backend/api/handlers.go (limited to 'backend/api/handlers.go') diff --git a/backend/api/handlers.go b/backend/api/handlers.go new file mode 100644 index 0000000..b8f80f3 --- /dev/null +++ b/backend/api/handlers.go @@ -0,0 +1,62 @@ +package api + +import ( + "context" + "net/http" + + "github.com/labstack/echo/v4" + + "github.com/nsfisis/iosdc-2024-albatross-backend/auth" + "github.com/nsfisis/iosdc-2024-albatross-backend/db" +) + +type ApiHandler struct { + q *db.Queries +} + +func NewHandler(queries *db.Queries) *ApiHandler { + return &ApiHandler{ + q: queries, + } +} + +func (h *ApiHandler) PostApiLogin(ctx context.Context, request PostApiLoginRequestObject) (PostApiLoginResponseObject, error) { + username := request.Body.Username + password := request.Body.Password + userId, err := auth.Login(ctx, h.q, username, password) + if err != nil { + return PostApiLogin401JSONResponse{ + Message: "Invalid username or password", + }, echo.NewHTTPError(http.StatusUnauthorized, "Invalid username or password") + } + + user, err := h.q.GetUserById(ctx, int32(userId)) + if err != nil { + return PostApiLogin401JSONResponse{ + Message: "Invalid username or password", + }, echo.NewHTTPError(http.StatusUnauthorized, "Invalid username or password") + } + + jwt, err := auth.NewJWT(&user) + if err != nil { + // TODO + return PostApiLogin401JSONResponse{ + Message: "Internal Server Error", + }, echo.NewHTTPError(http.StatusInternalServerError, "Internal Server Error") + } + + return PostApiLogin200JSONResponse{ + Token: jwt, + }, nil +} + +func _assertJwtPayloadIsCompatibleWithJWTClaims() { + var c auth.JWTClaims + var p JwtPayload + p.UserId = float32(c.UserID) + p.Username = c.Username + p.DisplayUsername = c.DisplayUsername + p.IconPath = c.IconPath + p.IsAdmin = c.IsAdmin + _ = p +} -- cgit v1.2.3-70-g09d2