add auth module
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/chedius/delivery-tracker/internal/auth"
|
||||
db "github.com/chedius/delivery-tracker/internal/db/sqlc"
|
||||
"github.com/chedius/delivery-tracker/internal/delivery"
|
||||
"github.com/gin-contrib/cors"
|
||||
@@ -15,6 +16,19 @@ import (
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func initAuth(queries *db.Queries) (*auth.Service, *auth.Handler) {
|
||||
secret := []byte(os.Getenv("JWT_SECRET"))
|
||||
expiry := 24 * time.Minute
|
||||
|
||||
if len(secret) == 0 {
|
||||
log.Fatal("JWT_SECRET not set")
|
||||
}
|
||||
|
||||
service := auth.New(queries, secret, expiry)
|
||||
handler := auth.NewHandler(service)
|
||||
return service, handler
|
||||
}
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
godotenv.Load()
|
||||
@@ -27,6 +41,7 @@ func main() {
|
||||
defer pool.Close()
|
||||
|
||||
queries := db.New(pool)
|
||||
_, authHandler := initAuth(queries)
|
||||
h := delivery.NewHandler(queries)
|
||||
|
||||
r := gin.Default()
|
||||
@@ -44,13 +59,20 @@ func main() {
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||
})
|
||||
|
||||
r.GET("/api/deliveries", h.GetDeliveries)
|
||||
r.GET("/api/deliveries/:id", h.GetDeliveryByID)
|
||||
r.GET("/api/deliveries/count", h.GetDeliveryCount)
|
||||
r.POST("/api/deliveries", h.CreateDelivery)
|
||||
r.PATCH("/api/deliveries/:id", h.UpdateDelivery)
|
||||
r.PATCH("/api/deliveries/:id/status", h.UpdateDeliveryStatus)
|
||||
r.DELETE("/api/deliveries/:id", h.DeleteDelivery)
|
||||
r.POST("/api/auth/register", authHandler.Register)
|
||||
r.POST("/api/auth/login", authHandler.Login)
|
||||
|
||||
authorized := r.Group("/api")
|
||||
authorized.Use(auth.AuthMiddleware([]byte(os.Getenv("JWT_SECRET"))))
|
||||
{
|
||||
authorized.GET("/deliveries", h.GetDeliveries)
|
||||
authorized.GET("/deliveries/:id", h.GetDeliveryByID)
|
||||
authorized.GET("/deliveries/count", h.GetDeliveryCount)
|
||||
authorized.POST("/deliveries", h.CreateDelivery)
|
||||
authorized.PATCH("/deliveries/:id", h.UpdateDelivery)
|
||||
authorized.PATCH("/deliveries/:id/status", h.UpdateDeliveryStatus)
|
||||
authorized.DELETE("/deliveries/:id", h.DeleteDelivery)
|
||||
}
|
||||
|
||||
r.Run(":8080")
|
||||
}
|
||||
|
||||
54
backend/cmd/seed.go/main.go
Normal file
54
backend/cmd/seed.go/main.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/chedius/delivery-tracker/internal/auth"
|
||||
db "github.com/chedius/delivery-tracker/internal/db/sqlc"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
godotenv.Load()
|
||||
|
||||
dsn := os.Getenv("DATABASE_URL")
|
||||
pool, err := pgxpool.New(ctx, dsn)
|
||||
if err != nil {
|
||||
log.Fatalf("db connect: %v", err)
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
queries := db.New(pool)
|
||||
|
||||
// Проверяем, есть ли уже пользователи
|
||||
_, err = queries.GetUserByUsername(ctx, "admin")
|
||||
if err == nil {
|
||||
log.Println("admin user already exists, skipping seed")
|
||||
return
|
||||
}
|
||||
|
||||
// Создаём через auth service (правильное хеширование)
|
||||
secret := []byte(os.Getenv("JWT_SECRET"))
|
||||
if len(secret) == 0 {
|
||||
log.Fatalf("JWT_SECRET not set")
|
||||
}
|
||||
authService := auth.New(queries, secret, 0)
|
||||
|
||||
// Пароль из env или дефолтный (только для разработки!)
|
||||
password := os.Getenv("SEED_ADMIN_PASSWORD")
|
||||
if password == "" {
|
||||
password = "admin123" // ⚠️ только для dev!
|
||||
}
|
||||
|
||||
user, token, err := authService.Register(ctx, "admin", password)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to create admin: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("created admin user: id=%s, username=%s", user.ID, user.Username)
|
||||
log.Printf("token: %s", token)
|
||||
}
|
||||
Reference in New Issue
Block a user