-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
57 lines (53 loc) · 1.55 KB
/
types.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
package main
import (
"math/rand"
"time"
"golang.org/x/crypto/bcrypt"
)
type LogInResponse struct {
Number int64 `json:"number"`
Token string `json:"token"`
}
type TransferRequest struct {
Number int `json:"ownerAccount"`
TransferTo int `json:"transferTo"`
Amount int `json:"amonut"`
}
type CreateAccountRequest struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Email string `json:"email"`
Password string `json:"password"`
}
type Account struct {
ID int `json:"id"`
Email string `json:"email"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
EncryptedPassword string `-`
Number int64 `json:"number"`
Balance int64 `json:"balance"`
CreatedAt time.Time `json:"createdAt"`
}
type Login struct {
Number int64 `json:"number"`
Password string `json:"password"`
}
func (a *Account) validPassword(token string) bool {
return bcrypt.CompareHashAndPassword([]byte(a.EncryptedPassword), []byte(token)) == nil
}
func HandleAccount(firstName, lastName, password, email string) (*Account, error) {
encrypt, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return nil, err
}
return &Account{
// ID: rand.Intn(10000),
Email: email,
FirstName: firstName,
LastName: lastName,
EncryptedPassword: string(encrypt),
Number: int64(rand.Intn(10000000000)),
CreatedAt: time.Now().UTC(),
}, nil
}