kuvia2/pkg/globaleuserdata/globaluserdata.go

32 lines
813 B
Go
Raw Normal View History

2022-01-14 23:09:03 +00:00
package globaleuserdata
import (
"github.com/flamego/flamego"
"github.com/flamego/session"
"github.com/flamego/template"
"github.com/jmoiron/sqlx"
"github.com/rs/zerolog/log"
"reflect"
)
func GlobalUserData(db *sqlx.DB) flamego.Handler {
log.Debug().Msg("Handler")
return flamego.ContextInvoker(func(c flamego.Context) {
r := c.Value(reflect.TypeOf(template.Data{})).Interface().(template.Data)
s := c.Value(reflect.TypeOf((*session.Session)(nil)).Elem()).Interface().(session.Session)
userId := s.Get("user_id")
r["g_user_id"] = userId
if userId != nil {
r["g_loggedin"] = true
log.Debug().Msg("User Data Query")
re := []struct{
Username string
}{}
db.Select(&re, "SELECT username FROM users WHERE id = ?", userId)
r["g_userName"] = re[0].Username
}
c.Next()
})
}