aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/api/handlers.go
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-07-28 02:11:02 +0900
committernsfisis <nsfisis@gmail.com>2024-07-28 02:11:02 +0900
commit6603d3ea5a54647e8eda8ec253835eb7a36e5eb2 (patch)
tree3a1e823bf464d5b291c0a1cdf004af72eeb10de1 /backend/api/handlers.go
parentec5fa50956c7f7f7f57c2dde4fb85ad0f3eb441f (diff)
downloadphperkaigi-2025-albatross-6603d3ea5a54647e8eda8ec253835eb7a36e5eb2.tar.gz
phperkaigi-2025-albatross-6603d3ea5a54647e8eda8ec253835eb7a36e5eb2.tar.zst
phperkaigi-2025-albatross-6603d3ea5a54647e8eda8ec253835eb7a36e5eb2.zip
backend: 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
+}