2026-01-13 20:26:15 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/auth"
|
2026-02-17 05:33:12 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/config"
|
2026-01-15 00:20:19 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
2026-01-30 18:21:27 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/html"
|
2026-01-13 20:26:15 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
)
|
|
|
|
|
|
2026-02-17 05:33:12 +00:00
|
|
|
type ContentSignin struct {
|
|
|
|
|
InvalidCredentials bool
|
|
|
|
|
Next string
|
|
|
|
|
}
|
|
|
|
|
type ContentSignup struct{}
|
|
|
|
|
|
2026-01-13 20:26:15 +00:00
|
|
|
func getSignin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
errorCode := r.URL.Query().Get("error")
|
2026-02-17 05:33:12 +00:00
|
|
|
next := r.URL.Query().Get("next")
|
|
|
|
|
signin(w, errorCode, next)
|
2026-01-13 20:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-15 00:20:19 +00:00
|
|
|
func getSignout(w http.ResponseWriter, r *http.Request, user *models.User) {
|
|
|
|
|
auth.SignoutUser(r, user)
|
|
|
|
|
http.Redirect(w, r, "/signin", http.StatusFound)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-13 20:26:15 +00:00
|
|
|
func getSignup(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
signup(w, r.URL.Path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func postSignin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
|
respondError(w, "Could not parse form", err, http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-17 05:33:12 +00:00
|
|
|
next := r.FormValue("next")
|
2026-01-13 20:26:15 +00:00
|
|
|
username := r.FormValue("username")
|
|
|
|
|
password := r.FormValue("password")
|
|
|
|
|
|
2026-02-17 05:33:12 +00:00
|
|
|
log.Info().Str("username", username).Str("next", next).Msg("HTML Signin")
|
2026-01-13 20:26:15 +00:00
|
|
|
|
|
|
|
|
_, err := auth.SigninUser(r, username, password)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, auth.InvalidCredentials{}) {
|
|
|
|
|
http.Redirect(w, r, "/signin?error=invalid-credentials", http.StatusFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if errors.Is(err, auth.InvalidUsername{}) {
|
|
|
|
|
http.Redirect(w, r, "/signin?error=invalid-credentials", http.StatusFound)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
respondError(w, "Failed to signin user", err, http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-02-17 05:33:12 +00:00
|
|
|
if next == "" {
|
|
|
|
|
next = "/"
|
|
|
|
|
}
|
|
|
|
|
location := config.MakeURLNidus(next)
|
|
|
|
|
http.Redirect(w, r, location, http.StatusFound)
|
2026-01-13 20:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func postSignup(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
|
respondError(w, "Could not parse form", err, http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
username := r.FormValue("username")
|
|
|
|
|
name := r.FormValue("name")
|
|
|
|
|
password := r.FormValue("password")
|
|
|
|
|
terms := r.FormValue("terms")
|
|
|
|
|
|
|
|
|
|
log.Info().Str("username", username).Str("name", name).Str("password", strings.Repeat("*", len(password))).Msg("Signup")
|
|
|
|
|
|
|
|
|
|
if terms != "on" {
|
|
|
|
|
log.Warn().Msg("Terms not agreed")
|
|
|
|
|
http.Error(w, "You must agree to the terms to register", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user, err := auth.SignupUser(r.Context(), username, name, password)
|
|
|
|
|
if err != nil {
|
|
|
|
|
respondError(w, "Failed to signup user", err, http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auth.AddUserSession(r, user)
|
|
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-17 05:33:12 +00:00
|
|
|
func signin(w http.ResponseWriter, errorCode string, next string) {
|
|
|
|
|
if next == "" {
|
|
|
|
|
next = "/"
|
|
|
|
|
}
|
2026-01-13 20:26:15 +00:00
|
|
|
data := ContentSignin{
|
|
|
|
|
InvalidCredentials: errorCode == "invalid-credentials",
|
2026-02-17 05:33:12 +00:00
|
|
|
Next: next,
|
2026-01-13 20:26:15 +00:00
|
|
|
}
|
2026-02-07 05:51:21 +00:00
|
|
|
html.RenderOrError(w, "sync/signin.html", data)
|
2026-01-13 20:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func signup(w http.ResponseWriter, path string) {
|
|
|
|
|
data := ContentSignup{}
|
2026-02-07 05:51:21 +00:00
|
|
|
html.RenderOrError(w, "sync/signup.html", data)
|
2026-01-13 20:26:15 +00:00
|
|
|
}
|