YesodAuthEmail could not deduce m ~ HandlerFor site0 [duplicate]

我的梦境 提交于 2019-12-19 09:49:34

问题


I'm trying to add

instance YesodAuthEmail App 

to the Yesod-Postgres scaffolding (yesod version 1.6) and getting stuck on a compile error.

The relevant code is:

instance YesodAuth App where
     type AuthId App = UserId
     ....
     authPlugins :: App -> [AuthPlugin App]
     authPlugins app = [authOpenId Claimed []] ++ extraAuthPlugins
         where extraAuthPlugins = [ authEmail ]

instance YesodAuthEmail App where
    type AuthEmailId App = UserId

    afterPasswordRoute _ = HomeR

    addUnverified email verkey =
        runDB $ insert $ User email Nothing 

The error I receive is:

/home/justin/code/yesodemail/src/Foundation.hs:273:11: error:
• Could not deduce: m ~ HandlerFor site0 from the context: MonadAuthHandler App m bound by the type signature for: addUnverified :: Yesod.Auth.Email.Email -> VerKey -> AuthHandler App (AuthEmailId App) ....
Expected type: m (AuthEmailId App) Actual type: HandlerFor site0 (Key User)

Based on the types,

getEmail :: AuthEmailId site -> AuthHandler site (Maybe Email) 
type MonadAuthHandler master m = (MonadHandler m, YesodAuth master, master ~ HandlerSite m, Auth ~ SubHandlerSite m, MonadUnliftIO m)
type AuthHandler master a = forall m. MonadAuthHandler master m => m a

I would have thought this would compile. What I am misunderstanding?

P.S. I've tried to include everything relevant, but the full Foundation.hs is at https://gist.github.com/hyperpape/39d4d2baf67d3bdbdba45a943e7e0425


回答1:


The type of runDB is:

runDB :: YesodDB site a -> HandlerFor site a 

in order to call it in AuthHandler you need to lift it to HandlerFor.

If I am not mistaken this is what the liftHandler method from MonadHandler is for.

If you compose your runDB call with it, it should work:

addUnverified email verkey =
    liftHandler . runDB $ insert $ User email Nothing

I found a detailed answer to your question here.



来源:https://stackoverflow.com/questions/50808477/yesodauthemail-could-not-deduce-m-handlerfor-site0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!