initial commit

This commit is contained in:
2025-12-13 17:40:55 +01:00
parent 3219445d37
commit 726db69115
32 changed files with 4411 additions and 264 deletions

View File

@@ -0,0 +1,60 @@
package auth
import (
"database/sql"
"net/http"
"studia/internal/user"
"time"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v5"
)
type LoginRequest struct {
Email string `json:"email"`
Password string `json:"password"`
}
var secret = []byte("secret")
func LoginHandler(c *gin.Context, db *sql.DB) {
var req LoginRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
return
}
if req.Email == "" || req.Password == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Email and password are required"})
return
}
User, err := user.GetUserByEmail(db, req.Email)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid email or password"})
return
}
if !user.CheckPasswordHash(db, User.Email, req.Password) {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid email or password"})
return
}
token, err := GenerateJWT(User.ID, User.Email, User.Role)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not generate token"})
return
}
c.JSON(http.StatusOK, gin.H{"token": token})
}
func GenerateJWT(uuid string, email string, roles []string) (any, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"user": uuid,
"email": email,
"role": roles,
"exp": time.Now().Add(24 * time.Hour).Unix(),
})
return token.SignedString(secret)
}

View File

@@ -0,0 +1 @@
package auth

View File

@@ -0,0 +1,26 @@
package server
import (
"time"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v5"
)
var secret = []byte("secret")
func StartServer() {
r := gin.Default()
r.POST("/login", func(c *gin.Context) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"user": "demo",
"role": "admin",
"exp": time.Now().Add(24 * time.Hour).Unix(),
})
signed, _ := token.SignedString(secret)
c.JSON(200, gin.H{"token": signed})
})
}

View File

@@ -0,0 +1 @@
package user

View File

@@ -0,0 +1,62 @@
package user
import (
"database/sql"
"time"
"golang.org/x/crypto/bcrypt"
)
type User struct {
ID string `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
PasswordHash string `json:"-"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Role []string `json:"role"`
}
// type Repository interface {
// Create(ctx context.Context, u *User) error
// GetByID(ctx context.Context, id int64) (*User, error)
// GetByEmail(ctx context.Context, email string) (*User, error)
// Update(ctx context.Context, u *User) error
// Delete(ctx context.Context, id int64) error
// List(ctx context.Context, limit, offset int) ([]*User, error)
// CheckPasswordHash(email string, password string) bool
// }
func GetUserByEmail(db *sql.DB, email string) (*User, error) {
row := db.QueryRow("SELECT id, email, password_hash, role FROM users WHERE email=$1", email)
var user User
err := row.Scan(&user.ID, &user.Email, &user.PasswordHash, &user.Role)
if err != nil {
return nil, err
}
return &user, nil
}
func CheckPasswordHash(db *sql.DB, email string, password string) bool {
row := db.QueryRow("SELECT password_hash FROM users WHERE email=$1", email)
var hash string
if err := row.Scan(&hash); err != nil {
return false
}
UserPasswordHash, error := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if error != nil {
return false
}
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(UserPasswordHash)) == nil
}
func CreateUser(db *sql.DB, email string, name string, password string, role []string) error {
passwordHash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return err
}
_, err = db.Exec("INSERT INTO users (email, name, password_hash, role, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6)",
email, name, string(passwordHash), role, time.Now(), time.Now())
return err
}