diff options
author | Vikas Kushwaha <dev@vikas.rocks> | 2025-02-11 16:31:08 +0530 |
---|---|---|
committer | Vikas Kushwaha <dev@vikas.rocks> | 2025-02-11 16:31:08 +0530 |
commit | 57eb8f6712361a3bf75983ce153fac4846dc0273 (patch) | |
tree | 269a168d59c917c4e313c819e2b4c3ff8175f912 /internal/authentication |
Initial commit
Diffstat (limited to 'internal/authentication')
-rw-r--r-- | internal/authentication/session.go | 60 |
1 files changed, 60 insertions, 0 deletions
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) + } +} |