This commit is contained in:
metacryst
2025-09-06 20:26:07 -05:00
parent 7194b48b14
commit eff0c160a5
47 changed files with 2470 additions and 254 deletions

148
server/handlers/join.go Normal file
View File

@@ -0,0 +1,148 @@
package handlers
import (
"net/http"
"github.com/rs/zerolog/log"
"crypto/rand"
"encoding/hex"
"encoding/json"
"regexp"
"time"
"context"
"hyperia/config"
"github.com/mailgun/mailgun-go/v4"
)
type joinRequest struct {
Email string `json:"email"`
}
var emailRegex = regexp.MustCompile(`^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$`)
func isValidEmail(email string) bool {
return emailRegex.MatchString(email)
}
func HandleJoin(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Only POST allowed", http.StatusMethodNotAllowed)
return
}
var creds joinRequest
if err := json.NewDecoder(r.Body).Decode(&creds); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
if !isValidEmail(creds.Email) {
http.Error(w, "Invalid email address", http.StatusBadRequest)
return
}
// exists, err := EmailExists(creds.Email)
// if err != nil {
// log.Printf("Error checking email: %v", err)
// http.Error(w, "Internal server error", http.StatusInternalServerError)
// return
// }
// if exists {
// http.Error(w, "Email already exists.", http.StatusConflict)
// return
// }
// err = CreateApplicant(creds.Email)
// if err != nil {
// log.Printf("Error creating applicant: %v", err)
// http.Error(w, "Failed to create applicant", http.StatusInternalServerError)
// return
// }
// token, err := generateVerificationToken(creds.Email)
// if err != nil {
// log.Printf("Error generating verification token: %v", err)
// http.Error(w, "Error, please try again later.", http.StatusInternalServerError)
// return
// }
// err = sendWelcomeEmail(creds.Email, token)
// if err != nil {
// log.Printf("Error sending welcome email: %v", err)
// http.Error(w, "Failed to send email", http.StatusInternalServerError)
// return
// }
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
func generateVerificationToken(email string) (string, error) {
// Create 32 random bytes → 64-char hex string
b := make([]byte, 32)
if _, err := rand.Read(b); err != nil {
return "", err
}
token := hex.EncodeToString(b)
// err := CreateApplicantVerification(email, token)
// if err != nil {
// return "", err
// }
return token, nil
}
func mailgunEmail(to string, token string) error {
// link format: https://hyperia.so/verify?token=7a1a7cb986437cf8868b18cf43d73ce2e947d65aef30b42419bab957f5e51a09
domain := "mg.hyperia.so"
apiKey := "aeb90a0c75ef782eab6fc3d48fdf4435-812b35f5-fe818055"
mg := mailgun.NewMailgun(domain, apiKey)
sender := "welcome@" + domain
subject := "Verify Your Email"
verifyLink := config.BASE_URL + "/verify?token=" + token
body := "Thanks for signing up! Please verify your email by clicking this link: " + verifyLink
message := mg.NewMessage(sender, subject, body, to)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
_, _, err := mg.Send(ctx, message)
return err
}
func sendWelcomeEmail(to string, token string) error {
if config.ENV == "development" {
verifyLink := config.BASE_URL + "/verify?token=" + token
log.Debug().Msgf("email Verify Link: %s", verifyLink)
return nil
}
return nil
// query := `
// INSERT INTO emails ("to", "from", subject, body, createdon, createdby, status)
// VALUES ($1, $2, $3, $4, $5, $6, $7)
// `
// sender := "noreply@mail.hyperia.so"
// subject := "Verify Your Email"
// verifyLink := config.BASE_URL + "/verify?token=" + token
// body := "Thanks for signing up! Please verify your email by clicking this link: " + verifyLink
// _, err := DB.Exec(
// query,
// to,
// sender,
// subject,
// body,
// time.Now(), // createdon
// "go-backend", // createdby
// "pending", // status
// )
// return err
}

107
server/handlers/login.go Normal file
View File

