Skip to content

Instantly share code, notes, and snippets.

@5outh
Last active July 3, 2018 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5outh/1891496de6e7bed696c564594a09339d to your computer and use it in GitHub Desktop.
Save 5outh/1891496de6e7bed696c564594a09339d to your computer and use it in GitHub Desktop.
Authorized
orNotFound :: MaybeT Handler a -> Handler a
orNotFound mHandler = do
act <- runMaybeT mHandler
maybe notFound act
withStudentToken :: (StudentId -> CourseId -> Handler a) -> MaybeT Handler a
withStudentToken action = do
mStudentToken <- MaybeT optionalStudentToken
case mStudentToken of
Nothing -> mempty
Just (StudentAuth studentId courseId) -> MaybeT $ action studentId courseId
withTeacherToken :: (TeacherId -> Handler a) -> MaybeT Handler a
withTeacherToken action = do
mTeacherToken <- MaybeT optionalTeacherToken
case mTeacherToken of
Nothing -> mempty
Just (TeacherAuth teacherId) -> MaybeT $ action teacherId
myHandler = orNotFound $ withStudentToken handleStudent <|> withTeacherToken handleTeacher
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment