From 6f8a173de6c1ca4a527b58d39394f1c69168b5a8 Mon Sep 17 00:00:00 2001 From: maxwell Date: Wed, 25 Sep 2024 16:52:59 +0800 Subject: [PATCH] a --- .history/config/config_20240925160002.go | 29 ----- .history/config/config_20240925164512.go | 31 ------ .history/config/config_20240925164617.go | 31 ------ .history/config/config_20240925164750.go | 31 ------ .history/config/config_20240925164752.go | 33 ------ .history/config/config_20240925165221.go | 29 ----- .history/handlers/handlers_20240925162136.go | 105 ------------------ .history/handlers/handlers_20240925165221.go | 106 ------------------- .history/middleware/auth_20240925162153.go | 31 ------ .history/middleware/auth_20240925164622.go | 31 ------ .history/models/user_20240925163150.go | 31 ------ .history/models/user_20240925164429.go | 36 ------- .history/models/user_20240925164519.go | 36 ------- .history/models/user_20240925164536.go | 36 ------- .history/models/user_20240925164547.go | 36 ------- .history/models/user_20240925164556.go | 36 ------- .history/models/user_20240925164558.go | 36 ------- .history/models/user_20240925164614.go | 36 ------- .history/models/user_20240925164759.go | 36 ------- .history/models/user_20240925164817.go | 37 ------- .history/models/user_20240925164826.go | 37 ------- .history/models/user_20240925164931.go | 37 ------- .history/models/user_20240925165047.go | 37 ------- .history/models/user_20240925165113.go | 36 ------- .history/models/user_20240925165203.go | 36 ------- .history/models/user_20240925165204.go | 36 ------- app.log | 6 ++ config/config.go | 2 +- 28 files changed, 7 insertions(+), 1033 deletions(-) delete mode 100644 .history/config/config_20240925160002.go delete mode 100644 .history/config/config_20240925164512.go delete mode 100644 .history/config/config_20240925164617.go delete mode 100644 .history/config/config_20240925164750.go delete mode 100644 .history/config/config_20240925164752.go delete mode 100644 .history/config/config_20240925165221.go delete mode 100644 .history/handlers/handlers_20240925162136.go delete mode 100644 .history/handlers/handlers_20240925165221.go delete mode 100644 .history/middleware/auth_20240925162153.go delete mode 100644 .history/middleware/auth_20240925164622.go delete mode 100644 .history/models/user_20240925163150.go delete mode 100644 .history/models/user_20240925164429.go delete mode 100644 .history/models/user_20240925164519.go delete mode 100644 .history/models/user_20240925164536.go delete mode 100644 .history/models/user_20240925164547.go delete mode 100644 .history/models/user_20240925164556.go delete mode 100644 .history/models/user_20240925164558.go delete mode 100644 .history/models/user_20240925164614.go delete mode 100644 .history/models/user_20240925164759.go delete mode 100644 .history/models/user_20240925164817.go delete mode 100644 .history/models/user_20240925164826.go delete mode 100644 .history/models/user_20240925164931.go delete mode 100644 .history/models/user_20240925165047.go delete mode 100644 .history/models/user_20240925165113.go delete mode 100644 .history/models/user_20240925165203.go delete mode 100644 .history/models/user_20240925165204.go create mode 100644 app.log diff --git a/.history/config/config_20240925160002.go b/.history/config/config_20240925160002.go deleted file mode 100644 index ddfa586..0000000 --- a/.history/config/config_20240925160002.go +++ /dev/null @@ -1,29 +0,0 @@ -package config - -import ( - "github.com/gofiber/storage/redis/v3" -) - -type Config struct { - DBConnString string - RedisAddr string - JWTSecret string -} - -func New() *Config { - return &Config{ - DBConnString: "root:password@tcp(localhost:3306)/testfb?charset=utf8mb4&parseTime=True&loc=Local", - RedisAddr: "localhost:6379", - JWTSecret: "your-secret-key", - } -} - -func InitRedis(addr string) *redis.Storage { - return redis.New(redis.Config{ - Host: addr, - Port: 6379, - Username: "", - Password: "", - Database: 0, - }) -} diff --git a/.history/config/config_20240925164512.go b/.history/config/config_20240925164512.go deleted file mode 100644 index f136e45..0000000 --- a/.history/config/config_20240925164512.go +++ /dev/null @@ -1,31 +0,0 @@ -package config - -import ( - "github.com/gofiber/storage/redis/v3" -) - -type Config struct { - DBConnString string - RedisAddr string - JWTSecret string - ErrInvalidPassword string -} - -func New() *Config { - return &Config{ - DBConnString: "root:password@tcp(localhost:3306)/testfb?charset=utf8mb4&parseTime=True&loc=Local", - RedisAddr: "localhost:6379", - JWTSecret: "your-secret-key", - ErrInvalidPassword: "Invalid password", - } -} - -func InitRedis(addr string) *redis.Storage { - return redis.New(redis.Config{ - Host: addr, - Port: 6379, - Username: "", - Password: "", - Database: 0, - }) -} diff --git a/.history/config/config_20240925164617.go b/.history/config/config_20240925164617.go deleted file mode 100644 index f136e45..0000000 --- a/.history/config/config_20240925164617.go +++ /dev/null @@ -1,31 +0,0 @@ -package config - -import ( - "github.com/gofiber/storage/redis/v3" -) - -type Config struct { - DBConnString string - RedisAddr string - JWTSecret string - ErrInvalidPassword string -} - -func New() *Config { - return &Config{ - DBConnString: "root:password@tcp(localhost:3306)/testfb?charset=utf8mb4&parseTime=True&loc=Local", - RedisAddr: "localhost:6379", - JWTSecret: "your-secret-key", - ErrInvalidPassword: "Invalid password", - } -} - -func InitRedis(addr string) *redis.Storage { - return redis.New(redis.Config{ - Host: addr, - Port: 6379, - Username: "", - Password: "", - Database: 0, - }) -} diff --git a/.history/config/config_20240925164750.go b/.history/config/config_20240925164750.go deleted file mode 100644 index 4d19e80..0000000 --- a/.history/config/config_20240925164750.go +++ /dev/null @@ -1,31 +0,0 @@ -package config - -import ( - "github.com/gofiber/storage/redis/v3" -) -const ( - ErrInvalidPassword = "invalid password", -) -type Config struct { - DBConnString string - RedisAddr string - JWTSecret string -} - -func New() *Config { - return &Config{ - DBConnString: "root:password@tcp(localhost:3306)/testfb?charset=utf8mb4&parseTime=True&loc=Local", - RedisAddr: "localhost:6379", - JWTSecret: "your-secret-key", - } -} - -func InitRedis(addr string) *redis.Storage { - return redis.New(redis.Config{ - Host: addr, - Port: 6379, - Username: "", - Password: "", - Database: 0, - }) -} diff --git a/.history/config/config_20240925164752.go b/.history/config/config_20240925164752.go deleted file mode 100644 index 5ac971b..0000000 --- a/.history/config/config_20240925164752.go +++ /dev/null @@ -1,33 +0,0 @@ -package config - -import ( - "github.com/gofiber/storage/redis/v3" -) - -const ( - ErrInvalidPassword = "invalid password" -) - -type Config struct { - DBConnString string - RedisAddr string - JWTSecret string -} - -func New() *Config { - return &Config{ - DBConnString: "root:password@tcp(localhost:3306)/testfb?charset=utf8mb4&parseTime=True&loc=Local", - RedisAddr: "localhost:6379", - JWTSecret: "your-secret-key", - } -} - -func InitRedis(addr string) *redis.Storage { - return redis.New(redis.Config{ - Host: addr, - Port: 6379, - Username: "", - Password: "", - Database: 0, - }) -} diff --git a/.history/config/config_20240925165221.go b/.history/config/config_20240925165221.go deleted file mode 100644 index ddfa586..0000000 --- a/.history/config/config_20240925165221.go +++ /dev/null @@ -1,29 +0,0 @@ -package config - -import ( - "github.com/gofiber/storage/redis/v3" -) - -type Config struct { - DBConnString string - RedisAddr string - JWTSecret string -} - -func New() *Config { - return &Config{ - DBConnString: "root:password@tcp(localhost:3306)/testfb?charset=utf8mb4&parseTime=True&loc=Local", - RedisAddr: "localhost:6379", - JWTSecret: "your-secret-key", - } -} - -func InitRedis(addr string) *redis.Storage { - return redis.New(redis.Config{ - Host: addr, - Port: 6379, - Username: "", - Password: "", - Database: 0, - }) -} diff --git a/.history/handlers/handlers_20240925162136.go b/.history/handlers/handlers_20240925162136.go deleted file mode 100644 index e1876a9..0000000 --- a/.history/handlers/handlers_20240925162136.go +++ /dev/null @@ -1,105 +0,0 @@ -package handlers - -import ( - "strconv" - "time" - - "github.com/gofiber/fiber/v2" - "github.com/gofiber/storage/redis/v3" - "github.com/golang-jwt/jwt/v4" - "gorm.io/gorm" - - "testfb/models" -) - -func Login(db *gorm.DB, redisClient *redis.Storage) fiber.Handler { - return func(c *fiber.Ctx) error { - var input struct { - Username string `json:"username"` - Password string `json:"password"` - } - - if err := c.BodyParser(&input); err != nil { - return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid input"}) - } - - var user models.User - if err := db.Where("username = ?", input.Username).First(&user).Error; err != nil { - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Invalid credentials"}) - } - - if err := user.ComparePassword(input.Password); err != nil { - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Invalid credentials"}) - } - - token := jwt.New(jwt.SigningMethodHS256) - claims := token.Claims.(jwt.MapClaims) - claims["user_id"] = user.ID - claims["exp"] = time.Now().Add(time.Hour * 24).Unix() - - t, err := token.SignedString([]byte("your-secret-key")) - if err != nil { - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Could not login"}) - } - - // Convert user.ID to string before storing in Redis - userIDStr := strconv.FormatUint(uint64(user.ID), 10) - err = redisClient.Set(t, []byte(userIDStr), 24*time.Hour) - if err != nil { - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Could not store token"}) - } - - return c.JSON(fiber.Map{"token": t}) - } -} - -func GetCurrentUser(db *gorm.DB) fiber.Handler { - return func(c *fiber.Ctx) error { - userID := c.Locals("user_id").(uint) - - var user models.User - if err := db.First(&user, userID).Error; err != nil { - return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "User not found"}) - } - - return c.JSON(user) - } -} - -func UpdateCurrentUser(db *gorm.DB) fiber.Handler { - return func(c *fiber.Ctx) error { - userID := c.Locals("user_id").(uint) - - var input models.User - if err := c.BodyParser(&input); err != nil { - return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid input"}) - } - - var user models.User - if err := db.First(&user, userID).Error; err != nil { - return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "User not found"}) - } - - user.Email = input.Email - user.Phone = input.Phone - - if err := db.Save(&user).Error; err != nil { - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Could not update user"}) - } - - return c.JSON(user) - } -} - -func GetUserByID(db *gorm.DB) fiber.Handler { - return func(c *fiber.Ctx) error { - id := c.Params("id") - - var user models.User - if err := db.First(&user, id).Error; err != nil { - return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "User not found"}) - } - - return c.JSON(user) - } -} diff --git a/.history/handlers/handlers_20240925165221.go b/.history/handlers/handlers_20240925165221.go deleted file mode 100644 index 9aa2769..0000000 --- a/.history/handlers/handlers_20240925165221.go +++ /dev/null @@ -1,106 +0,0 @@ -package handlers - -import ( - "strconv" - "time" - - "github.com/gofiber/fiber/v2" - "github.com/gofiber/storage/redis/v3" - "github.com/golang-jwt/jwt/v4" - "gorm.io/gorm" - - "testfb/models" -) - -func Login(db *gorm.DB, redisClient *redis.Storage) fiber.Handler { - return func(c *fiber.Ctx) error { - var input struct { - Username string `json:"username"` - Password string `json:"password"` - } - - if err := c.BodyParser(&input); err != nil { - return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid input"}) - } - - var user models.User - if err := db.Where("username = ?", input.Username).First(&user).Error; err != nil { - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Invalid credentials"}) - } - - // if err := user.ComparePassword(input.Password); err != nil { - if err := user.ComparePassword(input.Password); err != nil { - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Invalid credentials"}) - } - - token := jwt.New(jwt.SigningMethodHS256) - claims := token.Claims.(jwt.MapClaims) - claims["user_id"] = user.ID - claims["exp"] = time.Now().Add(time.Hour * 24).Unix() - - t, err := token.SignedString([]byte("your-secret-key")) - if err != nil { - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Could not login"}) - } - - // Convert user.ID to string before storing in Redis - userIDStr := strconv.FormatUint(uint64(user.ID), 10) - err = redisClient.Set(t, []byte(userIDStr), 24*time.Hour) - if err != nil { - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Could not store token"}) - } - - return c.JSON(fiber.Map{"token": t}) - } -} - -func GetCurrentUser(db *gorm.DB) fiber.Handler { - return func(c *fiber.Ctx) error { - userID := c.Locals("user_id").(uint) - - var user models.User - if err := db.First(&user, userID).Error; err != nil { - return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "User not found"}) - } - - return c.JSON(user) - } -} - -func UpdateCurrentUser(db *gorm.DB) fiber.Handler { - return func(c *fiber.Ctx) error { - userID := c.Locals("user_id").(uint) - - var input models.User - if err := c.BodyParser(&input); err != nil { - return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid input"}) - } - - var user models.User - if err := db.First(&user, userID).Error; err != nil { - return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "User not found"}) - } - - user.Email = input.Email - user.Phone = input.Phone - - if err := db.Save(&user).Error; err != nil { - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Could not update user"}) - } - - return c.JSON(user) - } -} - -func GetUserByID(db *gorm.DB) fiber.Handler { - return func(c *fiber.Ctx) error { - id := c.Params("id") - - var user models.User - if err := db.First(&user, id).Error; err != nil { - return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "User not found"}) - } - - return c.JSON(user) - } -} diff --git a/.history/middleware/auth_20240925162153.go b/.history/middleware/auth_20240925162153.go deleted file mode 100644 index 05242ac..0000000 --- a/.history/middleware/auth_20240925162153.go +++ /dev/null @@ -1,31 +0,0 @@ -package middleware - -import ( - "strconv" - - "github.com/gofiber/fiber/v2" - "github.com/gofiber/storage/redis/v3" -) - -func AuthMiddleware(redisClient *redis.Storage) fiber.Handler { - return func(c *fiber.Ctx) error { - token := c.Get("Authorization") - if token == "" { - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Missing authorization token"}) - } - - userIDBytes, err := redisClient.Get(token) - if err != nil { - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Invalid or expired token"}) - } - - userIDStr := string(userIDBytes) - userID, err := strconv.ParseUint(userIDStr, 10, 64) - if err != nil { - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Invalid user ID format"}) - } - - c.Locals("user_id", uint(userID)) - return c.Next() - } -} diff --git a/.history/middleware/auth_20240925164622.go b/.history/middleware/auth_20240925164622.go deleted file mode 100644 index 05242ac..0000000 --- a/.history/middleware/auth_20240925164622.go +++ /dev/null @@ -1,31 +0,0 @@ -package middleware - -import ( - "strconv" - - "github.com/gofiber/fiber/v2" - "github.com/gofiber/storage/redis/v3" -) - -func AuthMiddleware(redisClient *redis.Storage) fiber.Handler { - return func(c *fiber.Ctx) error { - token := c.Get("Authorization") - if token == "" { - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Missing authorization token"}) - } - - userIDBytes, err := redisClient.Get(token) - if err != nil { - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Invalid or expired token"}) - } - - userIDStr := string(userIDBytes) - userID, err := strconv.ParseUint(userIDStr, 10, 64) - if err != nil { - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Invalid user ID format"}) - } - - c.Locals("user_id", uint(userID)) - return c.Next() - } -} diff --git a/.history/models/user_20240925163150.go b/.history/models/user_20240925163150.go deleted file mode 100644 index b0e76d0..0000000 --- a/.history/models/user_20240925163150.go +++ /dev/null @@ -1,31 +0,0 @@ -package models - -import ( - "time" - - "golang.org/x/crypto/bcrypt" - "gorm.io/gorm" -) - -type User struct { - ID uint `gorm:"primarykey" json:"id"` - Username string `gorm:"unique" json:"username"` - Password string `json:"-"` - Email string `json:"email"` - Phone string `json:"phone"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` -} - -func (u *User) BeforeCreate(tx *gorm.DB) error { - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) - if err != nil { - return err - } - u.Password = string(hashedPassword) - return nil -} - -func (u *User) ComparePassword(password string) error { - return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) -} diff --git a/.history/models/user_20240925164429.go b/.history/models/user_20240925164429.go deleted file mode 100644 index 9f2a5ea..0000000 --- a/.history/models/user_20240925164429.go +++ /dev/null @@ -1,36 +0,0 @@ -package models - -import ( - "time" - - "golang.org/x/crypto/bcrypt" - "gorm.io/gorm" -) - -type User struct { - ID uint `gorm:"primarykey" json:"id"` - Username string `gorm:"unique" json:"username"` - Password string `json:"-"` - Email string `json:"email"` - Phone string `json:"phone"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` -} - -func (u *User) BeforeCreate(tx *gorm.DB) error { - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) - if err != nil { - return err - } - u.Password = string(hashedPassword) - return nil -} - -func (u *User) ComparePassword(password string) error { - //直接比较密码,不用bcrypt - if u.Password != password { - return ErrInvalidPassword - } - return nil - // return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) -} diff --git a/.history/models/user_20240925164519.go b/.history/models/user_20240925164519.go deleted file mode 100644 index 9f2a5ea..0000000 --- a/.history/models/user_20240925164519.go +++ /dev/null @@ -1,36 +0,0 @@ -package models - -import ( - "time" - - "golang.org/x/crypto/bcrypt" - "gorm.io/gorm" -) - -type User struct { - ID uint `gorm:"primarykey" json:"id"` - Username string `gorm:"unique" json:"username"` - Password string `json:"-"` - Email string `json:"email"` - Phone string `json:"phone"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` -} - -func (u *User) BeforeCreate(tx *gorm.DB) error { - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) - if err != nil { - return err - } - u.Password = string(hashedPassword) - return nil -} - -func (u *User) ComparePassword(password string) error { - //直接比较密码,不用bcrypt - if u.Password != password { - return ErrInvalidPassword - } - return nil - // return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) -} diff --git a/.history/models/user_20240925164536.go b/.history/models/user_20240925164536.go deleted file mode 100644 index 9f2a5ea..0000000 --- a/.history/models/user_20240925164536.go +++ /dev/null @@ -1,36 +0,0 @@ -package models - -import ( - "time" - - "golang.org/x/crypto/bcrypt" - "gorm.io/gorm" -) - -type User struct { - ID uint `gorm:"primarykey" json:"id"` - Username string `gorm:"unique" json:"username"` - Password string `json:"-"` - Email string `json:"email"` - Phone string `json:"phone"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` -} - -func (u *User) BeforeCreate(tx *gorm.DB) error { - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) - if err != nil { - return err - } - u.Password = string(hashedPassword) - return nil -} - -func (u *User) ComparePassword(password string) error { - //直接比较密码,不用bcrypt - if u.Password != password { - return ErrInvalidPassword - } - return nil - // return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) -} diff --git a/.history/models/user_20240925164547.go b/.history/models/user_20240925164547.go deleted file mode 100644 index 9f2a5ea..0000000 --- a/.history/models/user_20240925164547.go +++ /dev/null @@ -1,36 +0,0 @@ -package models - -import ( - "time" - - "golang.org/x/crypto/bcrypt" - "gorm.io/gorm" -) - -type User struct { - ID uint `gorm:"primarykey" json:"id"` - Username string `gorm:"unique" json:"username"` - Password string `json:"-"` - Email string `json:"email"` - Phone string `json:"phone"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` -} - -func (u *User) BeforeCreate(tx *gorm.DB) error { - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) - if err != nil { - return err - } - u.Password = string(hashedPassword) - return nil -} - -func (u *User) ComparePassword(password string) error { - //直接比较密码,不用bcrypt - if u.Password != password { - return ErrInvalidPassword - } - return nil - // return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) -} diff --git a/.history/models/user_20240925164556.go b/.history/models/user_20240925164556.go deleted file mode 100644 index 9f2a5ea..0000000 --- a/.history/models/user_20240925164556.go +++ /dev/null @@ -1,36 +0,0 @@ -package models - -import ( - "time" - - "golang.org/x/crypto/bcrypt" - "gorm.io/gorm" -) - -type User struct { - ID uint `gorm:"primarykey" json:"id"` - Username string `gorm:"unique" json:"username"` - Password string `json:"-"` - Email string `json:"email"` - Phone string `json:"phone"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` -} - -func (u *User) BeforeCreate(tx *gorm.DB) error { - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) - if err != nil { - return err - } - u.Password = string(hashedPassword) - return nil -} - -func (u *User) ComparePassword(password string) error { - //直接比较密码,不用bcrypt - if u.Password != password { - return ErrInvalidPassword - } - return nil - // return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) -} diff --git a/.history/models/user_20240925164558.go b/.history/models/user_20240925164558.go deleted file mode 100644 index 9f2a5ea..0000000 --- a/.history/models/user_20240925164558.go +++ /dev/null @@ -1,36 +0,0 @@ -package models - -import ( - "time" - - "golang.org/x/crypto/bcrypt" - "gorm.io/gorm" -) - -type User struct { - ID uint `gorm:"primarykey" json:"id"` - Username string `gorm:"unique" json:"username"` - Password string `json:"-"` - Email string `json:"email"` - Phone string `json:"phone"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` -} - -func (u *User) BeforeCreate(tx *gorm.DB) error { - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) - if err != nil { - return err - } - u.Password = string(hashedPassword) - return nil -} - -func (u *User) ComparePassword(password string) error { - //直接比较密码,不用bcrypt - if u.Password != password { - return ErrInvalidPassword - } - return nil - // return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) -} diff --git a/.history/models/user_20240925164614.go b/.history/models/user_20240925164614.go deleted file mode 100644 index 9f2a5ea..0000000 --- a/.history/models/user_20240925164614.go +++ /dev/null @@ -1,36 +0,0 @@ -package models - -import ( - "time" - - "golang.org/x/crypto/bcrypt" - "gorm.io/gorm" -) - -type User struct { - ID uint `gorm:"primarykey" json:"id"` - Username string `gorm:"unique" json:"username"` - Password string `json:"-"` - Email string `json:"email"` - Phone string `json:"phone"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` -} - -func (u *User) BeforeCreate(tx *gorm.DB) error { - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) - if err != nil { - return err - } - u.Password = string(hashedPassword) - return nil -} - -func (u *User) ComparePassword(password string) error { - //直接比较密码,不用bcrypt - if u.Password != password { - return ErrInvalidPassword - } - return nil - // return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) -} diff --git a/.history/models/user_20240925164759.go b/.history/models/user_20240925164759.go deleted file mode 100644 index 9f2a5ea..0000000 --- a/.history/models/user_20240925164759.go +++ /dev/null @@ -1,36 +0,0 @@ -package models - -import ( - "time" - - "golang.org/x/crypto/bcrypt" - "gorm.io/gorm" -) - -type User struct { - ID uint `gorm:"primarykey" json:"id"` - Username string `gorm:"unique" json:"username"` - Password string `json:"-"` - Email string `json:"email"` - Phone string `json:"phone"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` -} - -func (u *User) BeforeCreate(tx *gorm.DB) error { - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) - if err != nil { - return err - } - u.Password = string(hashedPassword) - return nil -} - -func (u *User) ComparePassword(password string) error { - //直接比较密码,不用bcrypt - if u.Password != password { - return ErrInvalidPassword - } - return nil - // return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) -} diff --git a/.history/models/user_20240925164817.go b/.history/models/user_20240925164817.go deleted file mode 100644 index 6bdec95..0000000 --- a/.history/models/user_20240925164817.go +++ /dev/null @@ -1,37 +0,0 @@ -package models - -import ( - . "testfb/config" - "time" - - "golang.org/x/crypto/bcrypt" - "gorm.io/gorm" -) - -type User struct { - ID uint `gorm:"primarykey" json:"id"` - Username string `gorm:"unique" json:"username"` - Password string `json:"-"` - Email string `json:"email"` - Phone string `json:"phone"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` -} - -func (u *User) BeforeCreate(tx *gorm.DB) error { - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) - if err != nil { - return err - } - u.Password = string(hashedPassword) - return nil -} - -func (u *User) ComparePassword(password string) error { - //直接比较密码,不用bcrypt - if u.Password != password { - return ErrInvalidPassword - } - return nil - // return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) -} diff --git a/.history/models/user_20240925164826.go b/.history/models/user_20240925164826.go deleted file mode 100644 index 6bdec95..0000000 --- a/.history/models/user_20240925164826.go +++ /dev/null @@ -1,37 +0,0 @@ -package models - -import ( - . "testfb/config" - "time" - - "golang.org/x/crypto/bcrypt" - "gorm.io/gorm" -) - -type User struct { - ID uint `gorm:"primarykey" json:"id"` - Username string `gorm:"unique" json:"username"` - Password string `json:"-"` - Email string `json:"email"` - Phone string `json:"phone"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` -} - -func (u *User) BeforeCreate(tx *gorm.DB) error { - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) - if err != nil { - return err - } - u.Password = string(hashedPassword) - return nil -} - -func (u *User) ComparePassword(password string) error { - //直接比较密码,不用bcrypt - if u.Password != password { - return ErrInvalidPassword - } - return nil - // return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) -} diff --git a/.history/models/user_20240925164931.go b/.history/models/user_20240925164931.go deleted file mode 100644 index 6e94137..0000000 --- a/.history/models/user_20240925164931.go +++ /dev/null @@ -1,37 +0,0 @@ -package models - -import ( - "time" - - "golang.org/x/crypto/bcrypt" - "gorm.io/gorm" -) - -type User struct { - ID uint `gorm:"primarykey" json:"id"` - Username string `gorm:"unique" json:"username"` - Password string `json:"-"` - Email string `json:"email"` - Phone string `json:"phone"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` -} - -func (u *User) BeforeCreate(tx *gorm.DB) error { - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) - if err != nil { - return err - } - u.Password = string(hashedPassword) - return nil -} - -func (u *User) ComparePassword(password string) error { - //直接比较密码,不用bcrypt - if u.Password != password { - err := "Invalid password" - return err - } - return nil - // return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) -} diff --git a/.history/models/user_20240925165047.go b/.history/models/user_20240925165047.go deleted file mode 100644 index ddfcc74..0000000 --- a/.history/models/user_20240925165047.go +++ /dev/null @@ -1,37 +0,0 @@ -package models - -import ( - "time" - - "golang.org/x/crypto/bcrypt" - "gorm.io/gorm" -) - -type User struct { - ID uint `gorm:"primarykey" json:"id"` - Username string `gorm:"unique" json:"username"` - Password string `json:"-"` - Email string `json:"email"` - Phone string `json:"phone"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` -} - -func (u *User) BeforeCreate(tx *gorm.DB) error { - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) - if err != nil { - return err - } - u.Password = string(hashedPassword) - return nil -} - -func (u *User) ComparePassword(password string) error { - //直接比较密码,不用bcrypt - if u.Password != password { - err := 123 - return err - } - return nil - // return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) -} diff --git a/.history/models/user_20240925165113.go b/.history/models/user_20240925165113.go deleted file mode 100644 index 08debe7..0000000 --- a/.history/models/user_20240925165113.go +++ /dev/null @@ -1,36 +0,0 @@ -package models - -import ( - "time" - - "golang.org/x/crypto/bcrypt" - "gorm.io/gorm" -) - -type User struct { - ID uint `gorm:"primarykey" json:"id"` - Username string `gorm:"unique" json:"username"` - Password string `json:"-"` - Email string `json:"email"` - Phone string `json:"phone"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` -} - -func (u *User) BeforeCreate(tx *gorm.DB) error { - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) - if err != nil { - return err - } - u.Password = string(hashedPassword) - return nil -} - -func (u *User) ComparePassword(password string) error { - //直接比较密码,不用bcrypt - if u.Password == password { - return nil - } - - // return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) -} diff --git a/.history/models/user_20240925165203.go b/.history/models/user_20240925165203.go deleted file mode 100644 index bbc8e34..0000000 --- a/.history/models/user_20240925165203.go +++ /dev/null @@ -1,36 +0,0 @@ -package models - -import ( - "time" - - "golang.org/x/crypto/bcrypt" - "gorm.io/gorm" -) - -type User struct { - ID uint `gorm:"primarykey" json:"id"` - Username string `gorm:"unique" json:"username"` - Password string `json:"-"` - Email string `json:"email"` - Phone string `json:"phone"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` -} - -func (u *User) BeforeCreate(tx *gorm.DB) error { - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) - if err != nil { - return err - } - u.Password = string(hashedPassword) - return nil -} - -func (u *User) ComparePassword(password string) error { - //直接比较密码,不用bcrypt - if u.Password == password { - return nil - } else { - return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) - } -} diff --git a/.history/models/user_20240925165204.go b/.history/models/user_20240925165204.go deleted file mode 100644 index bbc8e34..0000000 --- a/.history/models/user_20240925165204.go +++ /dev/null @@ -1,36 +0,0 @@ -package models - -import ( - "time" - - "golang.org/x/crypto/bcrypt" - "gorm.io/gorm" -) - -type User struct { - ID uint `gorm:"primarykey" json:"id"` - Username string `gorm:"unique" json:"username"` - Password string `json:"-"` - Email string `json:"email"` - Phone string `json:"phone"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` -} - -func (u *User) BeforeCreate(tx *gorm.DB) error { - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost) - if err != nil { - return err - } - u.Password = string(hashedPassword) - return nil -} - -func (u *User) ComparePassword(password string) error { - //直接比较密码,不用bcrypt - if u.Password == password { - return nil - } else { - return bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) - } -} diff --git a/app.log b/app.log new file mode 100644 index 0000000..bebf6d7 --- /dev/null +++ b/app.log @@ -0,0 +1,6 @@ +[16:24:09] 401 - 193.040931ms POST /login +[16:24:20] 401 - 198.309132ms POST /login +[16:24:25] 401 - 194.157634ms POST /login +[16:24:40] 401 - 3.850798ms POST /login +[16:24:45] 401 - 182.36815ms POST /login +[16:25:04] 401 - 194.986562ms POST /login diff --git a/config/config.go b/config/config.go index ddfa586..8e5db4b 100644 --- a/config/config.go +++ b/config/config.go @@ -13,7 +13,7 @@ type Config struct { func New() *Config { return &Config{ DBConnString: "root:password@tcp(localhost:3306)/testfb?charset=utf8mb4&parseTime=True&loc=Local", - RedisAddr: "localhost:6379", + RedisAddr: "localhost", JWTSecret: "your-secret-key", } }