-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
47 lines (39 loc) · 947 Bytes
/
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
package youtube_player_backend
import (
"log"
"golang.org/x/crypto/bcrypt"
)
type User struct {
ID int64
Login string
Password string
}
func (u *User) CheckPassword(password []byte) bool {
hash, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
if err != nil {
log.Printf("Error on check password: %s", err)
return false
}
if u.Password == string(hash) {
return true
}
return false
}
func HashPassword(password []byte) (string, error) {
hash, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
if err != nil {
log.Printf("Error on gen hash: %s", err)
return "", err
}
return string(hash), nil
}
type UserService interface {
UserByID(id int) (*User, error)
UserByLogin(login string) (*User, error)
CreateUser(u User) (*User, error)
}
type UserSession interface {
Login(login string) (*User, string, error)
Logout(token string)
Authenticate(token string) (*User, error)
}