Backend (Go/Gin/pgx): env config, pgx pool with retries, Garage S3 client (path-style, region garage), embedded golang-migrate migrations, JWT auth with refresh rotation, albums CRUD, members/roles/invites with no-escalation matrix, photo upload + async imaging/goexif WebP variants, cursor pagination, presigned URLs, likes/comments, SSE hub (album + personal), notifications, share links. Frontend (SvelteKit 2 + Svelte 5 + Tailwind v4): API wrapper with 401 refresh, auth store, all screens, Uploader/PhotoGrid/Lightbox/Comments, SSE client with reconnect + REST catch-up, three themes + system via data-theme. Deploy without Docker: Makefile builds the frontend into backend/static and embeds it (go:embed) into a single statically linked binary; systemd unit and nginx SSE example included. Use gen2brain/webp (-tags nodynamic) instead of cgo kolesa-team/go-webp to keep CGO_ENABLED=0 static builds; design.md/proposal.md updated accordingly.
94 lines
2.4 KiB
Go
94 lines
2.4 KiB
Go
package auth
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"shared-album/internal/httpx"
|
|
)
|
|
|
|
type Handler struct {
|
|
svc *Service
|
|
}
|
|
|
|
func NewHandler(svc *Service) *Handler { return &Handler{svc: svc} }
|
|
|
|
type registerRequest struct {
|
|
Email string `json:"email" binding:"required"`
|
|
Password string `json:"password" binding:"required"`
|
|
Name string `json:"name" binding:"required"`
|
|
}
|
|
|
|
func (h *Handler) Register(c *gin.Context) {
|
|
var req registerRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.RespondError(c, httpx.BadRequest("invalid_request", "email, password and name are required"))
|
|
return
|
|
}
|
|
u, pair, err := h.svc.Register(c.Request.Context(), req.Email, req.Password, req.Name)
|
|
if err != nil {
|
|
httpx.RespondError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, gin.H{"user": u.Public(), "tokens": pair})
|
|
}
|
|
|
|
type loginRequest struct {
|
|
Email string `json:"email" binding:"required"`
|
|
Password string `json:"password" binding:"required"`
|
|
}
|
|
|
|
func (h *Handler) Login(c *gin.Context) {
|
|
var req loginRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.RespondError(c, httpx.BadRequest("invalid_request", "email and password are required"))
|
|
return
|
|
}
|
|
u, pair, err := h.svc.Login(c.Request.Context(), req.Email, req.Password)
|
|
if err != nil {
|
|
httpx.RespondError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"user": u.Public(), "tokens": pair})
|
|
}
|
|
|
|
type refreshRequest struct {
|
|
RefreshToken string `json:"refresh_token" binding:"required"`
|
|
}
|
|
|
|
func (h *Handler) Refresh(c *gin.Context) {
|
|
var req refreshRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.RespondError(c, httpx.BadRequest("invalid_request", "refresh_token is required"))
|
|
return
|
|
}
|
|
pair, err := h.svc.Refresh(c.Request.Context(), req.RefreshToken)
|
|
if err != nil {
|
|
httpx.RespondError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"tokens": pair})
|
|
}
|
|
|
|
func (h *Handler) Logout(c *gin.Context) {
|
|
var req refreshRequest
|
|
_ = c.ShouldBindJSON(&req)
|
|
if req.RefreshToken != "" {
|
|
if err := h.svc.Logout(c.Request.Context(), req.RefreshToken); err != nil {
|
|
httpx.RespondError(c, err)
|
|
return
|
|
}
|
|
}
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
|
|
func (h *Handler) Me(c *gin.Context) {
|
|
u, err := h.svc.store.GetUserByID(c.Request.Context(), UserID(c))
|
|
if err != nil {
|
|
httpx.RespondError(c, httpx.Unauthorized("unauthorized", "user not found"))
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"user": u.Public()})
|
|
}
|