users/model.go

63 lines
1.2 KiB
Go
Raw Normal View History

2023-10-19 21:15:55 +00:00
package users
import "golang.org/x/crypto/bcrypt"
type User struct {
ID string `json:"id"`
Username string `json:"username"`
HashedPassword []byte `json:"-"`
}
func (u *User) SetPassword(password string) error {
hashed, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return err
}
u.HashedPassword = hashed
return nil
}
func (u *User) ComparePassword(password string) error {
return bcrypt.CompareHashAndPassword(u.HashedPassword, []byte(password))
}
type InfoResponse struct {
Version string `json:"version"`
}
type ErrorResponse struct {
Status int `json:"status"`
Message string `json:"message"`
}
type CreateUserRequest struct {
Username string `json:"username"`
Password string `json:"password"`
}
type CreateUserResponse struct {
Message string `json:"message"`
User User `json:"user"`
}
type SetPasswordRequest struct {
NewPassword string `json:"newPassword"`
}
type VerifyRequest struct {
Password string `json:"password"`
}
// Messages
type MsgUserUpdate struct {
Message string `json:"message"`
ID string `json:"id"`
}
type MsgUserCreate struct {
Message string `json:"message"`
User User `json:"user"`
}