- Admin-only account creation (no self-registration); invite-token flow
replaces the public /auth/register endpoint
- New volunteer fields: phone, is_trainee, operational_roles,
notification_preference, admin_notes, last_login, completed_shifts
- Role-scoped profile editing: volunteers update name/phone only;
admins update all fields including notes and trainee flag
- /auth/activate endpoint for invite-token-based account activation
- /api/v1/volunteers/{id}/invite for admin to resend invite links
- last_login recorded on each successful authentication
Tests:
- Go: handler tests (auth rules, create, activate, update scoping) via
Storer/AuthServicer interfaces and fake store; auth unit tests for
HashPassword, IssueToken, and Parse
- Frontend: RTL tests for Activate, Profile, and Volunteers pages
- Fixed CRA 5 + React Router v7 Jest compatibility (moduleNameMapper +
TextEncoder polyfill)
- Replaced stale CRA App.test.tsx placeholder with real tests
CI:
- .gitea/workflows/ci.yml runs go vet, go test, tsc, and npm test on
every push and pull request
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
92 lines
2.5 KiB
Go
92 lines
2.5 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
var ErrInvalidCredentials = errors.New("invalid credentials")
|
|
|
|
type Claims struct {
|
|
VolunteerID int64 `json:"volunteer_id"`
|
|
Role string `json:"role"`
|
|
jwt.RegisteredClaims
|
|
}
|
|
|
|
type Service struct {
|
|
db *sql.DB
|
|
jwtSecret []byte
|
|
}
|
|
|
|
func NewService(db *sql.DB, secret string) *Service {
|
|
return &Service{db: db, jwtSecret: []byte(secret)}
|
|
}
|
|
|
|
// Login authenticates by email/password and returns the volunteer ID and JWT token.
|
|
func (s *Service) Login(ctx context.Context, email, password string) (int64, string, error) {
|
|
var id int64
|
|
var hash, role string
|
|
err := s.db.QueryRowContext(ctx,
|
|
`SELECT id, password, role FROM volunteers WHERE email = ? AND active = 1 AND (password != '' AND password IS NOT NULL)`,
|
|
email,
|
|
).Scan(&id, &hash, &role)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return 0, "", ErrInvalidCredentials
|
|
}
|
|
if err != nil {
|
|
return 0, "", fmt.Errorf("query volunteer: %w", err)
|
|
}
|
|
if err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)); err != nil {
|
|
return 0, "", ErrInvalidCredentials
|
|
}
|
|
token, err := s.issueToken(id, role)
|
|
return id, token, err
|
|
}
|
|
|
|
func (s *Service) issueToken(volunteerID int64, role string) (string, error) {
|
|
claims := Claims{
|
|
VolunteerID: volunteerID,
|
|
Role: role,
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)),
|
|
IssuedAt: jwt.NewNumericDate(time.Now()),
|
|
},
|
|
}
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
return token.SignedString(s.jwtSecret)
|
|
}
|
|
|
|
func (s *Service) Parse(tokenStr string) (*Claims, error) {
|
|
token, err := jwt.ParseWithClaims(tokenStr, &Claims{}, func(t *jwt.Token) (any, error) {
|
|
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
|
|
return nil, fmt.Errorf("unexpected signing method")
|
|
}
|
|
return s.jwtSecret, nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
claims, ok := token.Claims.(*Claims)
|
|
if !ok || !token.Valid {
|
|
return nil, errors.New("invalid token")
|
|
}
|
|
return claims, nil
|
|
}
|
|
|
|
func HashPassword(password string) (string, error) {
|
|
b, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
return string(b), err
|
|
}
|
|
|
|
// IssueToken mints a JWT for the given volunteer ID and role without querying the DB.
|
|
// Intended for use in tests and invite-activation flows.
|
|
func (s *Service) IssueToken(volunteerID int64, role string) (string, error) {
|
|
return s.issueToken(volunteerID, role)
|
|
}
|