Added login, some auth methods and a couple routes with no implementation

This commit is contained in:
Miguel de la Cruz 2021-09-13 10:06:57 +02:00
parent f76aa74179
commit f1cae0d660
79 changed files with 8971 additions and 10 deletions

34
server/utils/utils.go Normal file
View 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)
}