Skip to content

Commit 44101c8

Browse files
refactor: auth endpoint name
1 parent 9e5a98b commit 44101c8

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

web/controllers/auth.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func Login(c *fiber.Ctx, cc core.Context) error {
1616
return c.Render("login", LoginData{})
1717
}
1818

19-
func AuthLogin(c *fiber.Ctx, cc core.Context, store *session.Store) error {
19+
func LoginPost(c *fiber.Ctx, cc core.Context, store *session.Store) error {
2020
// Request
2121
username := c.FormValue("username")
2222
password := c.FormValue("password")
@@ -44,7 +44,7 @@ func AuthLogin(c *fiber.Ctx, cc core.Context, store *session.Store) error {
4444
return helpers.Redirect(c, "/")
4545
}
4646

47-
func AuthLogout(c *fiber.Ctx, cc core.Context, store *session.Store) error {
47+
func Logout(c *fiber.Ctx, cc core.Context, store *session.Store) error {
4848
sess, err := store.Get(c)
4949
if err != nil {
5050
return helpers.Error(c, err)
@@ -58,11 +58,23 @@ func AuthLogout(c *fiber.Ctx, cc core.Context, store *session.Store) error {
5858
return helpers.Redirect(c, "/login")
5959
}
6060

61+
func UserRequire(c *fiber.Ctx, cc core.Context, store *session.Store) error {
62+
if procs.AuthHTTPAnonymous(cc) {
63+
return helpers.NotFound(c)
64+
}
65+
66+
return authRequire(c, cc, store)
67+
}
68+
6169
func AuthRequire(c *fiber.Ctx, cc core.Context, store *session.Store) error {
6270
if procs.AuthHTTPAnonymous(cc) {
6371
return c.Next()
6472
}
6573

74+
return authRequire(c, cc, store)
75+
}
76+
77+
func authRequire(c *fiber.Ctx, cc core.Context, store *session.Store) error {
6678
sess, err := store.Get(c)
6779
if err != nil {
6880
panic(err)
@@ -76,7 +88,7 @@ func AuthRequire(c *fiber.Ctx, cc core.Context, store *session.Store) error {
7688
return c.Next()
7789
}
7890

79-
func AuthSkip(c *fiber.Ctx, cc core.Context, store *session.Store) error {
91+
func AuthRestrict(c *fiber.Ctx, cc core.Context, store *session.Store) error {
8092
if procs.AuthHTTPAnonymous(cc) {
8193
return helpers.Redirect(c, "/")
8294
}

web/http/routes.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import (
99
)
1010

1111
func route(app core.App, store *session.Store, http fiber.Router) {
12-
authSkip := inject.AppStore(app, store, controllers.AuthSkip)
12+
userRequire := inject.AppStore(app, store, controllers.UserRequire)
1313
authRequire := inject.AppStore(app, store, controllers.AuthRequire)
14+
authSkip := inject.AppStore(app, store, controllers.AuthRestrict)
1415

1516
http.Get("/login", authSkip, inject.App(app, controllers.Login))
16-
http.Post("/auth", authSkip, inject.AppStore(app, store, controllers.AuthLogin))
17-
http.Delete("/auth", authRequire, inject.AppStore(app, store, controllers.AuthLogout))
17+
http.Post("/login", authSkip, inject.AppStore(app, store, controllers.LoginPost))
18+
http.Post("/logout", userRequire, inject.AppStore(app, store, controllers.Logout))
1819

1920
http.Get("/", authRequire, inject.App(app, controllers.Index))
2021
http.Route("/index", func(http fiber.Router) {

web/views/login.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ <h1>SMTPBridge</h1>
88
<article class="max-w-md mx-auto">
99
<h2>Login</h2>
1010
{{block "form" .}}
11-
<form method="post" action="/auth" hx-post="/auth" hx-swap="outerHTML">
11+
<form method="post" action="/login" hx-post="/login" hx-swap="outerHTML">
1212
<label for="username">
1313
Username
1414
<input type="text" id="username" name="username" placeholder="Username" />
@@ -19,7 +19,7 @@ <h2>Login</h2>
1919
<input type="password" id="password" name="password" placeholder="Password" />
2020
</label>
2121

22-
<button data-loading-path="/auth" data-loading-disable data-loading-aria-busy>
22+
<button data-loading-path="/login" data-loading-disable data-loading-aria-busy>
2323
Log in
2424
</button>
2525
{{with .Flash}}

web/views/partials/header.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
</a>
4545
</li>
4646
<li>
47-
<a href="#" class="contrast" hx-delete="/auth">
47+
<a href="#" class="contrast" hx-post="/logout">
4848
<div class="flex flex-items-center flex-row-reverse gap-2">
4949
<div class="i-ri-logout-box-r-fill"></div>Log out
5050
</div>
@@ -84,7 +84,7 @@
8484
</a>
8585
</li>
8686
<li>
87-
<a href="#" class="contrast" hx-delete="/auth">
87+
<a href="#" class="contrast" hx-post="/logout">
8888
<div class="flex flex-items-center gap-2">
8989
<div class="i-ri-logout-box-r-fill"></div>Log out
9090
</div>

0 commit comments

Comments
 (0)