feat(auth): Implement database-backed user authentication and
persistence This commit introduces a major architectural change by replacing the previous hardcoded user and data system with a robust, database-driven approach. It lays the foundation for persistent data storage for all application models. Key Changes: - **Database Integration:** - Added `GORM` and `SQLite` for the database layer. - The database is now initialized and migrated on application startup. - Configuration is managed through environment variables (`DATABASE`, `USERS_FILE`). - **User Authentication & Management:** - The login endpoint (`/api/token`) now authenticates users against the database. - JWT claims are now populated with real user data (`id`, `username`, `is_superuser`). - Implemented a system to seed the database with initial users from a `users.json` file. - **New User API Endpoints:** - `GET /api/users`: Retrieves a list of all users (superuser access required). - `GET /api/users/user`: Fetches the data for the currently authenticated user based on their JWT. - **Data Model Overhaul:** - Refactored all data models (`User`, `Category`, `Operation`, `Subcategory`) to include GORM tags, relationships, and a `BaseModel` with auto-generated UUIDs for primary keys. - Separated user-related structs into `User` (database model), `AuthRequest` (login payload), and `UserResponse` (safe API response). - **Middleware & Routing:** - Improved JWT error handling for clearer client-side messages. - Added a new middleware to allow passing the JWT in the request body for more flexible client integration.
This commit is contained in:
1 parent
c6ed1d66b0
commit
599142af80
23 files changed
+458
-108
No files matched your search
@@ -0,0 +1,18 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type BaseModel struct {
|
||||
ID string `json:"id" gorm:"primaryKey"`
|
||||
}
|
||||
|
||||
func (base *BaseModel) BeforeCreate(tx *gorm.DB) (err error) {
|
||||
// generate new uuid if ID not created yet
|
||||
if base.ID == "" {
|
||||
base.ID = uuid.New().String()
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
package model
|
||||
|
||||
type Category struct {
|
||||
ID string `json:"id"`
|
||||
BaseModel
|
||||
Icon string `json:"icon"`
|
||||
Title string `json:"title"`
|
||||
Subcategories []Subcategory `json:"subcategories"`
|
||||
Subcategories []Subcategory `json:"subcategories" gorm:"foreignKey:CategoryID"`
|
||||
Type string `json:"type"`
|
||||
UserID string
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package model
|
||||
|
||||
type Operation struct {
|
||||
BaseModel
|
||||
Title string `json:"title"`
|
||||
Amount float64 `json:"amount"`
|
||||
Note string `json:"note,omitempty"`
|
||||
Icon string `json:"icon"`
|
||||
Type string `json:"type"`
|
||||
Date string `json:"date"`
|
||||
UserID string
|
||||
CategoryID string
|
||||
SubcategoryID string
|
||||
Category Category `gorm:"foreignKey:CategoryID"`
|
||||
Subcategory Subcategory `gorm:"foreignKey:SubcategoryID"`
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package model
|
||||
|
||||
type Subcategory struct {
|
||||
BaseModel
|
||||
Title string `json:"title"`
|
||||
CategoryID string
|
||||
UserID string
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package model
|
||||
|
||||
type TokenRequest struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package model
|
||||
|
||||
type User struct {
|
||||
BaseModel
|
||||
Username string `json:"username" gorm:"unique"`
|
||||
Email string `json:"email" gorm:"unique"`
|
||||
Password string `json:"password"`
|
||||
Group string `json:"group"`
|
||||
IsSuperuser bool `json:"is_superuser"`
|
||||
Categories []Category `json:"categories" gorm:"foreignKey:UserID"`
|
||||
Subcategories []Subcategory `json:"subcategories" gorm:"foreignKey:UserID"`
|
||||
Operations []Operation `json:"operations" gorm:"foreignKey:UserID"`
|
||||
}
|
||||
|
||||
type AuthRequest struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type UserResponse struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Email string `json:"email"`
|
||||
Group string `json:"group"`
|
||||
IsSuperuser bool `json:"is_superuser"`
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package model
|
||||
|
||||
type Operation struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Amount float64 `json:"amount"`
|
||||
Category string `json:"category"`
|
||||
SubCategory Subcategory `json:"subcategory,omitzero"`
|
||||
Note string `json:"note,omitempty"`
|
||||
Icon string `json:"icon"`
|
||||
Type string `json:"type"`
|
||||
Date string `json:"date"`
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package model
|
||||
|
||||
type Subcategory struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package model
|
||||
|
||||
type AuthRequest struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Email string `json:"email"`
|
||||
Group string `json:"group"`
|
||||
IsSuperuser bool `json:"is_superuser"`
|
||||
}
|
||||
Reference in new issue
Block a user