handleSignup handles POST requests to sign up for a new Cronitor account
(w http.ResponseWriter, r *http.Request)
| 1635 | |
| 1636 | // handleSignup handles POST requests to sign up for a new Cronitor account |
| 1637 | func handleSignup(w http.ResponseWriter, r *http.Request) { |
| 1638 | if r.Method != "POST" { |
| 1639 | http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 1640 | return |
| 1641 | } |
| 1642 | |
| 1643 | var request struct { |
| 1644 | Name string `json:"name"` |
| 1645 | Email string `json:"email"` |
| 1646 | Password string `json:"password"` |
| 1647 | } |
| 1648 | |
| 1649 | if err := json.NewDecoder(r.Body).Decode(&request); err != nil { |
| 1650 | http.Error(w, "Invalid request body", http.StatusBadRequest) |
| 1651 | return |
| 1652 | } |
| 1653 | |
| 1654 | // Validate inputs |
| 1655 | if request.Name == "" || request.Email == "" || request.Password == "" { |
| 1656 | http.Error(w, "All fields are required", http.StatusBadRequest) |
| 1657 | return |
| 1658 | } |
| 1659 | |
| 1660 | if !strings.Contains(request.Email, "@") || len(request.Email) < 5 { |
| 1661 | http.Error(w, "Please enter a valid email address", http.StatusBadRequest) |
| 1662 | return |
| 1663 | } |
| 1664 | |
| 1665 | if len(request.Password) < 8 { |
| 1666 | http.Error(w, "Password must be at least 8 characters", http.StatusBadRequest) |
| 1667 | return |
| 1668 | } |
| 1669 | |
| 1670 | // Call the Cronitor API to sign up |
| 1671 | api := lib.CronitorApi{ |
| 1672 | UserAgent: "cronitor-cli", |
| 1673 | } |
| 1674 | |
| 1675 | resp, err := api.Signup(request.Name, request.Email, request.Password) |
| 1676 | if err != nil { |
| 1677 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 1678 | return |
| 1679 | } |
| 1680 | |
| 1681 | // Save the API keys to config |
| 1682 | viper.Set(varApiKey, resp.ApiKey) |
| 1683 | viper.Set(varPingApiKey, resp.PingApiKey) |
| 1684 | |
| 1685 | // Write config to file |
| 1686 | if err := viper.WriteConfig(); err != nil { |
| 1687 | // Try to create config directory if it doesn't exist |
| 1688 | if err := os.MkdirAll(defaultConfigFileDirectory(), os.ModePerm); err == nil { |
| 1689 | viper.WriteConfig() |
| 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | // Return the API keys |
| 1694 | w.Header().Set("Content-Type", "application/json") |
nothing calls this directly
no test coverage detected