Added login, some auth methods and a couple routes with no implementation
This commit is contained in:
parent
f76aa74179
commit
f1cae0d660
79 changed files with 8971 additions and 10 deletions
34
server/utils/utils.go
Normal file
34
server/utils/utils.go
Normal file
|
@ -0,0 +1,34 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func Encrypt(s string) (string, error) {
|
||||
b, err := bcrypt.GenerateFromPassword([]byte(s), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(b), nil
|
||||
}
|
||||
|
||||
func CheckHash(hash, password string) bool {
|
||||
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) == nil
|
||||
}
|
||||
|
||||
func GenerateToken(userID int, secret string) (string, error) {
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||
"userID": strconv.Itoa(userID),
|
||||
})
|
||||
|
||||
return token.SignedString([]byte(secret))
|
||||
}
|
||||
|
||||
func Millis(t time.Time) int {
|
||||
return int(t.UnixNano() / 1000000)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue