Files
crawlab/core/controllers/login.go
Marvin Zhang 23cad00d92 refactor: streamline controller methods and enhance parameter handling
- Refactored multiple controller methods to accept structured parameters for improved clarity and maintainability.
- Consolidated error handling and response generation across various endpoints.
- Updated function signatures to eliminate unnecessary context parameters and enhance type safety.
- Improved consistency in response formatting and error handling across controllers.
- Enhanced file handling methods to support multipart file uploads and directory operations more effectively.
2025-03-13 17:37:30 +08:00

33 lines
893 B
Go

package controllers
import (
"github.com/crawlab-team/crawlab/core/constants"
"github.com/crawlab-team/crawlab/core/errors"
"github.com/crawlab-team/crawlab/core/user"
"github.com/gin-gonic/gin"
)
type PostLoginParams struct {
Username string `json:"username" validate:"required"`
Password string `json:"password" validate:"required"`
}
func PostLogin(c *gin.Context, params *PostLoginParams) (response *Response[string], err error) {
userSvc, err := user.GetUserService()
if err != nil {
return GetErrorResponse[string](err)
}
token, loggedInUser, err := userSvc.Login(params.Username, params.Password)
if err != nil {
return GetErrorResponse[string](errors.ErrorUserUnauthorized)
}
c.Set(constants.UserContextKey, loggedInUser)
return GetDataResponse(token)
}
func PostLogout(_ *gin.Context) (response *Response[any], err error) {
return GetDataResponse[any](nil)
}