electricity_bill_calc_service/controller/user.go

53 lines
1.2 KiB
Go

package controller
import (
"electricity_bill_calc/exceptions"
"electricity_bill_calc/model"
"electricity_bill_calc/response"
"electricity_bill_calc/service"
"errors"
"net/http"
"github.com/gin-gonic/gin"
)
type _UserController struct{}
var UserController _UserController
type LoginFormData struct {
Username string `form:"uname"`
Password string `form:"upass"`
Type int8 `form:"type"`
}
func (_UserController) Login(c *gin.Context) {
result := response.NewResult(c)
loginData := new(LoginFormData)
c.BindJSON(loginData)
var (
session *model.Session
err error
)
if loginData.Type == 0 {
session, err = service.UserService.ProcessEnterpriseUserLogin(loginData.Username, loginData.Password)
} else {
session, err = service.UserService.ProcessManagementUserLogin(loginData.Username, loginData.Password)
}
if err != nil {
if errors.Is(err, &exceptions.AuthenticationError{}) {
authError := err.(exceptions.AuthenticationError)
if authError.NeedReset {
result.LoginNeedReset()
return
}
result.Error(int(authError.Code), authError.Message)
return
} else {
result.Error(http.StatusInternalServerError, err.Error())
return
}
}
result.LoginSuccess(session, false)
}