This commit is contained in:
metacryst
2025-09-30 17:44:39 -05:00
parent 7c8fd24b49
commit faf2041b7f
12 changed files with 421 additions and 270 deletions

View File

@@ -1,11 +1,12 @@
package handlers
import (
"encoding/json"
"errors"
"log"
"net/http"
// "strings"
"os"
"strings"
"strconv"
"hyperia/db"
@@ -29,28 +30,57 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
}
var creds loginRequest
if err := json.NewDecoder(r.Body).Decode(&creds); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
if err := r.ParseForm(); err != nil {
http.Error(w, "Unable to parse form", http.StatusBadRequest)
return
}
email := r.FormValue("email")
password := r.FormValue("password")
creds.Email = email
creds.Password = password
user, err := getUserByCredentials(creds)
if err != nil {
if err != nil || user == 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)
keyInt, err := strconv.Atoi(user["key"].(string))
if err != nil {
// This means the string couldn't be parsed as an int — handle it
log.Println("user['key'] is not a valid int:", err)
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
jwtToken, err := GenerateJWT(keyInt)
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,
}
http.SetCookie(w, cookie)
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func getUserByCredentials(loginCreds loginRequest) (map[string]interface{}, error) {
// email := strings.TrimSpace(strings.ToLower(loginCreds.Email))
email := strings.TrimSpace(strings.ToLower(loginCreds.Email))
user, err := db.Get.User("1")
// err := DB.QueryRow("SELECT id, name, password FROM users WHERE LOWER(name) = LOWER($1)", name).Scan(&id, &dbName, &dbHash)
user, err := db.Get.UserByEmail(email)
if err != nil {
return nil, errors.New("user not found")
}

View File

@@ -14,20 +14,14 @@ func HandleLogout(w http.ResponseWriter, r *http.Request) {
Name: "auth_token",
Value: "",
Path: "/",
Domain: "." + os.Getenv("BASE_URL"), // must match what you set when logging in
HttpOnly: true,
Domain: "." + os.Getenv("BASE_URL"), // must match what you set when logging in
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)
}

100
server/handlers/signup.go Normal file
View File

@@ -0,0 +1,100 @@
package handlers
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
"strconv"
)
// Struct for incoming JSON request
type SignupRequest struct {
Email string `json:"email"`
}
// Struct for JSON response
type SignupResponse struct {
Message string `json:"message"`
}
type User struct {
Email string `json:"email"`
}
func HandleSignup(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Only POST allowed", http.StatusMethodNotAllowed)
return
}
if err := r.ParseForm(); err != nil {
http.Error(w, "Failed to parse form", http.StatusBadRequest)
return
}
email := strings.TrimSpace(r.FormValue("email"))
if email == "" {
http.Error(w, "Missing email", http.StatusBadRequest)
return
}
// Optional: basic email format check
if !strings.Contains(email, "@") {
http.Error(w, "Invalid email format", http.StatusBadRequest)
return
}
log.Printf("Received signup from email: %s", email)
err := AddUserToFile(email, "db/users.json")
if err != nil {
log.Printf("Error saving user: %v", err)
http.Error(w, "Failed to save user", http.StatusInternalServerError)
return
}
// Respond with success
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(SignupResponse{
Message: fmt.Sprintf("Signup received for %s", email),
})
}
func AddUserToFile(email string, filepath string) error {
// Read the current users (if file exists)
users := make(map[string]User)
if existingData, err := os.ReadFile(filepath); err == nil && len(existingData) > 0 {
if err := json.Unmarshal(existingData, &users); err != nil {
return fmt.Errorf("invalid users.json format: %v", err)
}
}
// Find the next numeric key
maxID := 0
for key := range users {
id, err := strconv.Atoi(key)
if err == nil && id > maxID {
maxID = id
}
}
newID := strconv.Itoa(maxID + 1)
// Add new user
users[newID] = User{Email: email}
// Marshal updated data
updated, err := json.MarshalIndent(users, "", " ")
if err != nil {
return fmt.Errorf("could not marshal updated users: %v", err)
}
// Write updated data back to file
if err := os.WriteFile(filepath, updated, 0644); err != nil {
return fmt.Errorf("could not write to users file: %v", err)
}
return nil
}

View File

@@ -1,18 +1,15 @@
package handlers
import (
"log"
"net/http"
// "os"
"time"
"hyperia/config"
"github.com/golang-jwt/jwt/v5"
)
func GenerateJWT(applicantId int) (string, error) {
func GenerateJWT(userId int) (string, error) {
claims := jwt.MapClaims{
"applicant_id": applicantId,
"applicant_id": userId,
"exp": time.Now().Add(2 * time.Hour).Unix(), // expires in 2 hours
"iat": time.Now().Unix(),
}
@@ -24,59 +21,4 @@ func GenerateJWT(applicantId int) (string, error) {
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)
}