39 lines
800 B
Go
39 lines
800 B
Go
package auth
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type LoginRequest struct {
|
|
Email string `json:"email"`
|
|
Password string `json:"password"`
|
|
}
|
|
|
|
type LoginResponse struct {
|
|
Token string `json:"token"`
|
|
}
|
|
|
|
func LoginHandler(c *gin.Context) {
|
|
var req LoginRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Bad request"})
|
|
return
|
|
}
|
|
|
|
// Systemnutzer
|
|
if req.Email == "systemuser@example.com" {
|
|
token, err := CreateJWT("system-user-id", req.Email, "admin", time.Hour*24*7)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Token error"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, LoginResponse{Token: token})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid credentials"})
|
|
}
|