代码之家  ›  专栏  ›  技术社区  ›  frankhucek

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

  •  1
  • frankhucek  · 技术社区  · 7 年前

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

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

    我是否可以在处理程序上设置类型约束,以确保 AuthId主机 显示 ?

    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 回复  |  直到 7 年前
        1
  •  1
  •   Cubic mgsloan    7 年前

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

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

    FlexibleContexts {-# LANGUAGE FlexibleContexts #-} 在源文件的顶部。