Gate sign-up on AllowSignup field instead of user count
This commit is contained in:
+7
-8
@@ -51,11 +51,11 @@ type AuthHandler struct {
|
|||||||
Store *store.Store
|
Store *store.Store
|
||||||
Sessions *SessionStore
|
Sessions *SessionStore
|
||||||
Tpl *Templates
|
Tpl *Templates
|
||||||
|
AllowSignup bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *AuthHandler) ServeSignup(w http.ResponseWriter, r *http.Request) {
|
func (h *AuthHandler) ServeSignup(w http.ResponseWriter, r *http.Request) {
|
||||||
count, err := h.Store.UserCount()
|
if !h.AllowSignup {
|
||||||
if err == nil && count > 0 {
|
|
||||||
http.Redirect(w, r, "/signin", http.StatusSeeOther)
|
http.Redirect(w, r, "/signin", http.StatusSeeOther)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -97,14 +97,13 @@ func (h *AuthHandler) ServeSignup(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *AuthHandler) hasUsers() bool {
|
func (h *AuthHandler) allowSignup() bool {
|
||||||
count, err := h.Store.UserCount()
|
return h.AllowSignup
|
||||||
return err == nil && count > 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *AuthHandler) ServeSignin(w http.ResponseWriter, r *http.Request) {
|
func (h *AuthHandler) ServeSignin(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == http.MethodGet {
|
if r.Method == http.MethodGet {
|
||||||
h.Tpl.Render(w, "signin", map[string]any{"HasUser": h.hasUsers()})
|
h.Tpl.Render(w, "signin", map[string]any{"HasUser": h.allowSignup()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,12 +113,12 @@ func (h *AuthHandler) ServeSignin(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
user, err := h.Store.UserByEmail(email)
|
user, err := h.Store.UserByEmail(email)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.Tpl.Render(w, "signin", map[string]any{"Error": "Invalid email or password.", "HasUser": h.hasUsers()})
|
h.Tpl.Render(w, "signin", map[string]any{"Error": "Invalid email or password.", "HasUser": h.allowSignup()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(password)); err != nil {
|
if err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(password)); err != nil {
|
||||||
h.Tpl.Render(w, "signin", map[string]any{"Error": "Invalid email or password.", "HasUser": h.hasUsers()})
|
h.Tpl.Render(w, "signin", map[string]any{"Error": "Invalid email or password.", "HasUser": h.allowSignup()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user