From 57eb8f6712361a3bf75983ce153fac4846dc0273 Mon Sep 17 00:00:00 2001 From: Vikas Kushwaha Date: Tue, 11 Feb 2025 16:31:08 +0530 Subject: Initial commit --- internal/authentication/session.go | 60 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 internal/authentication/session.go (limited to 'internal/authentication') diff --git a/internal/authentication/session.go b/internal/authentication/session.go new file mode 100644 index 0000000..8204034 --- /dev/null +++ b/internal/authentication/session.go @@ -0,0 +1,60 @@ +package authentication + +import ( + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/session" + "github.com/gofiber/storage/sqlite3" +) + +var StoredAuthenticationSession *session.Store + +func SessionStart() { + + store := sqlite3.New(sqlite3.Config{ + Table: "fiber_storage", + }) + + authSession := session.New(session.Config{ + Storage: store, + }) + + StoredAuthenticationSession = authSession +} + +func AuthStore(c *fiber.Ctx, userID uint) { + session, err := StoredAuthenticationSession.Get(c) + if err != nil { + panic(err) + } + + session.Set("authentication", userID) + if err := session.Save(); err != nil { + panic(err) + } +} + +func AuthGet(c *fiber.Ctx) (bool, uint) { + session, err := StoredAuthenticationSession.Get(c) + if err != nil { + panic(err) + } + + value := session.Get("authentication") + if value == nil { + return false, 0 + } + + return true, value.(uint) +} + +func AuthDestroy(c *fiber.Ctx) { + session, err := StoredAuthenticationSession.Get(c) + if err != nil { + panic(err) + } + + session.Delete("authentication") + if err := session.Save(); err != nil { + panic(err) + } +} -- cgit v1.2.3