add login/register/reset/logout views

This commit is contained in:
2025-01-05 23:54:50 +01:00
parent 273918f542
commit 48491d0786
7 changed files with 271 additions and 25 deletions

View File

@@ -48,7 +48,6 @@ func (uc *UserController) Register(c *gin.Context) {
return
}
//respond
c.JSON(http.StatusOK, gin.H{})
}
@@ -102,22 +101,86 @@ func (uc *UserController) Validate(c *gin.Context) {
}
func (rc *UserController) LoginView(c *gin.Context) {
rooms, _ := repositories.Rooms.GetAll()
data := gin.H{
"title": "Room Page",
"rooms": rooms,
//if already logged in
_, exists := c.Get("user")
if exists {
c.HTML(http.StatusOK, "index.html", CreateSessionData(c, gin.H{}))
return
}
c.HTML(http.StatusOK, "login.html", data)
data := gin.H{
"error": "",
}
c.HTML(http.StatusOK, "login.html", CreateSessionData(c, data))
}
func (rc *UserController) LoginHandler(c *gin.Context) {
email := c.PostForm("email")
password := c.PostForm("password")
tokenString, err := services.Users.Login(email, password)
if err != nil {
data := gin.H{
"error": "Login Failed. Wrong Email or Password!",
}
c.HTML(http.StatusOK, "login.html", data)
return
}
//set this so that CreateSessionData works
//otherwise header would not be generated correctly
user, _ := repositories.Users.GetByEmail(email)
c.Set("user", user)
// send it back
//c.SetSameSite(http.SameSiteLaxMode)
c.SetCookie("Authorization", tokenString, 3600 * 24, "", "", false, true)
c.HTML(http.StatusPermanentRedirect, "index.html", CreateSessionData(c, gin.H{}))
}
func CreateSessionData(c *gin.Context, extra any) gin.H {
_, exists := c.Get("user")
return gin.H{
"test": "HEllo World",
"loggedIn": exists,
"data": extra,
}
}
func (rc *UserController) RegisterHandler(c *gin.Context) {
name := c.PostForm("name")
email := c.PostForm("email")
password := c.PostForm("password")
_, err := services.Users.Register(name, email, password)
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. Try logging in.",
}
c.HTML(http.StatusOK, "register.html", data)
}
func (rc *UserController) RegisterView(c *gin.Context) {
rooms, _ := repositories.Rooms.GetAll()
data := gin.H{
"title": "Room Page",
"rooms": rooms,
"error": "",
"success": "",
}
c.HTML(http.StatusOK, "register.html", data)
@@ -134,7 +197,7 @@ func (rc *UserController) ResetView(c *gin.Context) {
c.HTML(http.StatusOK, "passwordreset.html", data)
}
func (rc *UserController) MainView(c *gin.Context) {
func (rc *UserController) ResetHandler(c *gin.Context) {
rooms, _ := repositories.Rooms.GetAll()
data := gin.H{
@@ -142,6 +205,20 @@ func (rc *UserController) MainView(c *gin.Context) {
"rooms": rooms,
}
c.HTML(http.StatusOK, "passwordreset.html", data)
}
func (rc *UserController) MainView(c *gin.Context) {
data := CreateSessionData(c, gin.H{
"title": "Room Page",
})
fmt.Println(data)
c.HTML(http.StatusOK, "index.html", data)
}
func (rc *UserController) Logout(c *gin.Context) {
c.SetCookie("Authorization", "", -1, "", "", false, true)
c.HTML(http.StatusOK, "index.html", gin.H{})
}