go-sample-webpage/pkg/user/userclient.go

50 lines
955 B
Go
Raw Normal View History

2021-11-04 02:04:05 +00:00
package user
import (
"errors"
"fmt"
)
type UserClient interface {
2021-11-09 18:23:19 +00:00
register(username, password, email string) (bool, error)
2021-11-04 02:04:05 +00:00
login(username, password string) (bool, error)
}
type UserClientMemory struct {
users map[string]string
}
var u *UserClientMemory
func GetUserClient() *UserClientMemory {
if u == nil {
uc := UserClientMemory{}
uc.users = make(map[string]string)
uc.users["admin"]="password"
u = &uc
}
return u
}
2021-11-09 18:23:19 +00:00
func (uc *UserClientMemory) register(username, password, email string) (bool, error) {
2021-11-04 02:04:05 +00:00
if _, ok := uc.users[username]; ok {
return false, errors.New("Username already used")
}
uc.users[username] = password
return true, nil
}
func (uc UserClientMemory) login(username, password string) (bool, error) {
if val, ok := uc.users[username]; ok {
fmt.Println("Login for valide user")
if val == password {
return true, nil
}
} else {
fmt.Printf("User %v not found", username)
}
return false, nil
}