feat: added a side bar

This commit is contained in:
Mann Patel
2025-09-05 15:39:06 -06:00
parent a5bdc27de0
commit 05001a53e0
28 changed files with 1631 additions and 1655 deletions

View File

@@ -30,29 +30,12 @@ func getDefaultRedirectURL(role int) string {
}
}
// Helper function to render error pages with consistent data
func renderLoginError(w http.ResponseWriter, errorMsg string) {
utils.Render(w, "login.html", map[string]interface{}{
"Error": errorMsg,
"Title": "Login",
"IsAuthenticated": false,
})
}
func renderRegisterError(w http.ResponseWriter, errorMsg string) {
utils.Render(w, "register.html", map[string]interface{}{
"Error": errorMsg,
"Title": "Register",
"IsAuthenticated": false,
})
}
// Helper function to create and sign JWT token
func createJWTToken(userID, role int) (string, time.Time, error) {
err := godotenv.Load() // or specify path: godotenv.Load("/path/to/.env")
if err != nil {
log.Fatalf("Error loading .env file: %v", err)
log.Fatalf("Error loading .env file: %v", err)
}
// Get individual components from environment variables
@@ -60,7 +43,6 @@ func createJWTToken(userID, role int) (string, time.Time, error) {
var jwtKey = []byte(jwtSecret)
expirationTime := time.Now().Add(12 * time.Hour)
claims := &models.Claims{
UserID: userID,
@@ -113,7 +95,7 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
// Input validation
if email == "" || password == "" {
http.Redirect(w, r, "/?error=EmailAndPasswordRequired", http.StatusSeeOther)
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
@@ -130,7 +112,7 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Printf("Login failed for email %s: %v", email, err)
http.Redirect(w, r, "/?error=InvalidCredentials", http.StatusSeeOther)
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
@@ -138,7 +120,7 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
err = bcrypt.CompareHashAndPassword([]byte(storedHash), []byte(password))
if err != nil {
log.Printf("Password verification failed for user ID %d", userID)
http.Redirect(w, r, "/?error=InvalidCredentials", http.StatusSeeOther)
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
@@ -146,7 +128,7 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
tokenString, expirationTime, err := createJWTToken(userID, role)
if err != nil {
log.Printf("JWT token creation failed for user ID %d: %v", userID, err)
http.Redirect(w, r, "/?error=InternalError", http.StatusSeeOther)
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
@@ -159,7 +141,6 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, redirectURL, http.StatusSeeOther)
}
func RegisterHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
utils.Render(w, "layout.html", map[string]interface{}{
@@ -179,7 +160,7 @@ func RegisterHandler(w http.ResponseWriter, r *http.Request) {
// Input validation
if firstName == "" || lastName == "" || email == "" || password == "" || role == "" {
renderRegisterError(w, "All fields are required")
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
@@ -187,21 +168,21 @@ func RegisterHandler(w http.ResponseWriter, r *http.Request) {
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
log.Printf("Password hashing failed: %v", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
// Convert role to int
roleID, err := strconv.Atoi(role)
if err != nil {
renderRegisterError(w, "Invalid role")
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
var adminID int
if roleID == 3 { // volunteer
if adminCode == "" {
renderRegisterError(w, "Admin code is required for volunteers")
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
@@ -209,11 +190,11 @@ func RegisterHandler(w http.ResponseWriter, r *http.Request) {
err = models.DB.QueryRow(`SELECT user_id FROM users WHERE role_id = 1 AND admin_code = $1`, adminCode).Scan(&adminID)
if err != nil {
if err == sql.ErrNoRows {
renderRegisterError(w, "Invalid admin code")
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
log.Printf("DB error checking admin code: %v", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
}
@@ -227,7 +208,7 @@ func RegisterHandler(w http.ResponseWriter, r *http.Request) {
`, firstName, lastName, email, phone, string(hashedPassword), roleID).Scan(&userID)
if err != nil {
log.Printf("User registration failed: %v", err)
renderRegisterError(w, "Could not create account. Email might already be in use.")
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
@@ -239,7 +220,7 @@ func RegisterHandler(w http.ResponseWriter, r *http.Request) {
`, adminID, userID)
if err != nil {
log.Printf("Failed to link volunteer to admin: %v", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
}
@@ -248,9 +229,7 @@ func RegisterHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func LogoutHandler(w http.ResponseWriter, r *http.Request) {
clearSessionCookie(w)
http.Redirect(w, r, "/", http.StatusSeeOther)
}
}