From d0c1aae1e74e845a9dc1904bcf18fbcb67c75d07 Mon Sep 17 00:00:00 2001 From: Stavros Date: Sun, 19 Jan 2025 23:00:27 +0200 Subject: [PATCH] refactor: use a hook for checking sign in status in the backend --- .gitignore | 5 ++- docker-compose.example.yml | 4 +-- internal/api/api.go | 53 +++++++++++++------------------- internal/hooks/hooks.go | 44 ++++++++++++++++++++++++++ internal/types/types.go | 5 +++ site/src/pages/continue-page.tsx | 2 +- 6 files changed, 77 insertions(+), 36 deletions(-) create mode 100644 internal/hooks/hooks.go diff --git a/.gitignore b/.gitignore index f669f17..a8d0048 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,7 @@ internal/assets/dist # binaries -tinyauth \ No newline at end of file +tinyauth + +# dev docker compose +docker-compose.dev.yml \ No newline at end of file diff --git a/docker-compose.example.yml b/docker-compose.example.yml index bf4be28..f6c553c 100644 --- a/docker-compose.example.yml +++ b/docker-compose.example.yml @@ -15,7 +15,7 @@ services: image: nginx:latest labels: traefik.enable: true - traefik.http.routers.nginx.rule: Host(`nginx.dev.local`) + traefik.http.routers.nginx.rule: Host(`nginx.example.com`) traefik.http.services.nginx.loadbalancer.server.port: 80 traefik.http.routers.nginx.middlewares: tinyauth @@ -29,5 +29,5 @@ services: - USERS=user:$$2a$$10$$UdLYoJ5lgPsC0RKqYH/jMua7zIn0g9kPqWmhYayJYLaZQ/FTmH2/u labels: traefik.enable: true - traefik.http.routers.tinyauth.rule: Host(`tinyauth.dev.local`) + traefik.http.routers.tinyauth.rule: Host(`tinyauth.example.com`) traefik.http.services.tinyauth.loadbalancer.server.port: 3000 diff --git a/internal/api/api.go b/internal/api/api.go index ae992bf..25a84ff 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -9,6 +9,7 @@ import ( "time" "tinyauth/internal/assets" "tinyauth/internal/auth" + "tinyauth/internal/hooks" "tinyauth/internal/types" "github.com/gin-contrib/sessions" @@ -52,20 +53,14 @@ func Run(config types.Config, users types.UserList) { }) router.GET("/api/auth", func (c *gin.Context) { - session := sessions.Default(c) - value := session.Get("tinyauth") + userContext := hooks.UseUserContext(c, users) - if value != nil { - usernameString, ok := value.(string) - if ok { - if auth.FindUser(users, usernameString) != nil { - c.JSON(200, gin.H{ - "status": 200, - "message": "Authorized", - }) - return - } - } + if userContext.IsLoggedIn { + c.JSON(200, gin.H{ + "status": 200, + "message": "Authenticated", + }) + return } uri := c.Request.Header.Get("X-Forwarded-Uri") @@ -139,29 +134,23 @@ func Run(config types.Config, users types.UserList) { }) router.GET("/api/status", func (c *gin.Context) { - session := sessions.Default(c) - value := session.Get("tinyauth") + userContext := hooks.UseUserContext(c, users) - if value != nil { - usernameString, ok := value.(string) - if ok { - if auth.FindUser(users, usernameString) != nil { - c.JSON(200, gin.H{ - "status": 200, - "isLoggedIn": true, - "username": usernameString, - "version": assets.Version, - }) - return - } - } - } + if !userContext.IsLoggedIn { + c.JSON(200, gin.H{ + "status": 200, + "message": "Unauthenticated", + "username": "", + "isLoggedIn": false, + }) + return + } c.JSON(200, gin.H{ "status": 200, - "isLoggedIn": false, - "username": "", - "version": assets.Version, + "message": "Authenticated", + "username": userContext.Username, + "isLoggedIn": true, }) }) diff --git a/internal/hooks/hooks.go b/internal/hooks/hooks.go new file mode 100644 index 0000000..0790eb2 --- /dev/null +++ b/internal/hooks/hooks.go @@ -0,0 +1,44 @@ +package hooks + +import ( + "tinyauth/internal/auth" + "tinyauth/internal/types" + + "github.com/gin-contrib/sessions" + "github.com/gin-gonic/gin" +) + +func UseUserContext(c *gin.Context, userList types.UserList) (types.UserContext) { + session := sessions.Default(c) + cookie := session.Get("tinyauth") + + if cookie == nil { + return types.UserContext{ + Username: "", + IsLoggedIn: false, + } + } + + username, ok := cookie.(string) + + if !ok { + return types.UserContext{ + Username: "", + IsLoggedIn: false, + } + } + + user := auth.FindUser(userList, username) + + if user == nil { + return types.UserContext{ + Username: "", + IsLoggedIn: false, + } + } + + return types.UserContext{ + Username: username, + IsLoggedIn: true, + } +} \ No newline at end of file diff --git a/internal/types/types.go b/internal/types/types.go index 871a791..dda53b3 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -25,4 +25,9 @@ type Config struct { RootURL string `validate:"required,url" mapstructure:"root-url"` AppURL string `validate:"required,url" mapstructure:"app-url"` Users string `validate:"required" mapstructure:"users"` +} + +type UserContext struct { + Username string + IsLoggedIn bool } \ No newline at end of file diff --git a/site/src/pages/continue-page.tsx b/site/src/pages/continue-page.tsx index 5e2691a..1101b9b 100644 --- a/site/src/pages/continue-page.tsx +++ b/site/src/pages/continue-page.tsx @@ -29,7 +29,7 @@ export const ContinuePage = () => { return ( - {typeof redirectUri == "string" ? ( + {redirectUri !== "null" ? ( <> Continue