46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
|
package v1
|
||
|
|
||
|
import (
|
||
|
"github.com/labstack/echo/v4"
|
||
|
"go-template/internal/handler/http/response"
|
||
|
"go-template/internal/schema"
|
||
|
"go-template/internal/service/auth"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
type UserController struct {
|
||
|
authService *auth.Service
|
||
|
}
|
||
|
|
||
|
func NewUserController(authService *auth.Service) *UserController {
|
||
|
return &UserController{authService}
|
||
|
}
|
||
|
|
||
|
// Test godoc
|
||
|
// @Summary Greet
|
||
|
// @Description 测试接口,将会返回当前用户的信息
|
||
|
// @Tags ping
|
||
|
// @Accept json
|
||
|
// @Produce json
|
||
|
// @Security ApiKeyAuth
|
||
|
// @deprecated true
|
||
|
// @Success 200 {object} schema.ResponseBody{data=schema.CurrentUserResponse}
|
||
|
// @Failure 400 {object} schema.ResponseBody
|
||
|
// @Router /api/v1/ping [get]
|
||
|
func (u *UserController) Test(c echo.Context) error {
|
||
|
user, ok := u.authService.GetUser(c)
|
||
|
if !ok {
|
||
|
return response.Ctx(c).Status(http.StatusUnauthorized).Send()
|
||
|
}
|
||
|
|
||
|
var currentUserResponse = &schema.CurrentUserResponse{
|
||
|
IP: c.Request().RemoteAddr,
|
||
|
Valid: user.Valid,
|
||
|
UserEmail: user.Token.Email,
|
||
|
UserId: user.Token.Sub,
|
||
|
UserName: user.Token.Name,
|
||
|
}
|
||
|
|
||
|
return response.Ctx(c).Data(currentUserResponse).Send()
|
||
|
}
|