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

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"))
}