pollo/lib/user.go

128 lines
2.9 KiB
Go
Raw Permalink Normal View History

2024-06-27 21:23:51 -06:00
package lib
import (
2024-11-21 00:48:45 -06:00
"database/sql"
2024-06-27 21:23:51 -06:00
"errors"
"net/http"
"github.com/labstack/echo/v4"
"golang.org/x/crypto/bcrypt"
)
type User struct {
2024-06-28 00:35:58 -06:00
ID string
2024-06-28 09:11:03 -06:00
Name string
2024-06-27 21:23:51 -06:00
Email string
Password string
}
// HashPassword hashes the given password using bcrypt.
func HashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
return string(bytes), err
}
// CheckPasswordHash checks if the given password matches the hashed password.
func CheckPasswordHash(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}
// GetUserByEmail fetches a user by email from the database.
2024-11-21 00:48:45 -06:00
func GetUserByEmail(db *sql.DB, email string) (*User, error) {
if db == nil {
return nil, errors.New("database client is not initialized")
2024-06-27 21:23:51 -06:00
}
var user User
2024-11-21 00:48:45 -06:00
row := db.QueryRow(
"SELECT id, name, email, password FROM users WHERE email = ?",
email)
err := row.Scan(&user.ID, &user.Name, &user.Email, &user.Password)
2024-06-27 21:23:51 -06:00
if err != nil {
return nil, err
}
return &user, nil
}
2024-06-28 09:11:03 -06:00
// GetUserByID fetches a user by ID from the database.
2024-11-21 00:48:45 -06:00
func GetUserByID(db *sql.DB, id string) (*User, error) {
if db == nil {
return nil, errors.New("database client is not initialized")
2024-06-28 09:11:03 -06:00
}
var user User
2024-11-21 00:48:45 -06:00
row := db.QueryRow(
"SELECT id, name, email, password FROM users WHERE id = ?",
id)
err := row.Scan(&user.ID, &user.Name, &user.Email, &user.Password)
2024-06-28 09:11:03 -06:00
if err != nil {
return nil, err
}
return &user, nil
}
2024-06-27 21:23:51 -06:00
// SaveUser saves a new user to the database.
2024-11-21 00:48:45 -06:00
func SaveUser(db *sql.DB, user *User) error {
if db == nil {
return errors.New("database client is not initialized")
2024-06-27 21:23:51 -06:00
}
2024-11-21 00:48:45 -06:00
result, err := db.Exec(
"INSERT INTO users (id, name, email, password) VALUES (?, ?, ?, ?)",
GenerateNewID("user"), user.Name, user.Email, user.Password)
2024-06-27 21:23:51 -06:00
if err != nil {
return err
}
2024-11-21 00:48:45 -06:00
rowsAffected, _ := result.RowsAffected()
if rowsAffected != 1 {
2024-06-27 21:23:51 -06:00
return errors.New("expected one row to be affected")
}
return nil
}
2024-07-05 23:52:27 -06:00
func AuthenticatedPageMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
2024-06-27 21:23:51 -06:00
return func(c echo.Context) error {
isSignedIn := IsSignedIn(c)
// Check if user is authenticated
if !isSignedIn {
// Redirect to signin page if not authenticated
return c.Redirect(http.StatusFound, "/signin")
}
// Proceed with the request if authenticated
return next(c)
}
}
2024-07-05 23:52:27 -06:00
2024-07-09 11:10:32 -06:00
func AuthFlowPageMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
isSignedIn := IsSignedIn(c)
if isSignedIn {
return c.Redirect(http.StatusFound, "/dashboard")
}
return next(c)
}
}
2024-07-05 23:52:27 -06:00
func AuthenticatedEndpointMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
isSignedIn := IsSignedIn(c)
// Check if user is authenticated
if !isSignedIn {
// Return 401 if not authenticated
return c.String(http.StatusUnauthorized, "Unauthorized")
}
// Proceed with the request if authenticated
return next(c)
}
}