Haskell Yesod在子网站中获取用户凭据

时间:2017-08-15 14:34:18

标签: haskell yesod

我正在为yesod项目编写子网站,我需要在该子网站上显示登录用户的名称(我使用yesod-auth硬编码,其中AuthId master = Text的类型)。

但是,用户已登录主站点。我可以使用AuthId master获取maybeAuthId类型的值,但我无法显示此值,因为它不是Show的实例。

我可以在我的处理程序上放置一个类型约束,以确保AuthId master的类型派生Show吗?

getSubsiteHomeR :: (YesodAuth master) => HandlerT Subsite (HandlerT master IO) Html
getSubsiteHomeR = do
   lift $ do
     maid <- maybeAuthId -- I want to display the value of 'maid'
     liftIO $ print maid
     defaultLayout [whamlet|.......|]

编辑:以下是错误消息:

Could not deduce (Show (AuthId master))
    arising from a use of `print'
  from the context: YesodAuth master
    bound by the type signature for:
               getSubsiteHomeR :: YesodAuth master =>
                                     HandlerT Subsite (HandlerT master IO) Html
    at src/Subsite.hs:24:1-89
* In the second argument of `($)', namely `print maid'
  In a stmt of a 'do' block: liftIO $ print maid
  In the second argument of `($)', namely
    `do { maid <- maybeAuthId;
          liftIO $ print maid;
          defaultLayout
            (do { (asWidgetT . toWidget)
                    ((blaze-markup-0.8.0.0:Text.Blaze.Internal.preEscapedText . T.pack)
                       "<p>Welcome to the Subsite!</br><a href=\"");
                  (getUrlRenderParams
                   >>=
                     (\ urender_alJ6
                        -> (asWidgetT . toWidget)
                             (toHtml
                                ((\ u_alJ7 -> urender_alJ6 u_alJ7 ...)
                                   (toParent SubsiteHomeR)))));
                  (asWidgetT . toWidget)
                    ((blaze-markup-0.8.0.0:Text.Blaze.Internal.preEscapedText . T.pack)
                       "\">Subsite</br></a>\n\
                       \<a href=\"");
                  .... }) }'

1 个答案:

答案 0 :(得分:1)

在我看来,您需要的只是类型签名中的Show (AuthId master)约束:

getSubsiteHomeR :: (YesodAuth master, Show (AuthId master)) => HandlerT Subsite (HandlerT master IO) Html

请注意,这需要FlexibleContexts语言扩展名,您可以将{-# LANGUAGE FlexibleContexts #-}放在源文件的顶部来启用。