YesodAuthEmail无法推断m~HandlerFor site0

时间:2018-06-12 02:32:17

标签: haskell authorization yesod type-families

我正在尝试添加

instance YesodAuthEmail App 

Yesod-Postgres脚手架(yesod版本1.6)并且遇到编译错误。

相关代码是:

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 

我收到的错误是:

/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)

根据类型,

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

我原本以为这会编译。我误解了什么?

P.S。我试图包含所有相关内容,但完整的Foundation.hs位于https://gist.github.com/hyperpape/39d4d2baf67d3bdbdba45a943e7e0425

1 个答案:

答案 0 :(得分:3)

runDB的类型是:

runDB :: YesodDB site a -> HandlerFor site a 

要在AuthHandler中调用它,您需要将其提升到HandlerFor

如果我没有记错,这就是MonadHandler runDB方法的用途。

如果您使用它撰写addUnverified email verkey = liftHandler . runDB $ insert $ User email Nothing 来电,则应该有效:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Data / Import",
            "type": "process",
            "command": "php",
            "args": [
                "data/bin/import.php"
            ]
        }
    ]
}

我找到了问题liftHandler的详细答案。