module Helper.Auth
    ( requireAuth_
    , requireAuthId_
    , module Yesod.Auth
    ) where

import Import
import Yesod.Auth
import Data.Maybe (fromMaybe)
import Control.Monad (liftM2)

-- | Like @'requireAuth'@ except that it may respond
--   @'notAuthenticated'@ instead of redirecting to login.
requireAuth_ :: Handler (Entity User)
requireAuth_ = liftM2 fromMaybe notAuthenticated maybeAuth

-- | Like @'requireAuthId'@ except that it may respond
--   @'notAuthenticated'@ instead of redirecting to login.
requireAuthId_ :: Handler UserId
requireAuthId_ = liftM2 fromMaybe notAuthenticated maybeAuthId

module Helper.Auth
    ( requireAuth_
    , requireAuthId_
    , module Yesod.Auth
    ) where

import Import
import Yesod.Auth

-- | Like @'requireAuth'@ except that it may respond
--   @'notAuthenticated'@ instead of redirecting to login.
requireAuth_ :: Handler (Entity User)
requireAuth_ = fromMaybeM notAuthenticated maybeAuth

-- | Like @'requireAuthId'@ except that it may respond
--   @'notAuthenticated'@ instead of redirecting to login.
requireAuthId_ :: Handler UserId
requireAuthId_ = fromMaybeM notAuthenticated maybeAuthId

-- N.B. This should be equivalent to liftM fromMaybe, but that form is
-- evaluating the first argument regardless of the second's Just-ness,
-- resulting in constant notAuthenticated results
fromMaybeM :: Monad m => m a -> m (Maybe a) -> m a
fromMaybeM ma mmb = do
    mb <- mmb

    case mb of
        Nothing -> ma
        Just b  -> return b

Replace liftM2 with custom fromMaybeM
Replace liftM2 with custom fromMaybeM

fromMaybeM is functionally equivalent to liftM2 fromMaybe, but the
latter form was always evaluating the first argument regardless of the
second being Just or Nothing. This had the nasty effect of constantly
responding 401 NotAuthorized.
