Ecognium
Ecognium

Reputation: 2076

maybeAuthId Type Error

I have a scaffolded site and I am using this snippet of code in the Home Handler.

{-# LANGUAGE TupleSections, OverloadedStrings #-}
module Handler.Home where

import Import
import Yesod.Auth

getHomeR :: Handler RepHtml   
getHomeR = do
  defaultLayout $ do
    maid <- maybeAuthId
    setTitle "Welcome!"
    $(widgetFile "homepage")

I would like to access maid in my homepage.hamlet file. However, I get the following error:

Handler/Home.hs:10:17:
    Couldn't match expected type `WidgetT site0 IO t0' 
                with actual type `HandlerT master0 IO (Maybe (AuthId master0))'
    In a stmt of a 'do' block: maid <- maybeAuthId
    In the second argument of `($)', namely
      `do { maid <- maybeAuthId;
            setTitle "Welcome!";
            $(widgetFile "homepage") }'
    In a stmt of a 'do' block:
      defaultLayout
      $ do { maid <- maybeAuthId;
             setTitle "Welcome!";
             $(widgetFile "homepage") }

I get the above error message whether or not I put any contents inside homepage.hamlet. Instead of using $(widgetFile "homepage"), if I paste the whamlet code snippet from the Yesod Book (Auth section), it works fine.

If I remove the call to maybeAuthId, the issue goes away too. I am guessing it is something to do with the call to maybeAuthId and using the widgetFile but I am not sure how to fix the issue. Any help appreciated.

Thanks!

Upvotes: 1

Views: 226

Answers (1)

Michael Snoyman
Michael Snoyman

Reputation: 31305

maybeAuthId lives in the Handler monad, and the inside of defaultLayout is a Widget, which is why you have a mismatch. You could do one of the following:

  • Convert the Handler action to a Widget action using handlerToWidget
  • Move the maybeAuthId call to before defaultLayout

Upvotes: 2

Related Questions