我正在尝试设置硬编码的身份验证,以依赖Yesod脚手架中config/settings.yml
中指定的登录信息。我写了以下代码:
instance YesodAuthHardcoded App where
validatePassword u = do
app <- getYesod
let user = appUser $ appSettings app
return . validPassword user u . fromString . unpack
doesUserNameExist = do
app <- getYesod
return . isJust . lookupUser app
我收到validatePassord
的以下错误消息以及doesUserNameExist
的等效消息:
• Couldn't match type ‘HandlerSite ((->) Text)’ with ‘App’
Expected type: Text -> App
Actual type: Text -> HandlerSite ((->) Text)
• In a stmt of a 'do' block: app <- getYesod
In the expression:
do { app <- getYesod;
let user = appUser $ appSettings app;
return . validPassword user u . fromString . unpack }
In an equation for ‘validatePassword’:
validatePassword u
= do { app <- getYesod;
let user = ...;
return . validPassword user u . fromString . unpack }
validatePassword
is defined in Yesod.Auth.Hardcoded
的原型:
validatePassword :: Text -> Text -> AuthHandler site Bool
和AuthHandler
is defined in Yesod.Auth
:
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
最后getYesod
is defined as follow in Yesod.Core.Handler
:
getYesod :: MonadHandler m => m (HandlerSite m)
从这些定义看来,我应该有办法从getYesod
monad调用AuthHandler
,但我无法弄清楚如何。
编辑:
我已将yesod
及其脚手架更新为最新版本。我还修改了validatePassword
如下:
validatePassword u = do
app <- liftHandler . getYesod
let user = appUser $ appSettings app
return . validPassword user u . fromString . unpack
我收到以下错误消息:
• Couldn't match type ‘m1 a1’ with ‘App’
Expected type: HandlerFor (HandlerSite m1) a1 -> App
Actual type: HandlerFor (HandlerSite m1) a1 -> m1 a1
• In the first argument of ‘(.)’, namely ‘liftHandler’
In a stmt of a 'do' block: app <- liftHandler . getYesod
In the expression:
do app <- liftHandler . getYesod
let user = appUser $ appSettings app
return . validPassword user u . fromString . unpack
|
259 | app <- liftHandler . getYesod
|
答案 0 :(得分:0)
该错误来自我尝试修改Yesod.Auth.Hardcoded
模块提供的代码段。我引入了一个与省略的参数冲突的do
符号。指定参数可以解决问题,代码变为:
instance YesodAuthHardcoded App where
validatePassword u p = do
app <- getYesod
let user = appUser $ appSettings app
return . validPassword user u . fromString $ unpack p
doesUserNameExist u = do
app <- getYesod
return . isJust $ lookupUser app u