Feat: works on my machine
This commit is contained in:
59
handlers/auth/postLogin.go
Normal file
59
handlers/auth/postLogin.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"gitea.konchin.com/ytshih/inp2025/middlewares"
|
||||
"gitea.konchin.com/ytshih/inp2025/models"
|
||||
"gitea.konchin.com/ytshih/inp2025/types"
|
||||
"gitea.konchin.com/ytshih/inp2025/utils"
|
||||
|
||||
"github.com/uptrace/bunrouter"
|
||||
)
|
||||
|
||||
func (self *Handlers) PostLogin(
|
||||
w http.ResponseWriter,
|
||||
req bunrouter.Request,
|
||||
) error {
|
||||
ctx := req.Context()
|
||||
user, ok := ctx.Value(types.UserKey).(models.User)
|
||||
if !ok {
|
||||
return middlewares.HTTPError{
|
||||
StatusCode: http.StatusUnauthorized,
|
||||
Message: "user not login",
|
||||
}
|
||||
}
|
||||
|
||||
res, err := self.db.NewUpdate().
|
||||
Model((*models.User)(nil)).
|
||||
Set("login_count = login_count + ?", 1).
|
||||
Set("is_logged = ?", true).
|
||||
Where("is_logged = ?", false).
|
||||
Where("username = ?", user.Username).
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return middlewares.HTTPError{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Message: "failed to update login count",
|
||||
OriginError: err,
|
||||
}
|
||||
}
|
||||
if cnt, err := res.RowsAffected(); err != nil || cnt == 0 {
|
||||
if err != nil {
|
||||
return middlewares.HTTPError{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Message: "failed to get affected row count",
|
||||
OriginError: err,
|
||||
}
|
||||
}
|
||||
// debug
|
||||
return utils.Success(w)
|
||||
if cnt == 0 {
|
||||
return middlewares.HTTPError{
|
||||
StatusCode: http.StatusUnauthorized,
|
||||
Message: "already logged in",
|
||||
}
|
||||
}
|
||||
}
|
||||
return utils.Success(w)
|
||||
}
|
||||
Reference in New Issue
Block a user