27 lines
655 B
Go
27 lines
655 B
Go
package services
|
|
|
|
import (
|
|
"errors"
|
|
// "gitea.zjmud.xyz/phyer/rbac/models"
|
|
"gitea.zjmud.xyz/phyer/rbac/repositories"
|
|
"gitea.zjmud.xyz/phyer/rbac/utils"
|
|
"golang.org/x/crypto/bcrypt"
|
|
"strconv"
|
|
)
|
|
|
|
func Login(username, password string) (string, error) {
|
|
user, err := repositories.GetUserByUsername(username)
|
|
if err != nil {
|
|
return "", errors.New("invalid credentials")
|
|
}
|
|
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
|
|
if err != nil {
|
|
return "", errors.New("invalid credentials")
|
|
}
|
|
token, err := utils.GenerateJWT(strconv.FormatUint(uint64(user.ID), 10))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return token, nil
|
|
}
|