Compare commits

..

6 Commits

6 changed files with 101 additions and 52 deletions

View File

@@ -1,4 +1,3 @@
// internal/handlers/account/signup.go
package accountHandler
import (
@@ -20,7 +19,6 @@ import (
"github.com/justinas/nosurf"
)
// kept for handler-local parsing only (NOT stored in session)
type registerForm struct {
Username string
Email string
@@ -39,7 +37,6 @@ func SignupGet(c *gin.Context) {
}
ctx["CSRFToken"] = nosurf.Token(c.Request)
// Rehydrate maps (not structs) from session for sticky form + field errors
if v := sm.Pop(c.Request.Context(), "register.form"); v != nil {
if fm, ok := v.(map[string]string); ok {
ctx["Form"] = fm
@@ -51,11 +48,7 @@ func SignupGet(c *gin.Context) {
}
}
// layout-first, finalized path
tmpl := templateHelpers.LoadTemplateFiles(
"web/templates/layout.html",
"web/templates/account/signup.html",
)
tmpl := templateHelpers.LoadTemplateFiles("layout.html", "web/templates/account/signup.html")
c.Status(http.StatusOK)
if err := tmpl.ExecuteTemplate(c.Writer, "layout", ctx); err != nil {
@@ -79,9 +72,8 @@ func SignupPost(c *gin.Context) {
AcceptTerms: r.FormValue("accept_terms") == "on",
}
errors := validateRegisterForm(db, form)
if len(errors) > 0 {
// ✅ Stash maps instead of a struct → gob-safe with SCS
errMap := validateRegisterForm(db, form)
if len(errMap) > 0 {
formMap := map[string]string{
"username": form.Username,
"email": form.Email,
@@ -93,7 +85,7 @@ func SignupPost(c *gin.Context) {
}(),
}
sm.Put(r.Context(), "register.form", formMap)
sm.Put(r.Context(), "register.errors", errors)
sm.Put(r.Context(), "register.errors", errMap)
sm.Put(r.Context(), "flash", "Please fix the highlighted errors.")
c.Redirect(http.StatusSeeOther, "/account/signup")
@@ -101,7 +93,6 @@ func SignupPost(c *gin.Context) {
return
}
// Hash password
hash, err := securityHelpers.HashPassword(form.Password)
if err != nil {
logging.Info("❌ Hash error: %v", err)
@@ -111,18 +102,15 @@ func SignupPost(c *gin.Context) {
return
}
// Create user
id, err := usersStorage.CreateUser(db, form.Username, form.Email, hash)
if err != nil {
logging.Info("❌ CreateUser error: %v", err)
// Unique constraints might still trip here
sm.Put(r.Context(), "flash", "That username or email is already taken.")
c.Redirect(http.StatusSeeOther, "/account/signup")
c.Abort()
return
}
// Audit registration
auditlogStorage.LogSignup(
db,
id,
@@ -165,6 +153,5 @@ func validateRegisterForm(db *sql.DB, f registerForm) map[string]string {
}
func looksLikeEmail(s string) bool {
// Keep it simple; you can swap for a stricter validator later
return strings.Count(s, "@") == 1 && strings.Contains(s, ".")
}

View File

@@ -32,6 +32,7 @@ package bootstrap
import (
"context"
"database/sql"
"encoding/gob"
"fmt"
"net/http"
"time"
@@ -79,10 +80,15 @@ func Load(configPath string) (*App, error) {
return nil, fmt.Errorf("ensure schema: %w", err)
}
gob.Register(map[string]string{})
gob.Register([]string{})
gob.Register(time.Time{})
sessions := session.New(cfg)
router := gin.New()
router.Use(gin.Logger(), gin.Recovery())
//router.Use(gin.Logger(), gin.Recovery())
router.Use(gin.Logger())
router.Static("/static", "./web/static")
router.StaticFile("/favicon.ico", "./web/static/favicon.ico")

View File

@@ -1,7 +1,6 @@
package session
import (
"encoding/gob"
"net/http"
"time"
@@ -11,7 +10,6 @@ import (
)
func New(cfg config.Config) *scs.SessionManager {
gob.Register(time.Time{})
s := scs.New()
// Lifetime (absolute max age)

View File

@@ -4,6 +4,17 @@
-- - utf8mb4 for full Unicode
-- Booleans are TINYINT(1). Dates use DATE/DATETIME/TIMESTAMP as appropriate.
-- USERS
CREATE TABLE IF NOT EXISTS users (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(191) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL UNIQUE,
password_hash VARCHAR(255) NOT NULL,
is_admin TINYINT(1) NOT NULL DEFAULT 0,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP(),
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE audit_registration (
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id BIGINT UNSIGNED NOT NULL,
@@ -18,14 +29,6 @@ CREATE TABLE audit_registration (
ON DELETE CASCADE
);
-- USERS
CREATE TABLE IF NOT EXISTS users (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(191) NOT NULL UNIQUE,
password_hash VARCHAR(255) NOT NULL,
is_admin TINYINT(1) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- THUNDERBALL RESULTS
CREATE TABLE IF NOT EXISTS results_thunderball (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,

View File

@@ -1,41 +1,101 @@
{{ define "content" }}
<h2>Create your account</h2>
{{ if .Flash }}<div class="flash">{{ .Flash }}</div>{{ end }}
{{ if .Flash }}
<div class="alert alert-warning" role="alert">{{ .Flash }}</div>
{{ end }}
<form method="POST" action="/account/signup" class="form">
<input type="hidden" name="csrf_token" value="{{ .CSRFToken }}">
{{ $form := .Form }}
{{ $errs := .Errors }}
<div class="mb-3">
<label for="username">Username</label>
<input type="text" name="username" id="username" required class="form-control"
value="{{ with .Form }}{{ .Username }}{{ end }}">
{{ with .Errors }}{{ with index . "username" }}<div class="error">{{ . }}</div>{{ end }}{{ end }}
<label for="username" class="form-label">Username</label>
<input
type="text"
name="username"
id="username"
class="form-control {{ if $errs }}{{ if index $errs "username" }}is-invalid{{ end }}{{ end }}"
required
value="{{ if $form }}{{ index $form "username" }}{{ end }}"
autocomplete="username"
>
{{ if $errs }}
{{ with index $errs "username" }}
<div class="invalid-feedback">{{ . }}</div>
{{ end }}
{{ end }}
</div>
<div class="mb-3">
<label for="email">Email</label>
<input type="email" name="email" id="email" required class="form-control"
value="{{ with .Form }}{{ .Email }}{{ end }}">
{{ with .Errors }}{{ with index . "email" }}<div class="error">{{ . }}</div>{{ end }}{{ end }}
<label for="email" class="form-label">Email</label>
<input
type="email"
name="email"
id="email"
class="form-control {{ if $errs }}{{ if index $errs "email" }}is-invalid{{ end }}{{ end }}"
required
value="{{ if $form }}{{ index $form "email" }}{{ end }}"
autocomplete="email"
>
{{ if $errs }}
{{ with index $errs "email" }}
<div class="invalid-feedback">{{ . }}</div>
{{ end }}
{{ end }}
</div>
<div class="mb-3">
<label for="password">Password</label>
<input type="password" name="password" id="password" required class="form-control">
{{ with .Errors }}{{ with index . "password" }}<div class="error">{{ . }}</div>{{ end }}{{ end }}
<label for="password" class="form-label">Password</label>
<input
type="password"
name="password"
id="password"
class="form-control {{ if $errs }}{{ if index $errs "password" }}is-invalid{{ end }}{{ end }}"
required
autocomplete="new-password"
>
{{ if $errs }}
{{ with index $errs "password" }}
<div class="invalid-feedback">{{ . }}</div>
{{ end }}
{{ end }}
<div class="form-text">Minimum 8 characters.</div>
</div>
<div class="mb-3">
<label for="password_confirm">Confirm Password</label>
<input type="password" name="password_confirm" id="password_confirm" required class="form-control">
{{ with .Errors }}{{ with index . "password_confirm" }}<div class="error">{{ . }}</div>{{ end }}{{ end }}
<label for="password_confirm" class="form-label">Confirm Password</label>
<input
type="password"
name="password_confirm"
id="password_confirm"
class="form-control {{ if $errs }}{{ if index $errs "password_confirm" }}is-invalid{{ end }}{{ end }}"
required
autocomplete="new-password"
>
{{ if $errs }}
{{ with index $errs "password_confirm" }}
<div class="invalid-feedback">{{ . }}</div>
{{ end }}
{{ end }}
</div>
<div class="form-check mb-3">
<input type="checkbox" name="accept_terms" id="accept_terms" class="form-check-input"
{{ with .Form }}{{ if .AcceptTerms }}checked{{ end }}{{ end }}>
<input
type="checkbox"
name="accept_terms"
id="accept_terms"
class="form-check-input {{ if $errs }}{{ if index $errs "accept_terms" }}is-invalid{{ end }}{{ end }}"
{{ if $form }}{{ if eq (index $form "accept_terms") "on" }}checked{{ end }}{{ end }}
>
<label for="accept_terms" class="form-check-label">I accept the terms</label>
{{ with .Errors }}{{ with index . "accept_terms" }}<div class="error">{{ . }}</div>{{ end }}{{ end }}
{{ if $errs }}
{{ with index $errs "accept_terms" }}
<div class="invalid-feedback d-block">{{ . }}</div>
{{ end }}
{{ end }}
</div>
<button type="submit" class="btn btn-primary">Create account</button>

View File

@@ -66,11 +66,6 @@
<!-- Main Content -->
<main class="col px-md-4 pt-4">
{{ if .Flash }}
<div class="alert alert-info" role="alert">
{{ .Flash }}
</div>
{{ end }}
{{ template "content" . }}
</main>
</div>