| package api
|
|
|
| import (
|
| "errors"
|
| "net/http"
|
|
|
| "github.com/gin-gonic/gin"
|
| "go.uber.org/fx"
|
|
|
| "github.com/looplj/axonhub/internal/objects"
|
| "github.com/looplj/axonhub/internal/server/biz"
|
| )
|
|
|
| type AuthHandlersParams struct {
|
| fx.In
|
|
|
| AuthService *biz.AuthService
|
| }
|
|
|
| func NewAuthHandlers(params AuthHandlersParams) *AuthHandlers {
|
| return &AuthHandlers{
|
| AuthService: params.AuthService,
|
| }
|
| }
|
|
|
| type AuthHandlers struct {
|
| AuthService *biz.AuthService
|
| }
|
|
|
|
|
| type SignInRequest struct {
|
| Email string `json:"email" binding:"required,email"`
|
| Password string `json:"password" binding:"required"`
|
| }
|
|
|
|
|
| type SignInResponse struct {
|
| User *objects.UserInfo `json:"user"`
|
| Token string `json:"token"`
|
| }
|
|
|
|
|
| func (h *AuthHandlers) SignIn(c *gin.Context) {
|
| var (
|
| ctx = c.Request.Context()
|
| req SignInRequest
|
| )
|
|
|
| err := c.ShouldBindJSON(&req)
|
| if err != nil {
|
| JSONError(c, http.StatusBadRequest, errors.New("Invalid request format"))
|
| return
|
| }
|
|
|
|
|
| user, err := h.AuthService.AuthenticateUser(ctx, req.Email, req.Password)
|
| if err != nil {
|
| if errors.Is(err, biz.ErrInvalidPassword) {
|
| JSONError(c, http.StatusUnauthorized, errors.New("Invalid email or password"))
|
| return
|
| }
|
|
|
| JSONError(c, http.StatusInternalServerError, errors.New("Internal server error"))
|
|
|
| return
|
| }
|
|
|
|
|
| token, err := h.AuthService.GenerateJWTToken(ctx, user)
|
| if err != nil {
|
| JSONError(c, http.StatusInternalServerError, errors.New("Internal server error"))
|
| return
|
| }
|
|
|
| response := SignInResponse{
|
| User: biz.ConvertUserToUserInfo(ctx, user),
|
| Token: token,
|
| }
|
|
|
| c.JSON(http.StatusOK, response)
|
| }
|
|
|