go-sample-webpage/pkg/user/userclient.go
2021-11-04 03:04:05 +01:00

50 lines
No EOL
941 B
Go

package user
import (
"errors"
"fmt"
)
type UserClient interface {
register(username, password string) (bool, error)
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
}
func (uc *UserClientMemory) register(username, password string) (bool, error) {
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
}