Skip to content

Commit fb3c04d

Browse files
refactor: add middleware folder
1 parent c541293 commit fb3c04d

File tree

6 files changed

+70
-61
lines changed

6 files changed

+70
-61
lines changed

web/controllers/auth.go

+3-52
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ import (
88
"github.com/gofiber/fiber/v2/middleware/session"
99
)
1010

11-
func loginData(flash string) fiber.Map {
12-
return fiber.Map{
13-
"Flash": flash,
14-
}
15-
}
16-
1711
func Login(c *fiber.Ctx, cc core.Context) error {
1812
return h.Render(c, "login", fiber.Map{})
1913
}
@@ -60,51 +54,8 @@ func Logout(c *fiber.Ctx, cc core.Context, store *session.Store) error {
6054
return h.Redirect(c, "/login")
6155
}
6256

63-
func UserRequire(c *fiber.Ctx, cc core.Context, store *session.Store) error {
64-
if procs.AuthHTTPAnonymous(cc) {
65-
return NotFound(c)
66-
}
67-
68-
return authRequire(c, cc, store)
69-
}
70-
71-
func AuthRequire(c *fiber.Ctx, cc core.Context, store *session.Store) error {
72-
if procs.AuthHTTPAnonymous(cc) {
73-
c.Locals(h.AnonymousContextKey, true)
74-
return c.Next()
75-
}
76-
77-
return authRequire(c, cc, store)
78-
}
79-
80-
func authRequire(c *fiber.Ctx, cc core.Context, store *session.Store) error {
81-
sess, err := store.Get(c)
82-
if err != nil {
83-
panic(err)
84-
}
85-
86-
auth := sess.Get("auth")
87-
if auth == nil {
88-
return h.Redirect(c, "/login")
89-
}
90-
91-
return c.Next()
92-
}
93-
94-
func AuthRestrict(c *fiber.Ctx, cc core.Context, store *session.Store) error {
95-
if procs.AuthHTTPAnonymous(cc) {
96-
return h.Redirect(c, "/")
97-
}
98-
99-
sess, err := store.Get(c)
100-
if err != nil {
101-
return h.Error(c, err)
102-
}
103-
104-
auth := sess.Get("auth")
105-
if auth != nil {
106-
return h.Redirect(c, "/")
57+
func loginData(flash string) fiber.Map {
58+
return fiber.Map{
59+
"Flash": flash,
10760
}
108-
109-
return c.Next()
11061
}

web/controllers/controllers.go

-4
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,3 @@ func Trim(c *fiber.Ctx, cc core.Context) error {
117117
func SomethingWentWrong(c *fiber.Ctx) error {
118118
return h.Render(c, "something-went-wrong", fiber.Map{"Error": ""})
119119
}
120-
121-
func NotFound(c *fiber.Ctx) error {
122-
return h.Render(c.Status(404), "404", fiber.Map{})
123-
}

web/helpers/http.go

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ func Error(c *fiber.Ctx, err error, codes ...int) error {
2929
return Render(c, "something-went-wrong", fiber.Map{"Error": err})
3030
}
3131

32+
func NotFound(c *fiber.Ctx) error {
33+
return Render(c.Status(404), "404", fiber.Map{})
34+
}
35+
3236
type Meta struct {
3337
Anonymous bool
3438
CSRF string

web/http/http.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/ItsNotGoodName/smtpbridge/internal/core"
88
"github.com/ItsNotGoodName/smtpbridge/web"
9-
"github.com/ItsNotGoodName/smtpbridge/web/controllers"
109
h "github.com/ItsNotGoodName/smtpbridge/web/helpers"
1110
"github.com/gofiber/fiber/v2"
1211
"github.com/gofiber/fiber/v2/middleware/csrf"
@@ -56,7 +55,7 @@ func New(app core.App, shutdown context.CancelFunc, address string, bodyLimit in
5655
)
5756

5857
web.UseAssets(http)
59-
http.Use(controllers.NotFound)
58+
http.Use(h.NotFound)
6059

6160
return HTTP{
6261
http: http,

web/http/routes.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import (
44
"github.com/ItsNotGoodName/smtpbridge/internal/core"
55
"github.com/ItsNotGoodName/smtpbridge/web/controllers"
66
"github.com/ItsNotGoodName/smtpbridge/web/inject"
7+
"github.com/ItsNotGoodName/smtpbridge/web/middleware"
78
"github.com/gofiber/fiber/v2"
89
"github.com/gofiber/fiber/v2/middleware/session"
910
)
1011

1112
func route(app core.App, store *session.Store, http fiber.Router) {
12-
userRequire := inject.AppStore(app, store, controllers.UserRequire)
13-
authRequire := inject.AppStore(app, store, controllers.AuthRequire)
14-
authSkip := inject.AppStore(app, store, controllers.AuthRestrict)
13+
userRequire := inject.AppStore(app, store, middleware.UserRequire)
14+
authRequire := inject.AppStore(app, store, middleware.AuthRequire)
15+
authSkip := inject.AppStore(app, store, middleware.AuthRestrict)
1516

1617
http.Get("/login", authSkip, inject.App(app, controllers.Login))
1718
http.Post("/login", authSkip, inject.AppStore(app, store, controllers.LoginPost))

web/middleware/auth.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package middleware
2+
3+
import (
4+
"github.com/ItsNotGoodName/smtpbridge/internal/core"
5+
"github.com/ItsNotGoodName/smtpbridge/internal/procs"
6+
h "github.com/ItsNotGoodName/smtpbridge/web/helpers"
7+
"github.com/gofiber/fiber/v2"
8+
"github.com/gofiber/fiber/v2/middleware/session"
9+
)
10+
11+
func UserRequire(c *fiber.Ctx, cc core.Context, store *session.Store) error {
12+
if procs.AuthHTTPAnonymous(cc) {
13+
return h.NotFound(c)
14+
}
15+
16+
return authRequire(c, cc, store)
17+
}
18+
19+
func AuthRequire(c *fiber.Ctx, cc core.Context, store *session.Store) error {
20+
if procs.AuthHTTPAnonymous(cc) {
21+
c.Locals(h.AnonymousContextKey, true)
22+
return c.Next()
23+
}
24+
25+
return authRequire(c, cc, store)
26+
}
27+
28+
func authRequire(c *fiber.Ctx, cc core.Context, store *session.Store) error {
29+
sess, err := store.Get(c)
30+
if err != nil {
31+
panic(err)
32+
}
33+
34+
auth := sess.Get("auth")
35+
if auth == nil {
36+
return h.Redirect(c, "/login")
37+
}
38+
39+
return c.Next()
40+
}
41+
42+
func AuthRestrict(c *fiber.Ctx, cc core.Context, store *session.Store) error {
43+
if procs.AuthHTTPAnonymous(cc) {
44+
return h.Redirect(c, "/")
45+
}
46+
47+
sess, err := store.Get(c)
48+
if err != nil {
49+
return h.Error(c, err)
50+
}
51+
52+
auth := sess.Get("auth")
53+
if auth != nil {
54+
return h.Redirect(c, "/")
55+
}
56+
57+
return c.Next()
58+
}

0 commit comments

Comments
 (0)