func Authenticate(required bool) chainer { return func(ctx *_Context) error { session, err := sessionStore.Get(ctx.r, sessionStoreName) if err != nil { return err } userId, ok := session.Values[sessionUserIdKey] if !ok { if required { return util.Unauthorized } return nil } user, team, err := table.FindUserWithTeam(userId.(int64)) if err == nil { ctx.loggedIn = &_LoggedIn{ User: user, Team: team, } } else if err == table.NoUser { delete(session.Values, sessionUserIdKey) return session.Save(ctx.r, ctx.w) } return err } }
func TestFindUserWithTeam(t *T) { beforeUserTest() user, team, err := table.FindUserWithTeam(100) rdtest.Assert(t, err == nil, err) rdtest.Assert(t, user.UserId == 100, user) rdtest.Assert(t, team.TeamId == 1, team) }