feat: user jwt login

This commit is contained in:
Noah Hsu
2022-06-25 21:34:44 +08:00
parent 306b90399c
commit c5295f4d72
16 changed files with 269 additions and 21 deletions

View File

@ -1,5 +1,7 @@
package model
import "github.com/pkg/errors"
const (
GENERAL = iota
GUEST // only one exists
@ -7,12 +9,12 @@ const (
)
type User struct {
ID uint `json:"id" gorm:"primaryKey"` // unique key
Name string `json:"name" gorm:"unique"` // username
Password string `json:"password"` // password
BasePath string `json:"base_path"` // base path
ReadOnly bool `json:"read_only"` // allow upload
Role int `json:"role"` // user's role
ID uint `json:"id" gorm:"primaryKey"` // unique key
Username string `json:"username" gorm:"unique"` // username
Password string `json:"password"` // password
BasePath string `json:"base_path"` // base path
ReadOnly bool `json:"read_only"` // allow upload
Role int `json:"role"` // user's role
}
func (u User) IsGuest() bool {
@ -22,3 +24,13 @@ func (u User) IsGuest() bool {
func (u User) IsAdmin() bool {
return u.Role == ADMIN
}
func (u User) ValidatePassword(password string) error {
if password == "" {
return errors.New("password is empty")
}
if u.Password != password {
return errors.New("password is incorrect")
}
return nil
}