Files
backend/controllers/userController.go
2026-04-06 22:57:57 +02:00

225 lines
4.9 KiB
Go

package controllers
import (
"errors"
"fmt"
"net/http"
"github.com/gin-gonic/gin"
_ "github.com/swaggo/swag/example/celler/httputil"
"gorm.io/gorm"
"git.dynamicdiscord.de/harakat/backend/models"
"git.dynamicdiscord.de/harakat/backend/repositories"
"git.dynamicdiscord.de/harakat/backend/services"
)
type UserController struct{}
func NewUserController() UserController {
return UserController{}
}
type EmptyResponse struct{}
type LoginRequest struct {
Email string `json:"email" binding:"required"`
Password string `json:"password" binding:"required"`
}
type RegisterRequest struct {
LoginRequest
Name string `json:"name" binding:"required"`
}
// Register godoc
// @Summary Register user with invite token
// @Description register user with invite token
// @Tags user
// @Accept json
// @Produce json
// @Param token path string false "invite token"
// @Param user body RegisterRequest true "user data"
// @Success 200 {object} EmptyResponse
// @Failure 400 {object} httputil.HTTPError
// @Failure 404 {object} httputil.HTTPError
// @Failure 500 {object} httputil.HTTPError
// @Router /user/register/{token} [post]
func (uc *UserController) Register(c *gin.Context) {
var body RegisterRequest
err := c.BindJSON(&body)
if err != nil {
fmt.Println("Error: ", err)
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
}
name := body.Name
email := body.Email
password := body.Password
//first registered user is admin
isEmpty, _ := repositories.Users.IsEmpty()
if isEmpty {
_, err := services.Users.Register(name, email, password, true)
if err != nil {
fmt.Println("Error: ", err)
c.JSON(http.StatusBadRequest, gin.H{
"error": "Failed to create user",
})
return
}
c.JSON(http.StatusOK, gin.H{})
return
}
//for any other user token is required
token := c.PostForm("token")
if token == "" {
c.JSON(http.StatusForbidden, gin.H{
"error": "No valid token was given.",
})
return
}
tokenExists, err := repositories.Tokens.Exists(token)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
}
if !tokenExists {
c.JSON(http.StatusForbidden, gin.H{
"error": "No valid token was given.",
})
return
}
_, err = services.Users.Register(name, email, password, false)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "Registration failed",
})
return
}
err = repositories.Tokens.Delete(token)
if err != nil {
fmt.Println("Could not delete RegisterToken: ", err)
}
c.JSON(http.StatusOK, gin.H{})
}
// Login godoc
// @Summary Login user
// @Description Login user
// @Tags user
// @Accept json
// @Produce json
// @Param user body LoginRequest true "user data"
// @Success 200 {object} EmptyResponse
// @Failure 400 {object} httputil.HTTPError
// @Failure 401 {object} httputil.HTTPError
// @Failure 500 {object} httputil.HTTPError
// @Router /user/login [post]
func (uc *UserController) Login(c *gin.Context) {
//Get the email/passwd off req body
var body LoginRequest
err := c.Bind(&body)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "Login Failed",
})
return
}
tokenString, err := services.Users.Login(body.Email, body.Password)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": "Login Failed",
})
return
}
user, _ := repositories.Users.GetByEmail(body.Email)
c.Set("user", user)
// send it back
c.SetSameSite(http.SameSiteLaxMode)
c.SetCookie("Authorization", tokenString, 3600*24, "", "", false, true)
c.JSON(http.StatusOK, gin.H{})
}
// Login godoc
// @Summary User Status
// @Description Get user status of current session
// @Tags user
// @Accept json
// @Produce json
// @Success 200 {object} EmptyResponse
// @Failure 400 {object} httputil.HTTPError
// @Failure 500 {object} httputil.HTTPError
// @Router /user/status [get]
func (uc *UserController) Status(c *gin.Context) {
user, exists := c.Get("user")
if exists {
c.JSON(http.StatusOK, gin.H{
"message": fmt.Sprintf("Logged in with userID: %d", user.(models.User).ID),
})
} else {
c.JSON(http.StatusOK, gin.H{
"message": "Currently not logged in.",
})
}
}
func CreateSessionData(c *gin.Context, extra any) gin.H {
user, exists := c.Get("user")
userImpl, _ := user.(models.User)
return gin.H{
"loggedIn": exists,
"isAdmin": userImpl.IsAdmin,
"data": extra,
}
}
func (rc *UserController) InitAdmin(c *gin.Context) {
isEmpty, err := repositories.Users.IsEmpty()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
return
}
fmt.Println("FOOO")
if !isEmpty {
c.JSON(http.StatusForbidden, gin.H{
"error": "Registration is closed",
})
return
}
fmt.Println("FOOO")
rc.Register(c)
}