Begin refactoring structure
This commit is contained in:
65
app/controllers/get.go
Normal file
65
app/controllers/get.go
Normal file
@ -0,0 +1,65 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"GoWeb/app"
|
||||
"GoWeb/app/models"
|
||||
"GoWeb/security"
|
||||
"GoWeb/templating"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Get is a wrapper struct for the App struct
|
||||
type Get struct {
|
||||
App *app.Deps
|
||||
}
|
||||
|
||||
func (g *Get) ShowHome(w http.ResponseWriter, _ *http.Request) {
|
||||
type dataStruct struct {
|
||||
Test string
|
||||
}
|
||||
|
||||
data := dataStruct{
|
||||
Test: "Hello World!",
|
||||
}
|
||||
|
||||
templating.RenderTemplate(w, "templates/pages/home.html", data)
|
||||
}
|
||||
|
||||
func (g *Get) ShowRegister(w http.ResponseWriter, r *http.Request) {
|
||||
type dataStruct struct {
|
||||
CsrfToken string
|
||||
}
|
||||
|
||||
CsrfToken, err := security.GenerateCsrfToken(w, r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
data := dataStruct{
|
||||
CsrfToken: CsrfToken,
|
||||
}
|
||||
|
||||
templating.RenderTemplate(w, "templates/pages/register.html", data)
|
||||
}
|
||||
|
||||
func (g *Get) ShowLogin(w http.ResponseWriter, r *http.Request) {
|
||||
type dataStruct struct {
|
||||
CsrfToken string
|
||||
}
|
||||
|
||||
CsrfToken, err := security.GenerateCsrfToken(w, r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
data := dataStruct{
|
||||
CsrfToken: CsrfToken,
|
||||
}
|
||||
|
||||
templating.RenderTemplate(w, "templates/pages/login.html", data)
|
||||
}
|
||||
|
||||
func (g *Get) Logout(w http.ResponseWriter, r *http.Request) {
|
||||
models.LogoutUser(g.App, w, r)
|
||||
http.Redirect(w, r, "/", http.StatusFound)
|
||||
}
|
52
app/controllers/post.go
Normal file
52
app/controllers/post.go
Normal file
@ -0,0 +1,52 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"GoWeb/app"
|
||||
"GoWeb/app/models"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Post is a wrapper struct for the App struct
|
||||
type Post struct {
|
||||
App *app.Deps
|
||||
}
|
||||
|
||||
func (p *Post) Login(w http.ResponseWriter, r *http.Request) {
|
||||
username := r.FormValue("username")
|
||||
password := r.FormValue("password")
|
||||
remember := r.FormValue("remember") == "on"
|
||||
|
||||
if username == "" || password == "" {
|
||||
http.Redirect(w, r, "/login", http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
_, err := models.AuthenticateUser(p.App, w, username, password, remember)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, "/login", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/", http.StatusFound)
|
||||
}
|
||||
|
||||
func (p *Post) Register(w http.ResponseWriter, r *http.Request) {
|
||||
username := r.FormValue("username")
|
||||
password := r.FormValue("password")
|
||||
createdAt := time.Now()
|
||||
updatedAt := time.Now()
|
||||
|
||||
if username == "" || password == "" {
|
||||
http.Redirect(w, r, "/register", http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
_, err := models.CreateUser(p.App, username, password, createdAt, updatedAt)
|
||||
if err != nil {
|
||||
// TODO: if err == bcrypt.ErrPasswordTooLong display error to user, this will require a flash message system with cookies
|
||||
slog.Error("error creating user: " + err.Error())
|
||||
http.Redirect(w, r, "/register", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/login", http.StatusFound)
|
||||
}
|
Reference in New Issue
Block a user