nidus-sync/api/signup.go

38 lines
989 B
Go
Raw Normal View History

2026-03-27 06:08:55 -07:00
package api
import (
"context"
"net/http"
"strings"
"github.com/Gleipnir-Technology/nidus-sync/auth"
nhttp "github.com/Gleipnir-Technology/nidus-sync/http"
"github.com/rs/zerolog/log"
)
2026-03-27 06:08:55 -07:00
type reqSignup struct {
Username string `json:"username"`
Name string `json:"name"`
2026-03-27 06:08:55 -07:00
Password string `json:"password"`
Terms bool `json:"terms"`
2026-03-27 06:08:55 -07:00
}
2026-03-27 06:08:55 -07:00
func postSignup(ctx context.Context, r *http.Request, signup reqSignup) (string, *nhttp.ErrorWithStatus) {
log.Info().Str("username", signup.Username).Str("name", signup.Name).Str("password", strings.Repeat("*", len(signup.Password))).Msg("Signup")
if !signup.Terms {
log.Warn().Msg("Terms not agreed")
return "", nhttp.NewErrorStatus(http.StatusBadRequest, "You must agree to the terms to register")
}
user, err := auth.SignupUser(r.Context(), signup.Username, signup.Name, signup.Password)
if err != nil {
return "", nhttp.NewError("Failed to signup user", err)
}
auth.AddUserSession(ctx, user)
2026-03-27 06:08:55 -07:00
return "/", nil
}