Esempio n. 1
0
func (a *Authenticator) Middleware() negroni.HandlerFunc {
	return func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
		user := new(models.User)
		userID := a.session.Get(r, "UserID")
		if bson.IsObjectIdHex(userID) && user.FindByID(bson.ObjectIdHex(userID), a.database.Get(r)) == nil {
			a.currentUser.Set(r, user)
		} else {
			a.currentUser.Clear(r)
		}
		next(rw, r)
	}
}
func (ac *AccountControllerImpl) findOrCreateUser(email, accessToken string, r *http.Request) {
	user := new(models.User)
	db := ac.database.Get(r)
	if user.FindByEmail(email, db); user.ID.Valid() {
		user.Update(email, accessToken, db)
	} else {
		user = models.NewUser(email, accessToken)
		user.Save(db)
	}
	ac.session.Set(r, "UserID", user.ID.Hex())
}