package panel import ( "errors" "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt" "github.com/han-joker/moo-layout/api/moo/confm" "github.com/han-joker/moo-layout/api/moo/dbm" "github.com/han-joker/moo-layout/api/moo/logm" "github.com/han-joker/moo-layout/api/moo/toolm" "github.com/han-joker/moo-layout/api/panel/models" "github.com/han-joker/moo-layout/api/tables" "gorm.io/gorm" "net/http" "strconv" "time" ) func UserSignIn(c *gin.Context) { userSignIn := models.UserSignIn{} if err := c.ShouldBind(&userSignIn); err != nil { logm.Get().Info(err.Error()) c.JSON(http.StatusOK, gin.H{ "error": err.Error(), }) return } user := tables.User{} if err := dbm.Get().Where("username = ?", userSignIn.Username).First(&user).Error; errors.Is(err, gorm.ErrRecordNotFound) { logm.Get().Info(err.Error()) c.JSON(http.StatusOK, gin.H{ "error": err.Error(), }) return } if user.Password != toolm.Sha256HMacString(userSignIn.Password, user.PasswordSalt) { err := errors.New("password error") logm.Get().Info(err.Error()) c.JSON(http.StatusOK, gin.H{ "error": err.Error(), }) return } signingKey := []byte(confm.Get().String("app.signingKey")) // Create the Claims claims := &jwt.StandardClaims{ Audience: strconv.FormatUint(uint64(user.ID), 10), ExpiresAt: time.Now().Add(30*24*3600 * time.Second).UnixNano(), Issuer: confm.Get().String("app.issuer"), } token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) tokenString, err := token.SignedString(signingKey) if err != nil { logm.Get().Info(err.Error()) c.JSON(http.StatusOK, gin.H{ "error": err.Error(), }) return } user.JWTToken = tokenString if err := dbm.Get().Save(&user).Error; err != nil { logm.Get().Info(err.Error()) c.JSON(http.StatusOK, gin.H{ "error": err.Error(), }) return } c.JSON(http.StatusOK, gin.H{ "error": nil, "user": user, }) } func UserCheckJwtToken(c *gin.Context) { user := tables.User{} if userInterface, exists := c.Get("user"); !exists { err := errors.New("user not found") logm.Get().Info(err.Error()) c.JSON(http.StatusOK, gin.H{ "error": err.Error(), }) return } else { user = userInterface.(tables.User) } c.JSON(http.StatusOK, gin.H{ "error": nil, "user": user, }) } func UserSignOut(c *gin.Context) { user := tables.User{} if userInterface, exists := c.Get("user"); !exists { err := errors.New("user not found") logm.Get().Info(err.Error()) c.JSON(http.StatusOK, gin.H{ "error": err.Error(), }) return } else { user = userInterface.(tables.User) } user.JWTToken = "" dbm.Get().Save(&user) c.JSON(http.StatusOK, gin.H{ "error": nil, "user": user, }) } func UserChangePassword(c *gin.Context) { req := models.UserChangePassword{} if err := c.ShouldBind(&req); err != nil { logm.Get().Info(err.Error()) c.JSON(http.StatusOK, gin.H{ "error": err.Error(), }) return } if req.NewPassword != req.NewPasswordConfirm { err := errors.New("password not equal error") logm.Get().Info(err.Error()) c.JSON(http.StatusOK, gin.H{ "error": err.Error(), }) return } user := tables.User{} if userInterface, exists := c.Get("user"); !exists { err := errors.New("user not found") logm.Get().Info(err.Error()) c.JSON(http.StatusOK, gin.H{ "error": err.Error(), }) return } else { user = userInterface.(tables.User) } if user.Password != toolm.Sha256HMacString(req.Password, user.PasswordSalt) { err := errors.New("password error") logm.Get().Info(err.Error()) c.JSON(http.StatusOK, gin.H{ "error": err.Error(), }) return } user.Password = toolm.Sha256HMacString(req.NewPassword, user.PasswordSalt) if err := dbm.Get().Save(&user).Error; err != nil { logm.Get().Info(err.Error()) c.JSON(http.StatusOK, gin.H{ "error": err.Error(), }) return } c.JSON(http.StatusOK, gin.H{ "error": nil, "user": user, }) }