70 lines
1.5 KiB
Go
70 lines
1.5 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/astaxie/beego/validation"
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"message-nest/pkg/app"
|
|
"message-nest/pkg/e"
|
|
"message-nest/pkg/util"
|
|
"message-nest/service/auth_service"
|
|
)
|
|
|
|
type auth struct {
|
|
Username string `valid:"Required; MaxSize(50)"`
|
|
Password string `valid:"Required; MaxSize(50)"`
|
|
}
|
|
|
|
type ReqAuth struct {
|
|
Username string `json:"username"`
|
|
Password string `json:"passwd"`
|
|
}
|
|
|
|
func GetAuth(c *gin.Context) {
|
|
appG := app.Gin{C: c}
|
|
valid := validation.Validation{}
|
|
|
|
var user ReqAuth
|
|
err := c.ShouldBindJSON(&user)
|
|
if err != nil {
|
|
app.MarkErrors(valid.Errors)
|
|
appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
|
|
return
|
|
}
|
|
username := user.Username
|
|
password := user.Password
|
|
|
|
a := auth{Username: username, Password: password}
|
|
ok, _ := valid.Valid(&a)
|
|
|
|
if !ok {
|
|
app.MarkErrors(valid.Errors)
|
|
appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
|
|
return
|
|
}
|
|
|
|
authService := auth_service.Auth{Username: username, Password: password}
|
|
isExist, err := authService.Check()
|
|
if err != nil {
|
|
appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_CHECK_TOKEN_FAIL, nil)
|
|
return
|
|
}
|
|
|
|
if !isExist {
|
|
appG.Response(http.StatusUnauthorized, e.ERROR_AUTH, nil)
|
|
return
|
|
}
|
|
|
|
token, err := util.GenerateToken(username, password)
|
|
if err != nil {
|
|
appG.Response(http.StatusInternalServerError, e.ERROR_AUTH_TOKEN, nil)
|
|
return
|
|
}
|
|
|
|
appG.Response(http.StatusOK, e.SUCCESS, map[string]string{
|
|
"token": token,
|
|
})
|
|
}
|