-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.user.go
61 lines (51 loc) · 1.5 KB
/
models.user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// models.user.go
package main
import (
"errors"
"strings"
)
type user struct {
Username string `json:"username"`
Password string `json:"-"`
}
// For this demo, we're storing the user list in memory
// We also have some users predefined.
// In a real application, this list will most likely be fetched
// from a database. Moreover, in production settings, you should
// store passwords securely by salting and hashing them instead
// of using them as we're doing in this demo
var userList = []user{
user{Username: "user1", Password: "pass1"},
user{Username: "user2", Password: "pass2"},
user{Username: "user3", Password: "pass3"},
}
// Check if the username and password combination is valid
func isUserValid(username, password string) bool {
for _, u := range userList {
if u.Username == username && u.Password == password {
return true
}
}
return false
}
// Register a new user with the given username and password
// NOTE: For this demo, we
func registerNewUser(username, password string) (*user, error) {
if strings.TrimSpace(password) == "" {
return nil, errors.New("The password can't be empty")
} else if !isUsernameAvailable(username) {
return nil, errors.New("The username isn't available")
}
u := user{Username: username, Password: password}
userList = append(userList, u)
return &u, nil
}
// Check if the supplied username is available
func isUsernameAvailable(username string) bool {
for _, u := range userList {
if u.Username == username {
return false
}
}
return true
}