This repository has been archived on 2021-09-01. You can view files and clone it, but cannot push or open issues or pull requests.
goddit/web/sessions.go

48 lines
913 B
Go
Raw Normal View History

2021-08-29 17:09:48 +02:00
package web
import (
2021-08-29 17:43:45 +02:00
"context"
2021-08-29 17:09:48 +02:00
"database/sql"
"encoding/gob"
2021-08-29 17:09:48 +02:00
"github.com/alexedwards/scs/postgresstore"
"github.com/alexedwards/scs/v2"
"github.com/google/uuid"
2021-08-29 17:09:48 +02:00
)
func init() {
gob.Register(uuid.UUID{})
}
2021-08-29 17:09:48 +02:00
func NewSessionsManager(dataSourceName string) (*scs.SessionManager, error) {
db, err := sql.Open("postgres", dataSourceName)
if err != nil {
return nil, err
}
sessions := scs.New()
sessions.Store = postgresstore.New(db)
return sessions, nil
}
2021-08-29 17:43:45 +02:00
type SessionData struct {
FlashMessage string
2021-08-29 18:22:08 +02:00
Form interface{}
2021-08-29 17:43:45 +02:00
// UserID uuid.UUID
}
func GetSessionData(session *scs.SessionManager, ctx context.Context) SessionData {
var data SessionData
data.FlashMessage = session.PopString(ctx, "flash")
// data.UserID, _ = session.Get(ctx "user_id").(uuid.UUID)
2021-08-29 18:22:08 +02:00
data.Form = session.Pop(ctx, "form")
if data.Form == nil {
data.Form = map[string]string{}
}
2021-08-29 17:43:45 +02:00
return data
}