34 lines
564 B
Go
34 lines
564 B
Go
package routing
|
|
|
|
import (
|
|
"lishwist/db"
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/Teajey/sqlstore"
|
|
)
|
|
|
|
type Context struct {
|
|
store *sqlstore.Store
|
|
}
|
|
|
|
func NewContext(store *sqlstore.Store) *Context {
|
|
return &Context{
|
|
store,
|
|
}
|
|
}
|
|
|
|
func (auth *Context) ExpectUser(r *http.Request) *db.User {
|
|
session, _ := auth.store.Get(r, "lishwist_user")
|
|
username, ok := session.Values["username"].(string)
|
|
if !ok {
|
|
log.Fatalln("Failed to get username")
|
|
}
|
|
|
|
user, err := db.GetUserByName(username)
|
|
if err != nil {
|
|
log.Fatalf("Failed to get user: %s\n", err)
|
|
}
|
|
return user
|
|
}
|