[update] use status code for jsonMsg and 401 to unauthorize

This commit is contained in:
Hamidreza Ghavami 2024-03-10 18:28:30 +03:30
parent 1be4335371
commit 81878cda61
No known key found for this signature in database
GPG key ID: 402C6797325182D9
3 changed files with 10 additions and 17 deletions

View file

@ -15,7 +15,7 @@ type BaseController struct{}
func (a *BaseController) checkLogin(c *gin.Context) { func (a *BaseController) checkLogin(c *gin.Context) {
if !session.IsLogin(c) { if !session.IsLogin(c) {
if isAjax(c) { if isAjax(c) {
pureJsonMsg(c, false, I18nWeb(c, "pages.login.loginAgain")) pureJsonMsg(c, http.StatusUnauthorized, false, I18nWeb(c, "pages.login.loginAgain"))
} else { } else {
c.Redirect(http.StatusTemporaryRedirect, c.GetString("base_path")) c.Redirect(http.StatusTemporaryRedirect, c.GetString("base_path"))
} }

View file

@ -50,15 +50,15 @@ func (a *IndexController) login(c *gin.Context) {
var form LoginForm var form LoginForm
err := c.ShouldBind(&form) err := c.ShouldBind(&form)
if err != nil { if err != nil {
pureJsonMsg(c, false, I18nWeb(c, "pages.login.toasts.invalidFormData")) pureJsonMsg(c, http.StatusOK, false, I18nWeb(c, "pages.login.toasts.invalidFormData"))
return return
} }
if form.Username == "" { if form.Username == "" {
pureJsonMsg(c, false, I18nWeb(c, "pages.login.toasts.emptyUsername")) pureJsonMsg(c, http.StatusOK, false, I18nWeb(c, "pages.login.toasts.emptyUsername"))
return return
} }
if form.Password == "" { if form.Password == "" {
pureJsonMsg(c, false, I18nWeb(c, "pages.login.toasts.emptyPassword")) pureJsonMsg(c, http.StatusOK, false, I18nWeb(c, "pages.login.toasts.emptyPassword"))
return return
} }
@ -67,7 +67,7 @@ func (a *IndexController) login(c *gin.Context) {
if user == nil { if user == nil {
logger.Warningf("wrong username or password: \"%s\" \"%s\"", form.Username, form.Password) logger.Warningf("wrong username or password: \"%s\" \"%s\"", form.Username, form.Password)
a.tgbot.UserLoginNotify(form.Username, getRemoteIp(c), timeStr, 0) a.tgbot.UserLoginNotify(form.Username, getRemoteIp(c), timeStr, 0)
pureJsonMsg(c, false, I18nWeb(c, "pages.login.toasts.wrongUsernameOrPassword")) pureJsonMsg(c, http.StatusOK, false, I18nWeb(c, "pages.login.toasts.wrongUsernameOrPassword"))
return return
} else { } else {
logger.Infof("%s login success, Ip Address: %s\n", form.Username, getRemoteIp(c)) logger.Infof("%s login success, Ip Address: %s\n", form.Username, getRemoteIp(c))

View file

@ -49,18 +49,11 @@ func jsonMsgObj(c *gin.Context, msg string, obj interface{}, err error) {
c.JSON(http.StatusOK, m) c.JSON(http.StatusOK, m)
} }
func pureJsonMsg(c *gin.Context, success bool, msg string) { func pureJsonMsg(c *gin.Context, statusCode int, success bool, msg string) {
if success { c.JSON(statusCode, entity.Msg{
c.JSON(http.StatusOK, entity.Msg{ Success: success,
Success: true, Msg: msg,
Msg: msg, })
})
} else {
c.JSON(http.StatusOK, entity.Msg{
Success: false,
Msg: msg,
})
}
} }
func html(c *gin.Context, name string, title string, data gin.H) { func html(c *gin.Context, name string, title string, data gin.H) {