49 lines
898 B
Go
Executable File
49 lines
898 B
Go
Executable File
package controller
|
|
|
|
import (
|
|
"atom/modules/auth/dto"
|
|
"atom/modules/auth/service"
|
|
"atom/providers/config"
|
|
"atom/providers/jwt"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type UserController interface {
|
|
Login(*gin.Context, dto.LoginRequestForm) (*dto.LoginResponse, error)
|
|
}
|
|
|
|
type userControllerImpl struct {
|
|
conf *config.Config
|
|
user service.UserService
|
|
jwt *jwt.JWT
|
|
}
|
|
|
|
func NewUserController(
|
|
conf *config.Config,
|
|
user service.UserService,
|
|
jwt *jwt.JWT,
|
|
) UserController {
|
|
return &userControllerImpl{
|
|
conf: conf,
|
|
user: user,
|
|
jwt: jwt,
|
|
}
|
|
}
|
|
|
|
func (c *userControllerImpl) Login(ctx *gin.Context, req dto.LoginRequestForm) (*dto.LoginResponse, error) {
|
|
user, err := c.user.AuthMatchPassword(ctx, &req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
token, err := c.user.GenerateJWTTokenFromUser(ctx, user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &dto.LoginResponse{
|
|
Token: token,
|
|
}, nil
|
|
}
|