roles
This commit is contained in:
parent
8c8cbfd7dd
commit
41d48bf60a
28 changed files with 434 additions and 7 deletions
48
auth/authmiddleware/session.go
Normal file
48
auth/authmiddleware/session.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
package authmiddleware
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"git.sapphic.engineer/roleypoly/v4/types"
|
||||
"github.com/goccy/go-json"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/gofiber/fiber/v3/middleware/session"
|
||||
)
|
||||
|
||||
var (
|
||||
SessionKey uint8
|
||||
DefaultSession *Session = &Session{Permissions: PermAnonymous}
|
||||
)
|
||||
|
||||
type Session struct {
|
||||
Permissions Permission
|
||||
User *types.DiscordUser
|
||||
AccessToken string
|
||||
LastRefresh time.Time
|
||||
}
|
||||
|
||||
func (s *Session) AsJSON() []byte {
|
||||
out, err := json.Marshal(s)
|
||||
if err != nil {
|
||||
log.Panicln("failed to marshal session json: ", err)
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func SessionFromJSON(input []byte) (*Session, error) {
|
||||
var s Session
|
||||
err := json.Unmarshal(input, &s)
|
||||
return &s, err
|
||||
}
|
||||
|
||||
func SessionFrom(c fiber.Ctx) (s *Session) {
|
||||
sess := session.FromContext(c)
|
||||
sessionJSON := sess.Get(SessionKey).([]byte)
|
||||
s, err := SessionFromJSON(sessionJSON)
|
||||
if err != nil {
|
||||
log.Panicln("session failed to parse", err)
|
||||
}
|
||||
return s
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue