Backend (Go): - Project structure with chi router, pgxpool, goose migrations - JWT auth (access/refresh tokens) with Firebase token verification - NoopTokenVerifier for local dev without Firebase credentials - PostgreSQL user repository with atomic profile updates (transactions) - Mifflin-St Jeor calorie calculation based on profile data - REST API: POST /auth/login, /auth/refresh, /auth/logout, GET/PUT /profile, GET /health - Middleware: auth, CORS (localhost wildcard), logging, recovery, request_id - Unit tests (51 passing) and integration tests (testcontainers) - Docker Compose setup with postgres healthcheck and graceful shutdown Flutter client: - Riverpod state management with GoRouter navigation - Firebase Auth (email/password + Google sign-in with web popup support) - Platform-aware API URLs (web/Android/iOS) - Dio HTTP client with JWT auth interceptor and concurrent refresh handling - Secure token storage - Screens: Login, Register, Home (tabs: Menu, Recipes, Products, Profile) - Unit tests (17 passing) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
70 lines
1.7 KiB
Go
70 lines
1.7 KiB
Go
package auth
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type JWTManager struct {
|
|
secret []byte
|
|
accessDuration time.Duration
|
|
refreshDuration time.Duration
|
|
}
|
|
|
|
type Claims struct {
|
|
UserID string `json:"user_id"`
|
|
Plan string `json:"plan"`
|
|
jwt.RegisteredClaims
|
|
}
|
|
|
|
func NewJWTManager(secret string, accessDuration, refreshDuration time.Duration) *JWTManager {
|
|
return &JWTManager{
|
|
secret: []byte(secret),
|
|
accessDuration: accessDuration,
|
|
refreshDuration: refreshDuration,
|
|
}
|
|
}
|
|
|
|
func (j *JWTManager) GenerateAccessToken(userID, plan string) (string, error) {
|
|
claims := Claims{
|
|
UserID: userID,
|
|
Plan: plan,
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
ExpiresAt: jwt.NewNumericDate(time.Now().Add(j.accessDuration)),
|
|
IssuedAt: jwt.NewNumericDate(time.Now()),
|
|
},
|
|
}
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
return token.SignedString(j.secret)
|
|
}
|
|
|
|
func (j *JWTManager) GenerateRefreshToken() (string, time.Time) {
|
|
token := uuid.NewString()
|
|
expiresAt := time.Now().Add(j.refreshDuration)
|
|
return token, expiresAt
|
|
}
|
|
|
|
func (j *JWTManager) ValidateAccessToken(tokenStr string) (*Claims, error) {
|
|
token, err := jwt.ParseWithClaims(tokenStr, &Claims{}, func(t *jwt.Token) (interface{}, error) {
|
|
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
|
|
return nil, fmt.Errorf("unexpected signing method: %v", t.Header["alg"])
|
|
}
|
|
return j.secret, nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
claims, ok := token.Claims.(*Claims)
|
|
if !ok || !token.Valid {
|
|
return nil, fmt.Errorf("invalid token")
|
|
}
|
|
return claims, nil
|
|
}
|
|
|
|
func (j *JWTManager) AccessDuration() time.Duration {
|
|
return j.accessDuration
|
|
}
|