diff --git a/auth/auth.gen.go b/auth/auth.gen.go index b7cd839..7276545 100644 --- a/auth/auth.gen.go +++ b/auth/auth.gen.go @@ -13,32 +13,32 @@ import ( strictgin "github.com/oapi-codegen/runtime/strictmiddleware/gin" ) -// PostSignInJSONBody defines parameters for PostSignIn. -type PostSignInJSONBody struct { +// PostAuthSignInJSONBody defines parameters for PostAuthSignIn. +type PostAuthSignInJSONBody struct { Nickname string `json:"nickname"` Pass string `json:"pass"` } -// PostSignUpJSONBody defines parameters for PostSignUp. -type PostSignUpJSONBody struct { +// PostAuthSignUpJSONBody defines parameters for PostAuthSignUp. +type PostAuthSignUpJSONBody struct { Nickname string `json:"nickname"` Pass string `json:"pass"` } -// PostSignInJSONRequestBody defines body for PostSignIn for application/json ContentType. -type PostSignInJSONRequestBody PostSignInJSONBody +// PostAuthSignInJSONRequestBody defines body for PostAuthSignIn for application/json ContentType. +type PostAuthSignInJSONRequestBody PostAuthSignInJSONBody -// PostSignUpJSONRequestBody defines body for PostSignUp for application/json ContentType. -type PostSignUpJSONRequestBody PostSignUpJSONBody +// PostAuthSignUpJSONRequestBody defines body for PostAuthSignUp for application/json ContentType. +type PostAuthSignUpJSONRequestBody PostAuthSignUpJSONBody // ServerInterface represents all server handlers. type ServerInterface interface { // Sign in a user and return JWT - // (POST /sign-in) - PostSignIn(c *gin.Context) + // (POST /auth/sign-in) + PostAuthSignIn(c *gin.Context) // Sign up a new user - // (POST /sign-up) - PostSignUp(c *gin.Context) + // (POST /auth/sign-up) + PostAuthSignUp(c *gin.Context) } // ServerInterfaceWrapper converts contexts to parameters. @@ -50,8 +50,8 @@ type ServerInterfaceWrapper struct { type MiddlewareFunc func(c *gin.Context) -// PostSignIn operation middleware -func (siw *ServerInterfaceWrapper) PostSignIn(c *gin.Context) { +// PostAuthSignIn operation middleware +func (siw *ServerInterfaceWrapper) PostAuthSignIn(c *gin.Context) { for _, middleware := range siw.HandlerMiddlewares { middleware(c) @@ -60,11 +60,11 @@ func (siw *ServerInterfaceWrapper) PostSignIn(c *gin.Context) { } } - siw.Handler.PostSignIn(c) + siw.Handler.PostAuthSignIn(c) } -// PostSignUp operation middleware -func (siw *ServerInterfaceWrapper) PostSignUp(c *gin.Context) { +// PostAuthSignUp operation middleware +func (siw *ServerInterfaceWrapper) PostAuthSignUp(c *gin.Context) { for _, middleware := range siw.HandlerMiddlewares { middleware(c) @@ -73,7 +73,7 @@ func (siw *ServerInterfaceWrapper) PostSignUp(c *gin.Context) { } } - siw.Handler.PostSignUp(c) + siw.Handler.PostAuthSignUp(c) } // GinServerOptions provides options for the Gin server. @@ -103,54 +103,54 @@ func RegisterHandlersWithOptions(router gin.IRouter, si ServerInterface, options ErrorHandler: errorHandler, } - router.POST(options.BaseURL+"/sign-in", wrapper.PostSignIn) - router.POST(options.BaseURL+"/sign-up", wrapper.PostSignUp) + router.POST(options.BaseURL+"/auth/sign-in", wrapper.PostAuthSignIn) + router.POST(options.BaseURL+"/auth/sign-up", wrapper.PostAuthSignUp) } -type PostSignInRequestObject struct { - Body *PostSignInJSONRequestBody +type PostAuthSignInRequestObject struct { + Body *PostAuthSignInJSONRequestBody } -type PostSignInResponseObject interface { - VisitPostSignInResponse(w http.ResponseWriter) error +type PostAuthSignInResponseObject interface { + VisitPostAuthSignInResponse(w http.ResponseWriter) error } -type PostSignIn200JSONResponse struct { +type PostAuthSignIn200JSONResponse struct { UserId int64 `json:"user_id"` UserName string `json:"user_name"` } -func (response PostSignIn200JSONResponse) VisitPostSignInResponse(w http.ResponseWriter) error { +func (response PostAuthSignIn200JSONResponse) VisitPostAuthSignInResponse(w http.ResponseWriter) error { w.Header().Set("Content-Type", "application/json") w.WriteHeader(200) return json.NewEncoder(w).Encode(response) } -type PostSignIn401JSONResponse struct { +type PostAuthSignIn401JSONResponse struct { Error *string `json:"error,omitempty"` } -func (response PostSignIn401JSONResponse) VisitPostSignInResponse(w http.ResponseWriter) error { +func (response PostAuthSignIn401JSONResponse) VisitPostAuthSignInResponse(w http.ResponseWriter) error { w.Header().Set("Content-Type", "application/json") w.WriteHeader(401) return json.NewEncoder(w).Encode(response) } -type PostSignUpRequestObject struct { - Body *PostSignUpJSONRequestBody +type PostAuthSignUpRequestObject struct { + Body *PostAuthSignUpJSONRequestBody } -type PostSignUpResponseObject interface { - VisitPostSignUpResponse(w http.ResponseWriter) error +type PostAuthSignUpResponseObject interface { + VisitPostAuthSignUpResponse(w http.ResponseWriter) error } -type PostSignUp200JSONResponse struct { +type PostAuthSignUp200JSONResponse struct { UserId int64 `json:"user_id"` } -func (response PostSignUp200JSONResponse) VisitPostSignUpResponse(w http.ResponseWriter) error { +func (response PostAuthSignUp200JSONResponse) VisitPostAuthSignUpResponse(w http.ResponseWriter) error { w.Header().Set("Content-Type", "application/json") w.WriteHeader(200) @@ -160,11 +160,11 @@ func (response PostSignUp200JSONResponse) VisitPostSignUpResponse(w http.Respons // StrictServerInterface represents all server handlers. type StrictServerInterface interface { // Sign in a user and return JWT - // (POST /sign-in) - PostSignIn(ctx context.Context, request PostSignInRequestObject) (PostSignInResponseObject, error) + // (POST /auth/sign-in) + PostAuthSignIn(ctx context.Context, request PostAuthSignInRequestObject) (PostAuthSignInResponseObject, error) // Sign up a new user - // (POST /sign-up) - PostSignUp(ctx context.Context, request PostSignUpRequestObject) (PostSignUpResponseObject, error) + // (POST /auth/sign-up) + PostAuthSignUp(ctx context.Context, request PostAuthSignUpRequestObject) (PostAuthSignUpResponseObject, error) } type StrictHandlerFunc = strictgin.StrictGinHandlerFunc @@ -179,11 +179,11 @@ type strictHandler struct { middlewares []StrictMiddlewareFunc } -// PostSignIn operation middleware -func (sh *strictHandler) PostSignIn(ctx *gin.Context) { - var request PostSignInRequestObject +// PostAuthSignIn operation middleware +func (sh *strictHandler) PostAuthSignIn(ctx *gin.Context) { + var request PostAuthSignInRequestObject - var body PostSignInJSONRequestBody + var body PostAuthSignInJSONRequestBody if err := ctx.ShouldBindJSON(&body); err != nil { ctx.Status(http.StatusBadRequest) ctx.Error(err) @@ -192,10 +192,10 @@ func (sh *strictHandler) PostSignIn(ctx *gin.Context) { request.Body = &body handler := func(ctx *gin.Context, request interface{}) (interface{}, error) { - return sh.ssi.PostSignIn(ctx, request.(PostSignInRequestObject)) + return sh.ssi.PostAuthSignIn(ctx, request.(PostAuthSignInRequestObject)) } for _, middleware := range sh.middlewares { - handler = middleware(handler, "PostSignIn") + handler = middleware(handler, "PostAuthSignIn") } response, err := handler(ctx, request) @@ -203,8 +203,8 @@ func (sh *strictHandler) PostSignIn(ctx *gin.Context) { if err != nil { ctx.Error(err) ctx.Status(http.StatusInternalServerError) - } else if validResponse, ok := response.(PostSignInResponseObject); ok { - if err := validResponse.VisitPostSignInResponse(ctx.Writer); err != nil { + } else if validResponse, ok := response.(PostAuthSignInResponseObject); ok { + if err := validResponse.VisitPostAuthSignInResponse(ctx.Writer); err != nil { ctx.Error(err) } } else if response != nil { @@ -212,11 +212,11 @@ func (sh *strictHandler) PostSignIn(ctx *gin.Context) { } } -// PostSignUp operation middleware -func (sh *strictHandler) PostSignUp(ctx *gin.Context) { - var request PostSignUpRequestObject +// PostAuthSignUp operation middleware +func (sh *strictHandler) PostAuthSignUp(ctx *gin.Context) { + var request PostAuthSignUpRequestObject - var body PostSignUpJSONRequestBody + var body PostAuthSignUpJSONRequestBody if err := ctx.ShouldBindJSON(&body); err != nil { ctx.Status(http.StatusBadRequest) ctx.Error(err) @@ -225,10 +225,10 @@ func (sh *strictHandler) PostSignUp(ctx *gin.Context) { request.Body = &body handler := func(ctx *gin.Context, request interface{}) (interface{}, error) { - return sh.ssi.PostSignUp(ctx, request.(PostSignUpRequestObject)) + return sh.ssi.PostAuthSignUp(ctx, request.(PostAuthSignUpRequestObject)) } for _, middleware := range sh.middlewares { - handler = middleware(handler, "PostSignUp") + handler = middleware(handler, "PostAuthSignUp") } response, err := handler(ctx, request) @@ -236,8 +236,8 @@ func (sh *strictHandler) PostSignUp(ctx *gin.Context) { if err != nil { ctx.Error(err) ctx.Status(http.StatusInternalServerError) - } else if validResponse, ok := response.(PostSignUpResponseObject); ok { - if err := validResponse.VisitPostSignUpResponse(ctx.Writer); err != nil { + } else if validResponse, ok := response.(PostAuthSignUpResponseObject); ok { + if err := validResponse.VisitPostAuthSignUpResponse(ctx.Writer); err != nil { ctx.Error(err) } } else if response != nil { diff --git a/modules/auth/handlers/handlers.go b/modules/auth/handlers/handlers.go index 09907bc..6fee512 100644 --- a/modules/auth/handlers/handlers.go +++ b/modules/auth/handlers/handlers.go @@ -78,7 +78,7 @@ func (s Server) generateTokens(userID string) (accessToken string, refreshToken return accessToken, refreshToken, csrfToken, nil } -func (s Server) PostSignUp(ctx context.Context, req auth.PostSignUpRequestObject) (auth.PostSignUpResponseObject, error) { +func (s Server) PostAuthSignUp(ctx context.Context, req auth.PostAuthSignUpRequestObject) (auth.PostAuthSignUpResponseObject, error) { passhash, err := HashPassword(req.Body.Pass) if err != nil { log.Errorf("failed to hash password: %v", err) @@ -94,17 +94,17 @@ func (s Server) PostSignUp(ctx context.Context, req auth.PostSignUpRequestObject // TODO: check err and retyrn 400/500 } - return auth.PostSignUp200JSONResponse{ + return auth.PostAuthSignUp200JSONResponse{ UserId: user_id, }, nil } -func (s Server) PostSignIn(ctx context.Context, req auth.PostSignInRequestObject) (auth.PostSignInResponseObject, error) { +func (s Server) PostAuthSignIn(ctx context.Context, req auth.PostAuthSignInRequestObject) (auth.PostAuthSignInResponseObject, error) { ginCtx, ok := ctx.Value(gin.ContextKey).(*gin.Context) if !ok { log.Print("failed to get gin context") // TODO: change to 500 - return auth.PostSignIn200JSONResponse{}, fmt.Errorf("failed to get gin.Context from context.Context") + return auth.PostAuthSignIn200JSONResponse{}, fmt.Errorf("failed to get gin.Context from context.Context") } user, err := s.db.GetUserByNickname(context.Background(), req.Body.Nickname) @@ -120,7 +120,7 @@ func (s Server) PostSignIn(ctx context.Context, req auth.PostSignInRequestObject } if !ok { err_msg := "invalid credentials" - return auth.PostSignIn401JSONResponse{ + return auth.PostAuthSignIn401JSONResponse{ Error: &err_msg, }, nil } @@ -137,7 +137,7 @@ func (s Server) PostSignIn(ctx context.Context, req auth.PostSignInRequestObject ginCtx.SetCookie("refresh_token", refreshToken, 1209600, "/auth", "", false, true) ginCtx.SetCookie("xsrf_token", csrfToken, 1209600, "/api", "", false, false) - result := auth.PostSignIn200JSONResponse{ + result := auth.PostAuthSignIn200JSONResponse{ UserId: user.ID, UserName: user.Nickname, }