Haskell Yesod 在子站点中获取用户凭据
Haskell Yesod get user credentials in subsite
我正在为我的 yesod 项目编写一个子站点,我需要在该子站点上显示登录的用户名(我使用的是硬编码的 yesod-auth,其中 AuthId master = Text
的类型)。
但是,用户已登录到主站点。我可以使用 maybeAuthId
获得类型 AuthId master
的值,但我无法显示该值,因为它不是 Show
.
的实例
我可以对 Handler 施加类型约束以确保 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=\"");
.... }) }'
在我看来,你所需要的只是类型签名中的 Show (AuthId master)
约束:
getSubsiteHomeR :: (YesodAuth master, Show (AuthId master)) => HandlerT Subsite (HandlerT master IO) Html
请注意,这需要 FlexibleContexts
语言扩展,您可以通过将 {-# LANGUAGE FlexibleContexts #-}
放在源文件的顶部来启用它。
我正在为我的 yesod 项目编写一个子站点,我需要在该子站点上显示登录的用户名(我使用的是硬编码的 yesod-auth,其中 AuthId master = Text
的类型)。
但是,用户已登录到主站点。我可以使用 maybeAuthId
获得类型 AuthId master
的值,但我无法显示该值,因为它不是 Show
.
我可以对 Handler 施加类型约束以确保 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=\"");
.... }) }'
在我看来,你所需要的只是类型签名中的 Show (AuthId master)
约束:
getSubsiteHomeR :: (YesodAuth master, Show (AuthId master)) => HandlerT Subsite (HandlerT master IO) Html
请注意,这需要 FlexibleContexts
语言扩展,您可以通过将 {-# LANGUAGE FlexibleContexts #-}
放在源文件的顶部来启用它。