Skip to content

Commit

Permalink
wip: auth
Browse files Browse the repository at this point in the history
  • Loading branch information
kobamkode committed Sep 23, 2024
1 parent 8c0e11c commit ef4b8e5
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 54 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
APP_NAME=Terigu
APP_PORT=3000
APP_ENV=local

DB_HOST=localhost
DB_PORT=5432
Expand Down
10 changes: 6 additions & 4 deletions gen/migrations/000001_create_users.up.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
CREATE TABLE public.users (
id bigserial NOT NULL,
"name" varchar NOT NULL,
CONSTRAINT users_pk PRIMARY KEY (id)
CREATE TABLE users
(
id bigserial NOT NULL,
username varchar NOT NULL,
password varchar NOT NULL,
PRIMARY KEY (id)
);
File renamed without changes.
21 changes: 20 additions & 1 deletion internal/handlers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,27 @@ import (
)

func LoginPage(c *fiber.Ctx) error {
return c.Render("login", fiber.Map{
return c.Render("auth", fiber.Map{
"AppName": os.Getenv("APP_NAME"),
"Title": "Login",
}, "layouts/auth")
}

func Login(c *fiber.Ctx) error {
usr := c.FormValue("username")
pwd := c.FormValue("password")

if usr != "test" && pwd != "123456" {
return c.SendStatus(fiber.StatusUnauthorized)
}

return nil
}

func Logout(c *fiber.Ctx) error {
return nil
}

func Register(c *fiber.Ctx) error {
return nil
}
2 changes: 1 addition & 1 deletion internal/queries/users.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- name: CreateUser :exec
insert into users (name) values ($1);
insert into users (username) values ($1);

-- name: ListUsers :many
select * from users;
16 changes: 10 additions & 6 deletions internal/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package routes

import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/csrf"
// "github.com/gofiber/fiber/v2/middleware/csrf"
"github.com/gofiber/fiber/v2/middleware/limiter"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/requestid"
"github.com/gofiber/fiber/v2/middleware/session"
// "github.com/gofiber/fiber/v2/middleware/session"
"github.com/gofiber/storage/redis/v3"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/kobamkode/terigu/config"
Expand All @@ -19,19 +19,23 @@ func Setup(app *fiber.App, db *pgxpool.Pool, store *redis.Storage) {
app.Static("/admin/assets", "./views/assets")

// Middlewares
s := session.New(config.Session(store))
app.Use(csrf.New(config.Csrf(s)))
// s := session.New(config.Session(store))
// app.Use(csrf.New(config.Csrf(s)))
app.Use(logger.New(config.Logger()))
app.Use(requestid.New())
app.Use(limiter.New())
if config.Get("APP_ENV") != "local" {
app.Use(limiter.New())
}

// Web Routes
app.Get("/", handlers.HomePage)

// API Routes
api := app.Group("/api")

api.Get("/ping", handlers.Ping)
api.Post("/login", handlers.Login)
api.Post("/logout", handlers.Logout)
api.Post("/register", handlers.Register)

// Admin Routes
admin := app.Group("/admin")
Expand Down
4 changes: 0 additions & 4 deletions views/assets/css/dist.css
Original file line number Diff line number Diff line change
Expand Up @@ -2360,10 +2360,6 @@ html:has(.drawer-toggle:checked) {
align-items: center;
}

.justify-center {
justify-content: center;
}

.gap-2 {
gap: 0.5rem;
}
Expand Down
3 changes: 3 additions & 0 deletions views/auth.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="my-4">
{{template "components/login-card"}}
</div>
2 changes: 1 addition & 1 deletion views/login.html → views/components/login-card.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="card-body items-center text-center">
<h2 class="card-title">{{.Title}}</h2>

<form hx-post="/admin/login" hx-target="#form-login" hx-swap="outerHTML">
<form hx-post="/api/login">
<div class="form-control my-4">
<label class="input input-bordered flex items-center gap-2">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="currentColor"
Expand Down
4 changes: 2 additions & 2 deletions views/layouts/auth.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
</head>

<body>
<div class="flex min-h-screen justify-center items-center bg-slate-200">
{{embed}}
<div class="flex">
<div>{{embed}}</div>
</div>
</body>

Expand Down
35 changes: 0 additions & 35 deletions views/register.html

This file was deleted.

0 comments on commit ef4b8e5

Please sign in to comment.