add rest api for api_bb vue a lot of files
This commit is contained in:
@@ -1,52 +1,64 @@
|
||||
// service/auth_service.go
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"go-rest-api/internal/models"
|
||||
"go-rest-api/internal/repository"
|
||||
"errors"
|
||||
|
||||
"go-rest-api/internal/models"
|
||||
"go-rest-api/internal/repository"
|
||||
)
|
||||
|
||||
type AuthService interface {
|
||||
Register(user *models.User) error
|
||||
Login(email, password string) (*models.User, error)
|
||||
Register(user *models.User) error
|
||||
Login(email, password string) (*models.User, string, error)
|
||||
GetUserProfile(userID uint) (*models.User, error)
|
||||
}
|
||||
|
||||
type authService struct {
|
||||
userRepo repository.UserRepository
|
||||
userRepo repository.UserRepository
|
||||
jwtService JWTService
|
||||
}
|
||||
|
||||
func NewAuthService(userRepo repository.UserRepository) AuthService {
|
||||
return &authService{userRepo: userRepo}
|
||||
func NewAuthService(userRepo repository.UserRepository, jwtService JWTService) AuthService {
|
||||
return &authService{
|
||||
userRepo: userRepo,
|
||||
jwtService: jwtService,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *authService) Register(user *models.User) error {
|
||||
// Проверка существования пользователя
|
||||
existingUser, _ := s.userRepo.FindByEmail(user.Email)
|
||||
if existingUser != nil {
|
||||
return errors.New("user already exists")
|
||||
}
|
||||
|
||||
// Здесь должна быть хеширование пароля
|
||||
// user.Password = hashPassword(user.Password)
|
||||
|
||||
return s.userRepo.Create(user)
|
||||
// Проверяем, существует ли пользователь
|
||||
existingUser, err := s.userRepo.FindByEmail(user.Email)
|
||||
if err == nil && existingUser != nil {
|
||||
return errors.New("user with this email already exists")
|
||||
}
|
||||
|
||||
// Хешируем пароль
|
||||
if err := user.HashPassword(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.userRepo.Create(user)
|
||||
}
|
||||
|
||||
func (s *authService) Login(email, password string) (*models.User, error) {
|
||||
user, err := s.userRepo.FindByEmail(email)
|
||||
if err != nil {
|
||||
return nil, errors.New("invalid credentials")
|
||||
}
|
||||
|
||||
// Здесь должна быть проверка хеша пароля
|
||||
// if !checkPasswordHash(password, user.Password) {
|
||||
// return nil, errors.New("invalid credentials")
|
||||
// }
|
||||
|
||||
// Временно просто проверяем напрямую (для демо)
|
||||
if user.Password != password {
|
||||
return nil, errors.New("invalid credentials")
|
||||
}
|
||||
|
||||
return user, nil
|
||||
func (s *authService) Login(email, password string) (*models.User, string, error) {
|
||||
user, err := s.userRepo.FindByEmail(email)
|
||||
if err != nil {
|
||||
return nil, "", errors.New("invalid email or password")
|
||||
}
|
||||
|
||||
if !user.CheckPassword(password) {
|
||||
return nil, "", errors.New("invalid email or password")
|
||||
}
|
||||
|
||||
token, err := s.jwtService.GenerateToken(user.ID, user.Email)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
return user, token, nil
|
||||
}
|
||||
|
||||
func (s *authService) GetUserProfile(userID uint) (*models.User, error) {
|
||||
return s.userRepo.FindByID(userID)
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// service/jwt_service.go
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
type JWTService interface {
|
||||
GenerateToken(userID uint, email string) (string, error)
|
||||
ValidateToken(tokenString string) (*jwt.Token, error)
|
||||
ExtractUserID(token *jwt.Token) (uint, error)
|
||||
}
|
||||
|
||||
type jwtService struct {
|
||||
secretKey string
|
||||
}
|
||||
|
||||
func NewJWTService(secretKey string) JWTService {
|
||||
return &jwtService{secretKey: secretKey}
|
||||
}
|
||||
|
||||
type Claims struct {
|
||||
UserID uint `json:"user_id"`
|
||||
Email string `json:"email"`
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
func (j *jwtService) GenerateToken(userID uint, email string) (string, error) {
|
||||
claims := &Claims{
|
||||
UserID: userID,
|
||||
Email: email,
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)),
|
||||
IssuedAt: jwt.NewNumericDate(time.Now()),
|
||||
},
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
return token.SignedString([]byte(j.secretKey))
|
||||
}
|
||||
|
||||
func (j *jwtService) ValidateToken(tokenString string) (*jwt.Token, error) {
|
||||
return jwt.ParseWithClaims(tokenString, &Claims{}, func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
return []byte(j.secretKey), nil
|
||||
})
|
||||
}
|
||||
|
||||
func (j *jwtService) ExtractUserID(token *jwt.Token) (uint, error) {
|
||||
claims, ok := token.Claims.(*Claims)
|
||||
if !ok {
|
||||
return 0, errors.New("invalid token claims")
|
||||
}
|
||||
return claims.UserID, nil
|
||||
}
|
||||
Reference in New Issue
Block a user