Compare commits

..

2 Commits

Author SHA1 Message Date
max
86ff949eae Update x/crypto 2024-06-25 18:17:21 -05:00
max
8476e37499 Update x/crypto 2024-04-19 11:40:15 -05:00
8 changed files with 22 additions and 61 deletions

View File

@ -13,28 +13,13 @@ type Get struct {
App *app.App
}
func (g *Get) ShowHome(w http.ResponseWriter, r *http.Request) {
func (g *Get) ShowHome(w http.ResponseWriter, _ *http.Request) {
type dataStruct struct {
CsrfToken string
IsAuthenticated bool
Test string
}
CsrfToken, err := security.GenerateCsrfToken(w, r)
if err != nil {
return
}
isAuthenticated := true
user, err := models.CurrentUser(g.App, r)
if err != nil || user.Id == 0 {
isAuthenticated = false
Test string
}
data := dataStruct{
CsrfToken: CsrfToken,
Test: "Hello World!",
IsAuthenticated: isAuthenticated,
Test: "Hello World!",
}
templating.RenderTemplate(w, "templates/pages/home.html", data)
@ -42,8 +27,7 @@ func (g *Get) ShowHome(w http.ResponseWriter, r *http.Request) {
func (g *Get) ShowRegister(w http.ResponseWriter, r *http.Request) {
type dataStruct struct {
CsrfToken string
IsAuthenticated bool
CsrfToken string
}
CsrfToken, err := security.GenerateCsrfToken(w, r)
@ -51,15 +35,8 @@ func (g *Get) ShowRegister(w http.ResponseWriter, r *http.Request) {
return
}
isAuthenticated := true
user, err := models.CurrentUser(g.App, r)
if err != nil || user.Id == 0 {
isAuthenticated = false
}
data := dataStruct{
CsrfToken: CsrfToken,
IsAuthenticated: isAuthenticated,
CsrfToken: CsrfToken,
}
templating.RenderTemplate(w, "templates/pages/register.html", data)
@ -67,8 +44,7 @@ func (g *Get) ShowRegister(w http.ResponseWriter, r *http.Request) {
func (g *Get) ShowLogin(w http.ResponseWriter, r *http.Request) {
type dataStruct struct {
CsrfToken string
IsAuthenticated bool
CsrfToken string
}
CsrfToken, err := security.GenerateCsrfToken(w, r)
@ -82,3 +58,8 @@ func (g *Get) ShowLogin(w http.ResponseWriter, r *http.Request) {
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)
}

View File

@ -50,8 +50,3 @@ func (p *Post) Register(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/login", http.StatusFound)
}
func (p *Post) Logout(w http.ResponseWriter, r *http.Request) {
models.LogoutUser(p.App, w, r)
http.Redirect(w, r, "/", http.StatusFound)
}

2
go.mod
View File

@ -4,5 +4,5 @@ go 1.22
require (
github.com/lib/pq v1.10.9
golang.org/x/crypto v0.20.0
golang.org/x/crypto v0.24.0
)

4
go.sum
View File

@ -1,4 +1,4 @@
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
golang.org/x/crypto v0.20.0 h1:jmAMJJZXr5KiCw05dfYK9QnqaqKLYXijU23lsEdcQqg=
golang.org/x/crypto v0.20.0/go.mod h1:Xwo95rrVNIoSMx9wa1JroENMToLWn3RNVrTBpLHgZPQ=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=

View File

@ -25,7 +25,7 @@ func RunAllMigrations(app *app.App) error {
Id: 1,
UserId: 1,
AuthToken: "migrate",
RememberMe: true, // Booleans must be true to migrate properly
RememberMe: false,
CreatedAt: time.Now(),
}
err = database.Migrate(app, session)

View File

@ -22,11 +22,12 @@ func Get(app *app.App) {
return
}
staticHandler := http.FileServer(http.FS(staticFS))
http.Handle("GET /static/", http.StripPrefix("/static/", staticHandler))
http.Handle("/static/", http.StripPrefix("/static/", staticHandler))
slog.Info("serving static files from embedded file system /static")
// Pages
http.HandleFunc("GET /", getController.ShowHome)
http.HandleFunc("GET /login", getController.ShowLogin)
http.HandleFunc("GET /register", getController.ShowRegister)
http.HandleFunc("/", getController.ShowHome)
http.HandleFunc("/login", getController.ShowLogin)
http.HandleFunc("/register", getController.ShowRegister)
http.HandleFunc("/logout", getController.Logout)
}

View File

@ -15,7 +15,6 @@ func Post(app *app.App) {
}
// User authentication
http.HandleFunc("POST /register-handle", middleware.Csrf(postController.Register))
http.HandleFunc("POST /login-handle", middleware.Csrf(postController.Login))
http.HandleFunc("POST /logout", middleware.Csrf(postController.Logout))
http.HandleFunc("/register-handle", middleware.Csrf(postController.Register))
http.HandleFunc("/login-handle", middleware.Csrf(postController.Login))
}

View File

@ -6,21 +6,6 @@
<link href="/static/css/style.css" rel="stylesheet">
</head>
<body>
<div class="navbar">
{{ if .IsAuthenticated }}
<form action="/logout" method="post">
<input name="csrf_token" type="hidden" value="{{ .CsrfToken }}">
<input type="submit" value="Logout">
</form>
{{ else }}
<form action="/login" method="get">
<input type="submit" value="Login">
</form>
<form action="/register" method="get">
<input type="submit" value="Register">
</form>
{{ end }}
</div>
{{ template "content" . }}
<div class="footer-container">
<footer>