aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/api/handlers.go
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-07-28 02:12:32 +0900
committernsfisis <nsfisis@gmail.com>2024-07-28 02:12:32 +0900
commit2b9de78cf20faa8d3ae8cd31a8e9d1b7f3ed9aef (patch)
treec381d9479e3542e13512c696ad0859b141f51549 /backend/api/handlers.go
parentab2a49654a7964bf9e6222b17676ca87588b88d8 (diff)
parent7468748c141943fa000edea4098d54bf3cdff55e (diff)
downloadphperkaigi-2025-albatross-2b9de78cf20faa8d3ae8cd31a8e9d1b7f3ed9aef.tar.gz
phperkaigi-2025-albatross-2b9de78cf20faa8d3ae8cd31a8e9d1b7f3ed9aef.tar.zst
phperkaigi-2025-albatross-2b9de78cf20faa8d3ae8cd31a8e9d1b7f3ed9aef.zip
Merge branch 'openapi'
Diffstat (limited to 'backend/api/handlers.go')
-rw-r--r--backend/api/handlers.go62
1 files changed, 62 insertions, 0 deletions
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
+}