65 lines
2.5 KiB
Go
65 lines
2.5 KiB
Go
package handlers
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"threadr/models"
|
|
"github.com/gorilla/sessions"
|
|
)
|
|
|
|
func SignupHandler(app *App) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
session := r.Context().Value("session").(*sessions.Session)
|
|
if r.Method == http.MethodPost {
|
|
username := r.FormValue("username")
|
|
password := r.FormValue("password")
|
|
err := models.CreateUser(app.DB, username, password)
|
|
if err != nil {
|
|
log.Printf("Error creating user: %v", err)
|
|
data := struct {
|
|
PageData
|
|
Error string
|
|
}{
|
|
PageData: PageData{
|
|
Title: "ThreadR - Sign Up",
|
|
Navbar: "signup",
|
|
LoggedIn: false,
|
|
ShowCookieBanner: true,
|
|
BasePath: app.Config.ThreadrDir,
|
|
StaticPath: app.Config.ThreadrDir + "/static",
|
|
CurrentURL: r.URL.Path,
|
|
},
|
|
Error: "An error occurred during sign up. Please try again.",
|
|
}
|
|
if err := app.Tmpl.ExecuteTemplate(w, "signup", data); err != nil {
|
|
log.Printf("Error executing template in SignupHandler: %v", err)
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
http.Redirect(w, r, app.Config.ThreadrDir+"/login/", http.StatusFound)
|
|
return
|
|
}
|
|
data := struct {
|
|
PageData
|
|
Error string
|
|
}{
|
|
PageData: PageData{
|
|
Title: "ThreadR - Sign Up",
|
|
Navbar: "signup",
|
|
LoggedIn: session.Values["user_id"] != nil,
|
|
ShowCookieBanner: true,
|
|
BasePath: app.Config.ThreadrDir,
|
|
StaticPath: app.Config.ThreadrDir + "/static",
|
|
CurrentURL: r.URL.Path,
|
|
},
|
|
Error: "",
|
|
}
|
|
if err := app.Tmpl.ExecuteTemplate(w, "signup", data); err != nil {
|
|
log.Printf("Error executing template in SignupHandler: %v", err)
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
} |