2022-10-20 16:59:33 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"GoWeb/app"
|
2022-11-01 22:29:41 +00:00
|
|
|
"GoWeb/database/models"
|
2022-11-14 18:29:55 +00:00
|
|
|
"GoWeb/security"
|
2022-10-20 16:59:33 +00:00
|
|
|
"GoWeb/templating"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetController is a wrapper struct for the App struct
|
|
|
|
type GetController struct {
|
|
|
|
App *app.App
|
|
|
|
}
|
|
|
|
|
2022-12-05 00:04:24 +00:00
|
|
|
func (getController *GetController) ShowHome(w http.ResponseWriter, _ *http.Request) {
|
2022-10-20 16:59:33 +00:00
|
|
|
type dataStruct struct {
|
|
|
|
Test string
|
|
|
|
}
|
|
|
|
|
|
|
|
data := dataStruct{
|
|
|
|
Test: "Hello World!",
|
|
|
|
}
|
|
|
|
|
|
|
|
templating.RenderTemplate(getController.App, w, "templates/pages/home.html", data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (getController *GetController) ShowRegister(w http.ResponseWriter, r *http.Request) {
|
2022-11-14 18:29:55 +00:00
|
|
|
type dataStruct struct {
|
2022-12-05 00:04:24 +00:00
|
|
|
csrfToken string
|
2022-11-14 18:29:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create csrf token
|
2022-12-05 00:04:24 +00:00
|
|
|
csrfToken, err := security.GenerateCsrfToken(w, r)
|
2022-11-14 18:29:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data := dataStruct{
|
2022-12-05 00:04:24 +00:00
|
|
|
csrfToken: csrfToken,
|
2022-11-14 18:29:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
templating.RenderTemplate(getController.App, w, "templates/pages/register.html", data)
|
2022-10-20 16:59:33 +00:00
|
|
|
}
|
2022-11-01 16:23:44 +00:00
|
|
|
|
|
|
|
func (getController *GetController) ShowLogin(w http.ResponseWriter, r *http.Request) {
|
2022-11-14 18:29:55 +00:00
|
|
|
type dataStruct struct {
|
2022-12-05 00:04:24 +00:00
|
|
|
csrfToken string
|
2022-11-14 18:29:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create csrf token
|
2022-12-05 00:04:24 +00:00
|
|
|
csrfToken, err := security.GenerateCsrfToken(w, r)
|
2022-11-14 18:29:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data := dataStruct{
|
2022-12-05 00:04:24 +00:00
|
|
|
csrfToken: csrfToken,
|
2022-11-14 18:29:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
templating.RenderTemplate(getController.App, w, "templates/pages/login.html", data)
|
2022-11-01 16:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (getController *GetController) Logout(w http.ResponseWriter, r *http.Request) {
|
2022-11-01 22:29:41 +00:00
|
|
|
models.LogoutUser(getController.App, w, r)
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
2022-11-01 16:23:44 +00:00
|
|
|
}
|