Skip to content

Commit

Permalink
feat: add way to login from mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
believer committed Sep 27, 2023
1 parent 61a0215 commit f30bb68
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
32 changes: 32 additions & 0 deletions handlers/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"believer/movies/db"
"believer/movies/types"
"believer/movies/utils"
"encoding/base64"
"os"
"strconv"
"time"

"github.com/gofiber/fiber/v2"
)
Expand Down Expand Up @@ -40,3 +43,32 @@ LIMIT 20
"NextPage": page + 1,
})
}

func HandleGetLogin(c *fiber.Ctx) error {
return c.Render("login", fiber.Map{})
}

func HandlePostLogin(c *fiber.Ctx) error {
data := new(struct {
Password string `form:"password"`
Username string `form:"username"`
})

if err := c.BodyParser(data); err != nil {
return err
}

encoded := base64.StdEncoding.EncodeToString([]byte(data.Username + ":" + data.Password))

if encoded == os.Getenv("ADMIN_SECRET") {
c.Cookie(&fiber.Cookie{
Name: "admin_secret",
Value: encoded,
Expires: time.Now().AddDate(0, 0, 30),
})
}

c.Set("HX-Redirect", "/")

return c.SendStatus(200)
}
2 changes: 2 additions & 0 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
func SetupRoutes(app *fiber.App) {
app.Get("/health", handlers.HandleHealthCheck)
app.Get("/", handlers.HandleFeed)
app.Get("/login", handlers.HandleGetLogin)
app.Post("/login", handlers.HandlePostLogin)

// Movies
// --------------------------
Expand Down
41 changes: 41 additions & 0 deletions views/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<form hx-post="/login" class="flex flex-col gap-6 max-w-xl px-4 py-8 mx-auto">
<div class="flex flex-col gap-2 relative">
<label
for="username"
class="text-sm font-semibold text-neutral-500 dark:text-neutral-400"
>
Username
</label>
<input
required
type="text"
name="username"
id="username"
class="w-full rounded border border-neutral-200 dark:border-neutral-700 bg-transparent ring-offset-white dark:ring-offset-neutral-900 px-4 py-2 ring-offset-2 focus:outline-none focus:ring-2 focus:ring-neutral-400 dark:focus:ring-neutral-500"
/>
</div>

<div class="flex flex-col gap-2 relative">
<label
for="password"
class="text-sm font-semibold text-neutral-500 dark:text-neutral-400"
>
Password
</label>
<input
required
type="password"
name="password"
id="password"
class="w-full rounded border border-neutral-200 dark:border-neutral-700 bg-transparent ring-offset-white dark:ring-offset-neutral-900 px-4 py-2 ring-offset-2 focus:outline-none focus:ring-2 focus:ring-neutral-400 dark:focus:ring-neutral-500"
/>
</div>
<footer>
<button
class="px-6 py-2 text-neutral-700 bg-neutral-200 dark:text-neutral-200 dark:bg-neutral-700 rounded"
type="submit"
>
Login
</button>
</footer>
</form>

0 comments on commit f30bb68

Please sign in to comment.