Exemple #1
0
func Authenticate(f func(http.ResponseWriter, *http.Request, *model.User) error) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var (
			user *model.User
			err  error
		)

		c, err := cookie.Read(r, "user")
		if err != nil {
			serveUnauthorized(w)
			return
		}

		userId := c.Values["id"]
		if userId == "" {
			serveUnauthorized(w)
			return
		}

		user = repository.User().FirstByObjectId(userId)
		if user == nil {
			serveUnauthorized(w)
			return
		}

		err = f(w, r, user)

		if err != nil {
			serveError(w, err)
			log.Printf("handling %q: %v", r.RequestURI, err)
		}
	}
}
Exemple #2
0
func Home(w http.ResponseWriter, r *http.Request) error {
	c, err := cookie.Read(r, "user")
	var userLogin, userAvatarURL string

	if err == nil {
		objectId := c.Values["id"]
		if objectId != "" {
			user := repository.User().FirstByObjectId(objectId)
			if user != nil {
				userLogin = user.Login
				userAvatarURL = user.AvatarURL
			}
		}
	}

	return Templates.ExecuteTemplate(w, "index.html", struct {
		UserLogin     string
		UserAvatarURL string
	}{
		UserLogin:     userLogin,
		UserAvatarURL: userAvatarURL,
	})
}