aboutsummaryrefslogtreecommitdiff
path: root/pkg/ext
diff options
context:
space:
mode:
authorGabriel Arakaki Giovanini <mail@gabrielgio.me>2023-06-26 22:26:10 +0200
committerGabriel Arakaki Giovanini <mail@gabrielgio.me>2023-06-26 22:40:16 +0200
commit4d930c0c8cb585979798fac2bb254f991faa62fb (patch)
tree0c33e0e0f2a2f47b0f64843f7d9a3eb299abb260 /pkg/ext
parentd4e1ca3a48e74573df6965ceee217e119ff899ae (diff)
downloadlens-4d930c0c8cb585979798fac2bb254f991faa62fb.tar.gz
lens-4d930c0c8cb585979798fac2bb254f991faa62fb.tar.bz2
lens-4d930c0c8cb585979798fac2bb254f991faa62fb.zip
feat: Add initial user setup
Diffstat (limited to 'pkg/ext')
-rw-r--r--pkg/ext/middleware.go42
1 files changed, 41 insertions, 1 deletions
diff --git a/pkg/ext/middleware.go b/pkg/ext/middleware.go
index 771c0ac..649272e 100644
--- a/pkg/ext/middleware.go
+++ b/pkg/ext/middleware.go
@@ -4,6 +4,7 @@ import (
"encoding/base64"
"time"
+ "git.sr.ht/~gabrielgio/img/pkg/components/user"
"github.com/sirupsen/logrus"
"github.com/valyala/fasthttp"
)
@@ -54,7 +55,7 @@ func NewAuthMiddleware(key []byte, log *logrus.Entry) *AuthMiddleware {
func (a *AuthMiddleware) LoggedIn(next fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
path := string(ctx.Path())
- if path == "/login" {
+ if path == "/login" || path == "/initial" {
next(ctx)
return
}
@@ -87,3 +88,42 @@ func (a *AuthMiddleware) LoggedIn(next fasthttp.RequestHandler) fasthttp.Request
next(ctx)
}
}
+
+type InitialSetupMiddleware struct {
+ userRepository user.Repository
+}
+
+func NewInitialSetupMiddleware(userRepository user.Repository) *InitialSetupMiddleware {
+ return &InitialSetupMiddleware{
+ userRepository: userRepository,
+ }
+}
+
+func (i *InitialSetupMiddleware) Check(next fasthttp.RequestHandler) fasthttp.RequestHandler {
+ return func(ctx *fasthttp.RequestCtx) {
+ // if user has been set to context it is logged in already
+ _, ok := ctx.UserValue("token").(*Token)
+ if ok {
+ next(ctx)
+ return
+ }
+
+ path := string(ctx.Path())
+ if path == "/initial" {
+ next(ctx)
+ return
+ }
+
+ exists, err := i.userRepository.Any(ctx)
+ if err != nil {
+ InternalServerError(ctx, err)
+ return
+ }
+
+ if exists {
+ next(ctx)
+ return
+ }
+ ctx.Redirect("/initial", 307)
+ }
+}