Allow adding users to rooms

This commit is contained in:
2025-01-04 23:12:56 +01:00
parent c78a84e075
commit 2eadad9135
6 changed files with 189 additions and 14 deletions
+15 -6
View File
@@ -2,6 +2,7 @@ package controllers
import(
"os"
"fmt"
"time"
"net/http"
"golang.org/x/crypto/bcrypt"
@@ -25,9 +26,10 @@ func NewUserController(db *gorm.DB) UserController {
}
func (uc *UserController) Signup(c *gin.Context) {
func (uc *UserController) Register(c *gin.Context) {
//Get the email/passwd off req body
var body struct {
Name string
Email string
Password string
}
@@ -54,10 +56,11 @@ func (uc *UserController) Signup(c *gin.Context) {
}
//create user
user := models.User{Email: body.Email, Password: string(hash)}
user := models.User{Name: body.Name, Email: body.Email, Password: string(hash)}
result := uc.DB.Create(&user)
if result.Error != nil {
fmt.Println("Error: ", result.Error)
c.JSON(http.StatusBadRequest, gin.H{
"error": "Failed to create user",
})
@@ -136,9 +139,15 @@ func (uc *UserController) Login(c *gin.Context) {
}
func (uc *UserController) Validate(c *gin.Context) {
user, _ := c.Get("user")
user, exists := c.Get("user")
c.JSON(http.StatusOK, gin.H{
"message": 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.",
})
}
}