feat(auth): Implement database-backed user authentication and
Build and Test / build-and-test-backend (push) Successful in 40s
Build and Test / build-and-test-frontend (push) Successful in 36s

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:
wt committed 2025-08-08 23:47:36 +07:00
1 parent c6ed1d66b0
commit 599142af80
23 files changed
+458 -108

No files matched your search

@@ -5,41 +5,34 @@ import "backend/model"
func GetAllCategories() ([]model.Category, error) {
return []model.Category{
{
ID: "1",
Title: "Electronics",
Icon: "😀",
Type: "expense",
},
{
ID: "2",
Title: "Clothing",
Icon: "😁",
Type: "expense",
},
{
ID: "3",
Title: "Books",
Icon: "😂",
Subcategories: []model.Subcategory{
{
ID: "1",
Title: "Fiction",
},
{
ID: "2",
Title: "Non-Fiction",
},
},
Type: "expense",
},
{
ID: "4",
Title: "Salary",
Icon: "😀",
Type: "income",
},
{
ID: "5",
Title: "asdf",
Icon: "😀",
Type: "income",
@@ -0,0 +1,36 @@
package internaljwt
import (
"backend/model"
"github.com/gofiber/fiber/v2"
)
func JwtFromBody(c *fiber.Ctx) error {
req := new(model.TokenRequest)
if err := c.BodyParser(req); err != nil {
return c.Status(
fiber.StatusBadRequest,
).JSON(
fiber.Map{
"error": "Cannot parse request body",
},
)
}
if req.Token == "" {
return c.Status(
fiber.StatusBadRequest,
).JSON(
fiber.Map{
"error": "Token not found in request body",
},
)
}
c.Request().Header.Set(
"Authorization", "Bearer "+req.Token,
)
return c.Next()
}
+50 -40
View File
@@ -8,12 +8,13 @@ import (
func GetAllOperation() ([]model.Operation, error) {
return []model.Operation{
{
ID: "1",
Title: "Operation 1",
Amount: 100,
Category: "Category 1",
SubCategory: model.Subcategory{
ID: "1",
Title: "Operation 1",
Amount: 100,
Category: model.Category{
Title: "category 1",
Type: "income",
},
Subcategory: model.Subcategory{
Title: "subcategory",
},
Date: "2022-01-01T00:00:00Z",
@@ -21,30 +22,35 @@ func GetAllOperation() ([]model.Operation, error) {
Icon: "😀",
},
{
ID: "2",
Title: "Operation 2",
Amount: 200,
Category: "Category 2",
Date: "2022-01-02T00:00:00Z",
Type: "expense",
Icon: "😂",
Title: "Operation 2",
Amount: 200,
Category: model.Category{
Title: "Category 2",
Type: "expense",
},
Date: "2022-01-02T00:00:00Z",
Type: "expense",
Icon: "😂",
},
{
ID: "2",
Title: "Operation 2",
Amount: 200,
Category: "Category 2",
Date: time.Now().Format(time.RFC3339),
Type: "expense",
Icon: "😂",
Title: "Operation 2",
Amount: 200,
Category: model.Category{
Title: "Category 2",
Type: "expense",
},
Date: time.Now().Format(time.RFC3339),
Type: "expense",
Icon: "😂",
},
{
ID: "1",
Title: "Operation 1",
Amount: 100,
Category: "Category 1",
SubCategory: model.Subcategory{
ID: "1",
Title: "Operation 1",
Amount: 100,
Category: model.Category{
Title: "Category 2",
Type: "expense",
},
Subcategory: model.Subcategory{
Title: "subcategory",
},
Date: "2022-01-02T00:00:00Z",
@@ -52,22 +58,26 @@ func GetAllOperation() ([]model.Operation, error) {
Icon: "😀",
},
{
ID: "2",
Title: "Operation 2",
Amount: 200,
Category: "Category 2",
Date: "2022-06-02T00:00:00Z",
Type: "expense",
Icon: "😂",
Title: "Operation 2",
Amount: 200,
Category: model.Category{
Title: "Category 2",
Type: "expense",
},
Date: "2022-06-02T00:00:00Z",
Type: "expense",
Icon: "😂",
},
{
ID: "2",
Title: "Operation 2",
Amount: 200,
Category: "Category 2",
Date: "2022-06-22T00:00:00Z",
Type: "expense",
Icon: "😂",
Title: "Operation 2",
Amount: 200,
Category: model.Category{
Title: "Category 2",
Type: "expense",
},
Date: "2022-06-22T00:00:00Z",
Type: "expense",
Icon: "😂",
},
}, nil
}