From 48491d07863d69caa3e1d0004359bf835a71f4b1 Mon Sep 17 00:00:00 2001 From: kalipso Date: Sun, 5 Jan 2025 23:54:50 +0100 Subject: [PATCH] add login/register/reset/logout views --- controllers/userController.go | 101 ++++++++++++++++++++++++++++++---- main.go | 7 ++- views/header.html | 56 +++++++++++++++++-- views/index.html | 9 +-- views/login.html | 44 +++++++++++++++ views/passwordreset.html | 28 ++++++++++ views/register.html | 51 +++++++++++++++++ 7 files changed, 271 insertions(+), 25 deletions(-) create mode 100644 views/login.html create mode 100644 views/passwordreset.html create mode 100644 views/register.html diff --git a/controllers/userController.go b/controllers/userController.go index c9c2b1a..0b322b3 100644 --- a/controllers/userController.go +++ b/controllers/userController.go @@ -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{}) +} diff --git a/main.go b/main.go index 751b117..45fc2d2 100644 --- a/main.go +++ b/main.go @@ -71,12 +71,17 @@ func main() { apiRoutes.GET("/users/validate", authValidator.OptionalAuth, userController.Validate) } - viewRoutes := server.Group("/") + viewRoutes := server.Group("/", authValidator.OptionalAuth) { viewRoutes.GET("/", userController.MainView) + //write middleware that redirects to homescreen on register/login/reset for logged in users viewRoutes.GET("/login", userController.LoginView) + viewRoutes.GET("/logout", userController.Logout) viewRoutes.GET("/register", userController.RegisterView) viewRoutes.GET("/passwordreset", userController.ResetView) + viewRoutes.POST("/login", userController.LoginHandler) + viewRoutes.POST("/register", userController.RegisterHandler) + viewRoutes.POST("/passwordreset", userController.ResetHandler) } diff --git a/views/header.html b/views/header.html index 80818de..44e9e8d 100644 --- a/views/header.html +++ b/views/header.html @@ -1,6 +1,52 @@ - - - {{ .title }} - - + + + FreiRaum + + + + + + + diff --git a/views/index.html b/views/index.html index 7400bb5..cd71cf9 100644 --- a/views/index.html +++ b/views/index.html @@ -1,8 +1,3 @@ -{{ template "header.html" }} +{{ template "header.html" . }} -{{ range .videos }} -{{ .Title }} -{{ .Description }} -{{ end }} - -{{ template "footer.html" }} +{{ template "footer.html" . }} diff --git a/views/login.html b/views/login.html new file mode 100644 index 0000000..2c66ee7 --- /dev/null +++ b/views/login.html @@ -0,0 +1,44 @@ +{{ template "header.html" . }} + +
+
+ Your Company +

Login to your account

+
+ +
+
+
+ +
+ +
+
+ +
+
+ + +
+
+ +
+
+ +

+ {{ .error }} +

+ +
+ +
+
+
+
+ + + + +{{ template "footer.html" . }} diff --git a/views/passwordreset.html b/views/passwordreset.html new file mode 100644 index 0000000..0007bb3 --- /dev/null +++ b/views/passwordreset.html @@ -0,0 +1,28 @@ +{{ template "header.html" }} + +
+
+ Your Company +

Reset your password

+
+ +
+
+
+ +
+ +
+
+ +
+ +
+
+

+ We will send you an E-Mail with a new password. +

+
+
+ +{{ template "footer.html" }} diff --git a/views/register.html b/views/register.html new file mode 100644 index 0000000..f9632e6 --- /dev/null +++ b/views/register.html @@ -0,0 +1,51 @@ +{{ template "header.html" . }} + +
+
+ Your Company +

Register your account

+
+ +
+
+
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+
+ +
+
+ +
+
+ +

+ {{ .error }} +

+

+ {{ .success }} +

+ + + +
+ +
+
+
+
+ + +{{ template "footer.html" . }}