-
Notifications
You must be signed in to change notification settings - Fork 49
/
auth.go
58 lines (50 loc) · 1.15 KB
/
auth.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
package model
import (
"crypto/sha256"
"encoding/json"
"fmt"
"time"
"gorm.io/gorm"
)
// Auth model - `auths` table
type Auth struct {
AuthID uint64 `gorm:"primaryKey"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
Email string `json:"Email"`
Password string `json:"Password"`
Users User `gorm:"foreignkey:IDAuth;references:AuthID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
}
// UnmarshalJSON ...
func (v *Auth) UnmarshalJSON(b []byte) error {
aux := struct {
AuthID uint64 `json:"AuthID"`
Email string `json:"Email"`
Password string `json:"Password"`
}{}
if err := json.Unmarshal(b, &aux); err != nil {
return err
}
v.AuthID = aux.AuthID
v.Email = aux.Email
v.Password = HashPass(aux.Password)
return nil
}
// HashPass ...
func HashPass(pass string) string {
h := sha256.New()
h.Write([]byte(pass))
return fmt.Sprintf("%x", h.Sum(nil))
}
// MarshalJSON ...
func (v Auth) MarshalJSON() ([]byte, error) {
aux := struct {
AuthID uint64 `json:"AuthId"`
Email string `json:"Email"`
}{
AuthID: v.AuthID,
Email: v.Email,
}
return json.Marshal(aux)
}