refactor: use a hook for checking sign in status in the backend

This commit is contained in:
Stavros
2025-01-19 23:00:27 +02:00
parent b8a134ed12
commit d0c1aae1e7
6 changed files with 77 additions and 36 deletions

5
.gitignore vendored
View File

@@ -2,4 +2,7 @@
internal/assets/dist internal/assets/dist
# binaries # binaries
tinyauth tinyauth
# dev docker compose
docker-compose.dev.yml

View File

@@ -15,7 +15,7 @@ services:
image: nginx:latest image: nginx:latest
labels: labels:
traefik.enable: true 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.services.nginx.loadbalancer.server.port: 80
traefik.http.routers.nginx.middlewares: tinyauth traefik.http.routers.nginx.middlewares: tinyauth
@@ -29,5 +29,5 @@ services:
- USERS=user:$$2a$$10$$UdLYoJ5lgPsC0RKqYH/jMua7zIn0g9kPqWmhYayJYLaZQ/FTmH2/u - USERS=user:$$2a$$10$$UdLYoJ5lgPsC0RKqYH/jMua7zIn0g9kPqWmhYayJYLaZQ/FTmH2/u
labels: labels:
traefik.enable: true 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 traefik.http.services.tinyauth.loadbalancer.server.port: 3000

View File

@@ -9,6 +9,7 @@ import (
"time" "time"
"tinyauth/internal/assets" "tinyauth/internal/assets"
"tinyauth/internal/auth" "tinyauth/internal/auth"
"tinyauth/internal/hooks"
"tinyauth/internal/types" "tinyauth/internal/types"
"github.com/gin-contrib/sessions" "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) { router.GET("/api/auth", func (c *gin.Context) {
session := sessions.Default(c) userContext := hooks.UseUserContext(c, users)
value := session.Get("tinyauth")
if value != nil { if userContext.IsLoggedIn {
usernameString, ok := value.(string) c.JSON(200, gin.H{
if ok { "status": 200,
if auth.FindUser(users, usernameString) != nil { "message": "Authenticated",
c.JSON(200, gin.H{ })
"status": 200, return
"message": "Authorized",
})
return
}
}
} }
uri := c.Request.Header.Get("X-Forwarded-Uri") 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) { router.GET("/api/status", func (c *gin.Context) {
session := sessions.Default(c) userContext := hooks.UseUserContext(c, users)
value := session.Get("tinyauth")
if value != nil { if !userContext.IsLoggedIn {
usernameString, ok := value.(string) c.JSON(200, gin.H{
if ok { "status": 200,
if auth.FindUser(users, usernameString) != nil { "message": "Unauthenticated",
c.JSON(200, gin.H{ "username": "",
"status": 200, "isLoggedIn": false,
"isLoggedIn": true, })
"username": usernameString, return
"version": assets.Version, }
})
return
}
}
}
c.JSON(200, gin.H{ c.JSON(200, gin.H{
"status": 200, "status": 200,
"isLoggedIn": false, "message": "Authenticated",
"username": "", "username": userContext.Username,
"version": assets.Version, "isLoggedIn": true,
}) })
}) })

44
internal/hooks/hooks.go Normal file
View File

@@ -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,
}
}

View File

@@ -25,4 +25,9 @@ type Config struct {
RootURL string `validate:"required,url" mapstructure:"root-url"` RootURL string `validate:"required,url" mapstructure:"root-url"`
AppURL string `validate:"required,url" mapstructure:"app-url"` AppURL string `validate:"required,url" mapstructure:"app-url"`
Users string `validate:"required" mapstructure:"users"` Users string `validate:"required" mapstructure:"users"`
}
type UserContext struct {
Username string
IsLoggedIn bool
} }

View File

@@ -29,7 +29,7 @@ export const ContinuePage = () => {
return ( return (
<Layout> <Layout>
<Paper shadow="md" p={30} mt={30} radius="md" withBorder> <Paper shadow="md" p={30} mt={30} radius="md" withBorder>
{typeof redirectUri == "string" ? ( {redirectUri !== "null" ? (
<> <>
<Text size="xl" fw={700}> <Text size="xl" fw={700}>
Continue Continue