fix: Migrated from gofiber/session to gofiber/fiber/v2/middleware/session

This commit is contained in:
ptrcnull 2020-11-17 17:46:36 +01:00
parent a58a2d9397
commit c008854f63
5 changed files with 27 additions and 25 deletions

8
app.go
View file

@ -5,7 +5,7 @@ import (
"git.ddd.rip/ptrcnull/modweb/static"
"git.ddd.rip/ptrcnull/modweb/utils"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/session/v2"
"github.com/gofiber/fiber/v2/middleware/session"
"github.com/gofiber/template/html"
)
@ -14,7 +14,7 @@ type App struct {
Config Config
modules map[string]Module
fiber *fiber.App
sessions *session.Session
sessions *session.Store
authHandlers []AuthHandler
viewsFs *utils.JoinedFilesystem
homepage string
@ -29,7 +29,7 @@ func (app *App) data(ctx *fiber.Ctx, d fiber.Map) fiber.Map {
for k, v := range d {
base[k] = v
}
sess := app.sessions.Get(ctx)
sess, _ := app.sessions.Get(ctx)
if _, ok := sess.Get("user:id").(string); ok {
user := &User{}
user.Load(sess)
@ -61,7 +61,7 @@ func New(configs ...Config) *App {
})
app.fiber.Get("/logout", func(ctx *fiber.Ctx) error {
store := app.sessions.Get(ctx)
store, _ := app.sessions.Get(ctx)
store.Destroy()
return ctx.Redirect("/")
})

18
auth.go
View file

@ -1,6 +1,6 @@
package modweb
import "github.com/gofiber/session/v2"
import "github.com/gofiber/fiber/v2/middleware/session"
type AuthHandler interface {
Module
@ -14,14 +14,14 @@ type User struct {
AccessLevel int64
}
func (u User) Save(store *session.Store) {
store.Set("user:id", u.ID)
store.Set("user:displayName", u.DisplayName)
store.Set("user:accessLevel", u.AccessLevel)
func (u User) Save(sess *session.Session) {
sess.Set("user:id", u.ID)
sess.Set("user:displayName", u.DisplayName)
sess.Set("user:accessLevel", u.AccessLevel)
}
func (u *User) Load(store *session.Store) {
u.ID = store.Get("user:id").(string)
u.DisplayName = store.Get("user:displayName").(string)
u.AccessLevel = store.Get("user:accessLevel").(int64)
func (u *User) Load(sess *session.Session) {
u.ID = sess.Get("user:id").(string)
u.DisplayName = sess.Get("user:displayName").(string)
u.AccessLevel = sess.Get("user:accessLevel").(int64)
}

2
go.mod
View file

@ -4,7 +4,7 @@ go 1.15
require (
github.com/UnnoTed/fileb0x v1.1.4 // indirect
github.com/gofiber/fiber/v2 v2.1.4
github.com/gofiber/fiber/v2 v2.2.0
github.com/gofiber/session/v2 v2.0.2
github.com/gofiber/template v1.6.4
golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0

11
go.sum
View file

@ -102,12 +102,10 @@ github.com/go-toolsmith/strparse v1.0.0/go.mod h1:YI2nUKP9YGZnL/L1/DLFBfixrcjslW
github.com/go-toolsmith/typep v1.0.0/go.mod h1:JSQCQMUPdRlMZFswiq3TGpNp1GMktqkR2Ns5AIQkATU=
github.com/go-toolsmith/typep v1.0.2/go.mod h1:JSQCQMUPdRlMZFswiq3TGpNp1GMktqkR2Ns5AIQkATU=
github.com/go-xmlfmt/xmlfmt v0.0.0-20191208150333-d5b6f63a941b/go.mod h1:aUCEOzzezBEjDBbFBoSiya/gduyIiWYRP6CnSFIV8AM=
github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI=
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/gofiber/fiber/v2 v2.1.0/go.mod h1:aG+lMkwy3LyVit4CnmYUbUdgjpc3UYOltvlJZ78rgQ0=
github.com/gofiber/fiber/v2 v2.1.4 h1:3PMynvfkvMTXouNe9vAyGFkLXk8JM1DlH+k6FJvVoTc=
github.com/gofiber/fiber/v2 v2.1.4/go.mod h1:YjN8skLvMICBTHLK3a5AJVsokZet6xTRs5axSP9aK1s=
github.com/gofiber/fiber/v2 v2.2.0 h1:U9IkTlomVnR+Q5aBhgC0R6ePTiwTnNLXWQR+h+oYUN8=
github.com/gofiber/fiber/v2 v2.2.0/go.mod h1:Slpou87elSO9qom9nwIo/IoQJ2qfRuMAQ/qQ9F0o4b0=
github.com/gofiber/session/v2 v2.0.2 h1:VuLOb+QpMHaXZ6JXZnp3xCRv8TPRc1VLB9ddaA0MMGU=
github.com/gofiber/session/v2 v2.0.2/go.mod h1:H+M+PENuiMTQJf9cTx+DJ7Jw8IqBAJ3QSbS6PbmvA78=
github.com/gofiber/template v1.6.4 h1:BA/qZs0s5Afksnmn9HGOip5BXZTdrFsQ3ijxYoYH+Yc=
@ -210,6 +208,8 @@ github.com/klauspost/compress v1.10.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYs
github.com/klauspost/compress v1.10.5/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/compress v1.10.7 h1:7rix8v8GpI3ZBb0nSozFRgbtXKv+hOe+qfEpZqybrAg=
github.com/klauspost/compress v1.10.7/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/compress v1.11.0 h1:wJbzvpYMVGG9iTI9VxpnNZfd4DzMPoCWze3GgSqz8yg=
github.com/klauspost/compress v1.11.0/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
@ -229,8 +229,6 @@ github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQ
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/maratori/testpackage v1.0.1/go.mod h1:ddKdw+XG0Phzhx8BFDTKgpWP4i7MpApTE5fXSKAqwDU=
github.com/markbates/pkger v0.17.1 h1:/MKEtWqtc0mZvu9OinB9UzVN9iYCwLWuyUv4Bw+PCno=
github.com/markbates/pkger v0.17.1/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI=
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb/go.mod h1:1BELzlh859Sh1c6+90blK8lbYy0kwQf1bYlBhBysy1s=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
@ -582,7 +580,6 @@ gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bl
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View file

@ -2,7 +2,7 @@ package modweb
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/session/v2"
"github.com/gofiber/fiber/v2/middleware/session"
"net/http"
)
@ -36,12 +36,13 @@ func (mm *ModuleManager) Render(ctx *fiber.Ctx, template string, data fiber.Map)
return ctx.Render(mm.module.Name() + "/" + template, mm.app.data(ctx, data), "layouts/main")
}
func (mm *ModuleManager) Session(ctx *fiber.Ctx) *session.Store {
return mm.app.sessions.Get(ctx)
func (mm *ModuleManager) Session(ctx *fiber.Ctx) *session.Session {
sess, _ := mm.app.sessions.Get(ctx)
return sess
}
func (mm *ModuleManager) User(ctx *fiber.Ctx) *User {
sess := mm.app.sessions.Get(ctx)
sess := mm.Session(ctx)
if _, ok := sess.Get("user:id").(string); ok {
user := &User{}
user.Load(sess)
@ -53,3 +54,7 @@ func (mm *ModuleManager) User(ctx *fiber.Ctx) *User {
func (mm *ModuleManager) SetHomepage(template string) {
mm.app.homepage = mm.module.Name() + "/" + template
}
func (mm *ModuleManager) SetSessionConfig(config session.Config) {
mm.app.sessions = session.New(config)
}