summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby <[email protected]>2026-02-11 14:02:58 +0530
committerBobby <[email protected]>2026-02-11 14:02:58 +0530
commitd87e08e0fc5911b2ff40604944448e1f0aaa31b7 (patch)
treed64dba5b796ad39cb0514293cfd29826b66b8c7c
parent2c567dd96712a887b42cf4df3bdaa9f5f2b51a43 (diff)
downloadcafe-d87e08e0fc5911b2ff40604944448e1f0aaa31b7.tar.xz
cafe-d87e08e0fc5911b2ff40604944448e1f0aaa31b7.zip
Implement authentication flow with middleware and controllers, add OpenID discovery URL to server config
-rw-r--r--cafe/main.go7
-rw-r--r--config/env.go13
-rw-r--r--controllers/auth.go19
-rw-r--r--controllers/main.go14
-rw-r--r--middleware/middleware.go7
-rw-r--r--router/auth.go13
-rw-r--r--router/base.go9
-rw-r--r--static/js/app.js2
-rw-r--r--templates/pages/auth.django5
-rw-r--r--utils/auth/auth.go27
10 files changed, 101 insertions, 15 deletions
diff --git a/cafe/main.go b/cafe/main.go
index 716b5ef..f123b0b 100644
--- a/cafe/main.go
+++ b/cafe/main.go
@@ -2,6 +2,7 @@ package main
import (
"cafe/config"
+ "cafe/middleware"
"cafe/processors"
"cafe/router"
"cafe/tags"
@@ -27,8 +28,9 @@ func main() {
engine.Reload(config.Server.DevMode)
app := fiber.New(fiber.Config{
- Views: engine,
- ErrorHandler: router.ErrorHandler,
+ Views: engine,
+ ErrorHandler: router.ErrorHandler,
+ DisableStartupMessage: true,
})
app.Use(logger.New())
@@ -40,6 +42,7 @@ func main() {
processors.Initialize(app)
router.Initialize(app)
+ middleware.Initialize(app)
address := fmt.Sprintf("%s:%d", config.Server.Host, config.Server.Port)
log.Printf("Starting server at %s\n", address)
diff --git a/config/env.go b/config/env.go
index 760bb3d..7bdbfa2 100644
--- a/config/env.go
+++ b/config/env.go
@@ -3,12 +3,13 @@ package config
import "time"
type server struct {
- Host string `env:"SERVER_HOST" default:"localhost"`
- Port int `env:"SERVER_PORT" default:"8080"`
- AppSecret string `env:"APP_SECRET" default:"mysecret"`
- AppName string `env:"APP_NAME" default:"Shifoo's Cafe"`
- AppDescription string `env:"APP_DESCRIPTION" default:"A cozy place for close friends"`
- DevMode bool `env:"DEV_MODE" default:"true"`
+ Host string `env:"SERVER_HOST" default:"localhost"`
+ Port int `env:"SERVER_PORT" default:"8080"`
+ AppSecret string `env:"APP_SECRET" default:"mysecret"`
+ AppName string `env:"APP_NAME" default:"Shifoo's Cafe"`
+ AppDescription string `env:"APP_DESCRIPTION" default:"A cozy place for close friends"`
+ OpenIDDiscoveryURL string `env:"OPENID_DISCOVERY_URL" default:""`
+ DevMode bool `env:"DEV_MODE" default:"true"`
}
type database struct {
diff --git a/controllers/auth.go b/controllers/auth.go
new file mode 100644
index 0000000..2d8d89b
--- /dev/null
+++ b/controllers/auth.go
@@ -0,0 +1,19 @@
+package controllers
+
+import (
+ "cafe/utils/auth"
+ "cafe/utils/meta"
+ "cafe/utils/shortcuts"
+
+ "github.com/gofiber/fiber/v2"
+)
+
+func Authenticate(context *fiber.Ctx) error {
+ if auth.IsAuthenticated(context) {
+ return shortcuts.Redirect(context, "mainHall")
+ }
+
+ meta.SetPageTitle(context, "Open ID Authentication")
+
+ return shortcuts.Render(context, "pages/auth", nil)
+}
diff --git a/controllers/main.go b/controllers/main.go
new file mode 100644
index 0000000..1b9a74b
--- /dev/null
+++ b/controllers/main.go
@@ -0,0 +1,14 @@
+package controllers
+
+import (
+ "cafe/utils/meta"
+ "cafe/utils/shortcuts"
+
+ "github.com/gofiber/fiber/v2"
+)
+
+func MainHall(context *fiber.Ctx) error {
+ meta.SetPageTitle(context, "Main Hall")
+
+ return shortcuts.Render(context, "pages/main", nil)
+}
diff --git a/middleware/middleware.go b/middleware/middleware.go
new file mode 100644
index 0000000..87775bb
--- /dev/null
+++ b/middleware/middleware.go
@@ -0,0 +1,7 @@
+package middleware
+
+import "github.com/gofiber/fiber/v2"
+
+func Initialize(app *fiber.App) {
+ // Middleware can be added here
+}
diff --git a/router/auth.go b/router/auth.go
new file mode 100644
index 0000000..21b9d9f
--- /dev/null
+++ b/router/auth.go
@@ -0,0 +1,13 @@
+package router
+
+import (
+ "cafe/controllers"
+ "cafe/types"
+ "cafe/utils/urls"
+)
+
+func init() {
+ urls.SetNamespace("auth")
+
+ urls.Path(types.GET, "/", controllers.Authenticate, "authenticate")
+}
diff --git a/router/base.go b/router/base.go
index aaeaad3..6c66c7b 100644
--- a/router/base.go
+++ b/router/base.go
@@ -1,17 +1,14 @@
package router
import (
+ "cafe/controllers"
"cafe/types"
- "cafe/utils/shortcuts"
+ "cafe/utils/auth"
"cafe/utils/urls"
-
- "github.com/gofiber/fiber/v2"
)
func init() {
urls.SetNamespace("")
- urls.Path(types.GET, "/", func(c *fiber.Ctx) error {
- return shortcuts.Render(c, "pages/main", fiber.Map{})
- }, "home")
+ urls.Path(types.GET, "/", auth.RequireAuthentication(controllers.MainHall), "mainHall")
}
diff --git a/static/js/app.js b/static/js/app.js
index f1ec61e..24953e7 100644
--- a/static/js/app.js
+++ b/static/js/app.js
@@ -1,6 +1,6 @@
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
- navigator.serviceWorker.register('/worker.js')
+ navigator.serviceWorker.register('/static/js/worker.js')
.then(registration => {
console.log('ServiceWorker registered:', registration);
})
diff --git a/templates/pages/auth.django b/templates/pages/auth.django
new file mode 100644
index 0000000..2c4c372
--- /dev/null
+++ b/templates/pages/auth.django
@@ -0,0 +1,5 @@
+{% extends "layouts/base.django" %}
+
+{% block content %}
+You will be authenticated here.
+{% endblock %} \ No newline at end of file
diff --git a/utils/auth/auth.go b/utils/auth/auth.go
new file mode 100644
index 0000000..3cee3a4
--- /dev/null
+++ b/utils/auth/auth.go
@@ -0,0 +1,27 @@
+package auth
+
+import (
+ "cafe/session"
+ "cafe/utils/shortcuts"
+
+ "github.com/gofiber/fiber/v2"
+)
+
+func IsAuthenticated(context *fiber.Ctx) bool {
+ session, err := session.Store.Get(context)
+ if err != nil {
+ return false
+ }
+
+ username := session.Get("username")
+ return username != nil
+}
+
+func RequireAuthentication(handler fiber.Handler) fiber.Handler {
+ return func(context *fiber.Ctx) error {
+ if !IsAuthenticated(context) {
+ return shortcuts.Redirect(context, "auth.authenticate")
+ }
+ return handler(context)
+ }
+}