all repos — auth-boilerplate @ 52392195bbed8dbcca7e080b528a7b8c798682af

A simple Go web-app boilerplate.

registration toggle
Marco Andronaco andronacomarco@gmail.com
Thu, 10 Oct 2024 18:43:29 +0200
commit

52392195bbed8dbcca7e080b528a7b8c798682af

parent

32f35ef61aea6ea30b3919f4298a7c4ea5893969

M .env.example.env.example

@@ -1,6 +1,7 @@

APP_PORT=3000 APP_BASE_URL=http://localhost:3000 APP_PEPPER=AnyRandomString +APP_REGISTRATION_ENABLED=true APP_SMTP_EMAIL=your-address@gmail.com APP_SMTP_PASSWORD=yourpassword APP_SMTP_HOST=smtp.gmail.com
M README.mdREADME.md

@@ -15,6 +15,7 @@

* `APP_PORT`: defaults to `3000`. * `APP_BASE_URL`: defaults to `http://localhost:<port>`. * `APP_PEPPER`: random string, used for password hashing. +* `APP_REGISTRATION_ENABLED`: defaults to `true`. * `APP_SMTP_EMAIL`: email address you want to send mails from. * `APP_SMTP_PASSWORD`: password for said email address. * `APP_SMTP_HOST`: host for the SMTP server.
M src/app/handlers.gosrc/app/handlers.go

@@ -33,6 +33,11 @@ templates.ExecuteTemplate(w, "reset_password.html", nil)

} func postRegisterHandler(w http.ResponseWriter, r *http.Request) { + if !registrationEnabled { + http.Error(w, "Registration is currently disabled.", http.StatusForbidden) + return + } + username := r.FormValue("username") email := r.FormValue("email") password := r.FormValue("password")
M src/app/init.gosrc/app/init.go

@@ -6,6 +6,7 @@ "log"

"net/http" "os" "path/filepath" + "strings" "time" "github.com/birabittoh/auth-boilerplate/src/auth"

@@ -36,8 +37,9 @@ db *gorm.DB

g *auth.Auth m *email.Client - baseUrl string - port string + baseUrl string + port string + registrationEnabled = true ks = myks.New[uint](0) durationDay = 24 * time.Hour

@@ -61,6 +63,11 @@

baseUrl = os.Getenv("APP_BASE_URL") if baseUrl == "" { baseUrl = "http://localhost:" + port + } + + e := strings.ToLower(os.Getenv("APP_REGISTRATION_ENABLED")) + if e == "false" || e == "0" { + registrationEnabled = false } // Init auth and email
M templates/login.htmltemplates/login.html

@@ -14,11 +14,11 @@ <h1>Login</h1>

<form method="post" action="/login"> <label> <span>Username:</span> - <input type="text" name="username" autocomplete="off"> + <input type="text" name="username" autocomplete="off" required> </label> <label> <span>Password:</span> - <input type="password" name="password"> + <input type="password" name="password" required> </label> <label> <span>Remember me:</span>
M templates/new_password.htmltemplates/new_password.html

@@ -12,7 +12,7 @@

<body> <h1>Reimposta la tua password</h1> <form method="post"> - <label>Nuova Password: <input type="password" name="password"></label><br> + <label>Nuova Password: <input type="password" name="password" required></label><br> <input type="submit" value="Reimposta Password"> </form> </body>
M templates/register.htmltemplates/register.html

@@ -12,9 +12,9 @@

<body> <h1>Sign up</h1> <form method="post" action="/register"> - <label>Username: <input type="text" name="username"></label><br> - <label>Email: <input type="email" name="email"></label><br> - <label>Password: <input type="password" name="password"></label><br> + <label>Username: <input type="text" name="username" required></label><br> + <label>Email: <input type="email" name="email" required></label><br> + <label>Password: <input type="password" name="password" required></label><br> <input type="submit" value="Registrati"> </form> <a href="/login">Login</a>
M templates/reset_password.htmltemplates/reset_password.html

@@ -12,7 +12,7 @@

<body> <h1>Reset Password</h1> <form method="post" action="/reset-password"> - <label>Email: <input type="email" name="email"></label><br> + <label>Email: <input type="email" name="email" required></label><br> <input type="submit" value="Reset Password"> </form> <a href="/register">Sign up</a>