25 lines
675 B
Go
25 lines
675 B
Go
package routing
|
|
|
|
import (
|
|
"lishwist/db"
|
|
"lishwist/rsvp"
|
|
"net/http"
|
|
)
|
|
|
|
func ExpectUser(next func(*db.User, http.Header, *rsvp.Request) rsvp.Response) rsvp.HandlerFunc {
|
|
return func(w http.Header, r *rsvp.Request) rsvp.Response {
|
|
session := r.GetSession()
|
|
username, ok := session.GetValue("username").(string)
|
|
if !ok {
|
|
return rsvp.Error(http.StatusInternalServerError, "Something went wrong.").Log("Failed to get username from session")
|
|
}
|
|
|
|
user, err := db.GetUserByName(username)
|
|
if err != nil {
|
|
return rsvp.Error(http.StatusInternalServerError, "Something went wrong.").Log("Failed to get user %q: %s", username, err)
|
|
}
|
|
|
|
return next(user, w, r)
|
|
}
|
|
}
|