feat(auth): implement JWT authentication flow
This commit implements a full-stack authentication system using JWT. On the backend, a new login endpoint `/api/token` is created. It validates credentials and issues a signed JWT. A JWT middleware is added to the server configuration to protect sensitive API endpoints like `/api/category` and `/api/operation`. Configuration is now managed via a `wallet.conf` file, loaded at startup. On the frontend, this enables a complete authentication lifecycle: - A new `/login` page allows users to enter their credentials. - A `PrivateRoute` component wraps protected pages, redirecting unauthenticated users to the login page. - API calls to protected resources now include the `Authorization: Bearer <token>` header. - A logout function is added to clear user session data. refactor(ui): enhance history and category views This commit also introduces major UI improvements. The History modal has been completely redesigned. Operations are no longer displayed as a flat list but are now grouped into tabs for Daily, Weekly, Monthly, and Yearly views, providing a much clearer overview of transactions. The Categories modal now separates categories into "Incomes" and "Expenses" tabs, reflecting the changes in the backend data model.
This commit is contained in:
1 parent
ca99cfcf6d
commit
1b6c7211ea
35 files changed
+789
-674
No files matched your search
@@ -0,0 +1,17 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/golang-jwt/jwt"
|
||||
)
|
||||
|
||||
func Accessible(c *fiber.Ctx) error {
|
||||
return c.SendString("Accessible")
|
||||
}
|
||||
|
||||
func Restricted(c *fiber.Ctx) error {
|
||||
user := c.Locals("user").(*jwt.Token)
|
||||
claims := user.Claims.(jwt.MapClaims)
|
||||
name := claims["name"].(string)
|
||||
return c.SendString("Welcome " + name)
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"backend/model"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/golang-jwt/jwt"
|
||||
)
|
||||
|
||||
func Login(c *fiber.Ctx) error {
|
||||
var requestdata model.AuthRequest
|
||||
err := c.BodyParser(&requestdata)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error parse body, %s", err)
|
||||
}
|
||||
if requestdata.Username != "tola" || requestdata.Password != "2808" {
|
||||
return c.SendStatus(fiber.StatusUnauthorized)
|
||||
}
|
||||
|
||||
claims := jwt.MapClaims{
|
||||
"username": "tola",
|
||||
"is_superuser": true,
|
||||
"exp": time.Now().Add(time.Hour * 72).Unix(),
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
|
||||
t, err := token.SignedString([]byte(os.Getenv("JWT_SECRET_KEY")))
|
||||
if err != nil {
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
}
|
||||
return c.JSON(fiber.Map{"token": t})
|
||||
}
|
||||
Reference in new issue
Block a user