Skip to content

Commit 54ccf08

Browse files
authored
Use HTTP verbs for authentication routes
2 parents bef65e9 + dd1d7d4 commit 54ccf08

File tree

8 files changed

+15
-16
lines changed

8 files changed

+15
-16
lines changed

internal/webserver/authentication_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestAuthentication(t *testing.T) {
2929

3030
t.Run("Try to log in with good and bad credentials", func(t *testing.T) {
3131
// Check that login page is accessible
32-
req, err := http.NewRequest(http.MethodGet, "/en/login", nil)
32+
req, err := http.NewRequest(http.MethodGet, "/en/sessions/new", nil)
3333
if err != nil {
3434
t.Fatalf("Unexpected error: %v", err.Error())
3535
}
@@ -42,7 +42,7 @@ func TestAuthentication(t *testing.T) {
4242
}
4343

4444
// Use no credentials to log in
45-
req, err = http.NewRequest(http.MethodPost, "/en/login", nil)
45+
req, err = http.NewRequest(http.MethodPost, "/en/sessions", nil)
4646
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
4747
if err != nil {
4848
t.Fatalf("Unexpected error: %v", err.Error())
@@ -56,7 +56,7 @@ func TestAuthentication(t *testing.T) {
5656
}
5757

5858
// Use good credentials to log in
59-
req, err = http.NewRequest(http.MethodPost, "/en/login", strings.NewReader(data.Encode()))
59+
req, err = http.NewRequest(http.MethodPost, "/en/sessions", strings.NewReader(data.Encode()))
6060
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
6161
if err != nil {
6262
t.Fatalf("Unexpected error: %v", err.Error())
@@ -267,7 +267,7 @@ func TestRecover(t *testing.T) {
267267
t.Error("No location header present")
268268
return
269269
}
270-
if expectedURL := "/en/login"; url.Path != expectedURL {
270+
if expectedURL := "/en/sessions"; url.Path != expectedURL {
271271
t.Errorf("Expected location %s, received %s", expectedURL, url.Path)
272272
}
273273

internal/webserver/controller/auth/login.go

+1
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ func (a *Controller) Login(c *fiber.Ctx) error {
2929
"Title": "Login",
3030
"Message": msg,
3131
"EmailSendingConfigured": emailSendingConfigured,
32+
"DisableLoginLink": true,
3233
}, "layout")
3334
}

internal/webserver/controller/auth/reset-password.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (a *Controller) UpdatePassword(c *fiber.Ctx) error {
4545
return fiber.ErrInternalServerError
4646
}
4747

48-
return c.Redirect(fmt.Sprintf("/%s/login", c.Params("lang")))
48+
return c.Redirect(fmt.Sprintf("/%s/sessions", c.Params("lang")))
4949
}
5050

5151
func (a *Controller) validateRecoveryAccess(recoveryUuid string) (*model.User, error) {

internal/webserver/controller/auth/signout.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package auth
22

33
import (
4-
"fmt"
5-
64
"github.com/gofiber/fiber/v2"
75
)
86

@@ -16,6 +14,6 @@ func (a *Controller) SignOut(c *fiber.Ctx) error {
1614
Secure: false,
1715
HTTPOnly: true,
1816
})
19-
20-
return c.Redirect(fmt.Sprintf("/%s", c.Params("lang")))
17+
c.Set("HX-Refresh", "true")
18+
return c.SendStatus(fiber.StatusNoContent)
2119
}

internal/webserver/embedded/views/auth/login.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<form method="post" action="/{{.Lang}}/login">
1+
<form method="post" action="/{{.Lang}}/sessions">
22
<h2 class="h3 mb-3 fw-normal">{{t .Lang "Please sign in"}}</h2>
33

44
<div class="form-floating">

internal/webserver/embedded/views/layout.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ <h5 class="offcanvas-title" id="offcanvasNavbarLabel">Coreander</h5>
9494
<hr class="dropdown-divider">
9595
</li>
9696
<li>
97-
<a class="dropdown-item" href="/{{.Lang}}/logout">
97+
<a class="dropdown-item" href="/{{.Lang}}/sessions" hx-delete="/{{.Lang}}/sessions">
9898
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"
9999
fill="currentColor" class="bi bi-box-arrow-right" viewBox="0 0 16 16">
100100
<path fill-rule="evenodd"
@@ -109,7 +109,7 @@ <h5 class="offcanvas-title" id="offcanvasNavbarLabel">Coreander</h5>
109109
</li>
110110
{{else if not .DisableLoginLink}}
111111
<li class="p-2">
112-
<a href="/{{.Lang}}/login">{{t .Lang "Login"}}</a>
112+
<a href="/{{.Lang}}/sessions/new">{{t .Lang "Login"}}</a>
113113
</li>
114114
{{end}}
115115
<hr class="d-lg-none">

internal/webserver/routes.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ func routes(app *fiber.App, controllers Controllers, jwtSecret []byte, sender Se
4444
return c.Next()
4545
})
4646

47-
langGroup.Get("/login", allowIfNotLoggedIn, controllers.Auth.Login)
48-
langGroup.Post("login", allowIfNotLoggedIn, controllers.Auth.SignIn)
47+
langGroup.Get("/sessions/new", allowIfNotLoggedIn, controllers.Auth.Login)
48+
langGroup.Post("/sessions", allowIfNotLoggedIn, controllers.Auth.SignIn)
4949
langGroup.Get("/recover", allowIfNotLoggedIn, controllers.Auth.Recover)
5050
langGroup.Post("/recover", allowIfNotLoggedIn, controllers.Auth.Request)
5151
langGroup.Get("/reset-password", allowIfNotLoggedIn, controllers.Auth.EditPassword)
5252
langGroup.Post("/reset-password", allowIfNotLoggedIn, controllers.Auth.UpdatePassword)
53-
langGroup.Get("/logout", alwaysRequireAuthentication, controllers.Auth.SignOut)
53+
langGroup.Delete("/sessions", alwaysRequireAuthentication, controllers.Auth.SignOut)
5454

5555
usersGroup := langGroup.Group("/users", alwaysRequireAuthentication)
5656

internal/webserver/user_management_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ func login(app *fiber.App, email, password string, t *testing.T) (*http.Cookie,
428428
"password": {password},
429429
}
430430

431-
req, err := http.NewRequest(http.MethodPost, "/en/login", strings.NewReader(data.Encode()))
431+
req, err := http.NewRequest(http.MethodPost, "/en/sessions", strings.NewReader(data.Encode()))
432432
if err != nil {
433433
return nil, err
434434
}

0 commit comments

Comments
 (0)