12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package main
- import (
- "time"
- "golang.org/x/crypto/bcrypt"
- )
- type Model struct {
- Id uint64 `gorm:"primaryKey"`
- CreatedAt time.Time
- UpdatedAt time.Time
- }
- type User struct {
- Model
- Name string `gorm:"uniqueIndex,collate:no_case"`
- Password string
- Class uint8
- Level uint32
- Exp uint32
- Loc *Vec2 `gorm:"embedded"`
- WorldId uint64
- Gold uint64
- GoldBank uint64
- RedGem uint32
- GreenGem uint32
- BlueGem uint32
- Health uint32
- MaxHealth uint32
- Magic uint32
- MaxMagic uint32
- Weapon uint8
- Armor uint8
- Ring uint8
- Amulet uint8
- }
- func (u *User) SetPassword(password string) error {
- hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
- if err != nil {
- return err
- }
- u.Password = string(hash)
- return nil
- }
- func (u *User) VerifyPassword(password string) error {
- err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
- return err
- }
- func (u *User) PasswordCost() int {
- cost, err := bcrypt.Cost([]byte(u.Password))
- if err != nil {
- return -1
- }
- return cost
- }
|