-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
48 lines (42 loc) · 951 Bytes
/
token.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
package service
import (
"fmt"
"log"
"time"
jwt "github.com/dgrijalva/jwt-go"
"golang.org/x/crypto/bcrypt"
)
type charityClaims struct {
Email string `json:"email"`
Password string `json:"password"`
jwt.StandardClaims
}
func generateToken(email string, password string) (string, error) {
claims := charityClaims{
email,
password,
jwt.StandardClaims{
ExpiresAt: time.Now().Add(30 * time.Minute).Unix(),
Issuer: "smart-kids",
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString(signingKey)
}
func hashAndSalt(pwd []byte) string {
hash, err := bcrypt.GenerateFromPassword(pwd, 14)
if err != nil {
log.Println(err)
return ""
}
return string(hash)
}
func comparePasswords(hashedPwd string, plainPwd []byte) bool {
byteHash := []byte(hashedPwd)
err := bcrypt.CompareHashAndPassword(byteHash, plainPwd)
if err != nil {
fmt.Println(err)
return false
}
return true
}