Add token repositorie

This commit is contained in:
2025-04-14 23:29:24 +02:00
parent cca0b2775c
commit 6d63e53200
6 changed files with 163 additions and 30 deletions

View File

@@ -35,7 +35,7 @@ func (uc *UserController) Register(c *gin.Context) {
return
}
_, err = services.Users.Register(body.Name, body.Email, body.Password)
_, err = services.Users.Register(body.Name, body.Email, body.Password, false)
if err != nil {
fmt.Println("Error: ", err)
@@ -152,18 +152,75 @@ func (rc *UserController) RegisterHandler(c *gin.Context) {
email := c.PostForm("email")
password := c.PostForm("password")
_, err := services.Users.Register(name, email, password)
//first registered user is admin
isEmpty, _ := repositories.Users.IsEmpty()
if isEmpty {
_, err := services.Users.Register(name, email, password, true)
if err != nil {
data := gin.H{
"error": "Registering Failed.",
"success": "",
}
c.HTML(http.StatusOK, "register.html", data)
return
}
data := gin.H{
"error": "",
"success": "You successfully registered as Admin. Try logging in.",
}
c.HTML(http.StatusOK, "register.html", data)
return
}
//for any other user token is required
token := c.PostForm("token")
if token == "" {
data := gin.H{
"error": "No token. No register.",
"success": "",
}
c.HTML(http.StatusOK, "register.html", data)
}
tokenExists, err := repositories.Tokens.Exists(token)
if err != nil {
data := gin.H{
"error": err,
"success": "",
}
c.HTML(http.StatusOK, "register.html", data)
}
if !tokenExists {
data := gin.H{
"error": "Invalid Token.",
"success": "",
}
c.HTML(http.StatusOK, "register.html", data)
}
_, err = services.Users.Register(name, email, password, false)
if err != nil {
data := gin.H{
"error": "Registering Failed.",
"success": "",
}
c.HTML(http.StatusOK, "register.html", data)
return
}
err = repositories.Tokens.Delete(token)
if err != nil {
fmt.Println("Could not delete RegisterToken: ", err)
}
data := gin.H{
"error": "",
"success": "You successfully registered. Try logging in.",