@@ -0,0 +1,107 @@
package handlers
import (
"encoding/json"
// "errors"
"net/http"
// "strings"
"github.com/rs/zerolog/log"
// "github.com/alexedwards/argon2id"
)
type loginRequest struct {
Name string `json:"name"`
Password string `json:"password"`
}
type user struct {
ID int `json:"id"`
Name string `json:"name"`
}
func HandleLogin(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Only POST allowed", http.StatusMethodNotAllowed)
return
}
var creds loginRequest
if err := json.NewDecoder(r.Body).Decode(&creds); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
// user, err := getUserByCredentials(creds.Name, creds.Password)
// if err != nil {
// http.Error(w, "Unauthorized: "+err.Error(), http.StatusUnauthorized)
// return
// }
w.Header().Set("Content-Type", "application/json")
http.Error(w, "Not implemented", http.StatusMethodNotAllowed)
// json.NewEncoder(w).Encode(user)
}
// func getUserByCredentials(name string, password string) (*user, error) {
// var id int
// var dbName, dbHash string
// name = strings.TrimSpace(strings.ToLower(name))
// err := DB.QueryRow("SELECT id, name, password FROM users WHERE LOWER(name) = LOWER($1)", name).Scan(&id, &dbName, &dbHash)
// if err != nil {
// return nil, errors.New("user not found")
// }
// match, err := argon2id.ComparePasswordAndHash(password, dbHash)
// if err != nil || !match {
// return nil, errors.New("invalid password")
// }
// return &user{
// ID: id,
// Name: dbName,
// }, nil
// }
func HandleApplicantLogin(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Only POST allowed", http.StatusMethodNotAllowed)
return
}
var creds loginRequest
if err := json.NewDecoder(r.Body).Decode(&creds); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
// exists, err := EmailExists(creds.Name)
// if err != nil {
// log.Err(err).Msg("error checking email")
// http.Error(w, "Internal server error", http.StatusInternalServerError)
// return
// }
// if !exists {
// http.Error(w, "Email does not exist.", http.StatusConflict)
// return
// }
token, err := generateVerificationToken(creds.Name)
if err != nil {
log.Err(err).Msg("error generating verification token")
http.Error(w, "Error, please try again later.", http.StatusInternalServerError)
return
}
err = sendWelcomeEmail(creds.Name, token)
if err != nil {
log.Err(err).Msg("error sending welcome email")
http.Error(w, "Failed to send email", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}

33
server/handlers/logout.go Normal file
View File

@@ -0,0 +1,33 @@
package handlers
import (
"net/http"
"time"
"os"
"hyperia/config"
)
func HandleLogout(w http.ResponseWriter, r *http.Request) {
// Create a cookie with the same name and domain, but expired
cookie := &http.Cookie{
Name: "auth_token",
Value: "",
Path: "/",
Domain: "." + os.Getenv("BASE_URL"), // must match what you set when logging in
HttpOnly: true,
Secure: true,
Expires: time.Unix(0, 0), // way in the past
MaxAge: -1, // tells browser to delete immediately
SameSite: http.SameSiteLaxMode,
}
if config.ENV == "development" {
cookie.Secure = false
cookie.Domain = ".hyperia.local"
}
http.SetCookie(w, cookie)
http.Redirect(w, r, config.BASE_URL, http.StatusSeeOther)
}

82
server/handlers/verify.go Normal file
View File

@@ -0,0 +1,82 @@
package handlers
import (
"log"
"net/http"
// "os"
"time"
"hyperia/config"
"github.com/golang-jwt/jwt/v5"
)
func GenerateJWT(applicantId int) (string, error) {
claims := jwt.MapClaims{
"applicant_id": applicantId,
"exp": time.Now().Add(2 * time.Hour).Unix(), // expires in 2 hours
"iat": time.Now().Unix(),
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
jwtSecret := []byte(config.JWT_SECRET)
signedToken, err := token.SignedString(jwtSecret)
if err != nil {
return "", err
}
return signedToken, nil
}
func HandleVerify(w http.ResponseWriter, r *http.Request) {
// token := r.URL.Query().Get("token")
// if token == "" {
// http.Error(w, "Missing token", http.StatusBadRequest)
// return
// }
// v, err := GetApplicantVerificationByToken(token)
// if err != nil {
// log.Println("Invalid token: ", token)
// http.Error(w, "Invalid token", http.StatusUnauthorized)
// return
// }
// if time.Since(v.CreatedOn) > 30*time.Minute || v.Expired {
// log.Println("Token expired: ", token)
// http.Error(w, "Token expired", http.StatusUnauthorized)
// return
// }
// _, err = DB.Exec(`
// UPDATE ApplicantVerifications SET Expired = 1 WHERE ApplicantId = $1
// `, v.ApplicantId)
// if err != nil {
// http.Error(w, "Failed to update verification", http.StatusInternalServerError)
// return
// }
// jwtToken, err := GenerateJWT(v.ApplicantId)
// if err != nil {
// log.Println("JWT generation error:", err)
// http.Error(w, "Failed to generate auth token", http.StatusInternalServerError)
// return
// }
// cookie := &http.Cookie{
// Name: "auth_token",
// Value: jwtToken,
// Path: "/",
// HttpOnly: true,
// Domain: "." + os.Getenv("BASE_URL"), // or ".localhost" — this allows subdomains
// Secure: true, // default to true (production)
// MaxAge: 2 * 60 * 60,
// SameSite: http.SameSiteLaxMode,
// }
// if config.ENV == "development" {
// cookie.Secure = false
// cookie.Domain = ".hyperia.local"
// }
// http.SetCookie(w, cookie)
log.Println("Verification success.")
http.Redirect(w, r, config.BASE_URL, http.StatusSeeOther)
}