我实现了一个类似于描述here的Yesod身份验证系统,通过查找列表中硬编码的用户名和密码可以正常工作。
我现在尝试修改此行为,通过修改 lookupUser 和 validPassword 函数来查找数据库中的用户名和密码。首先,只有 lookupUser :
lookupUser :: Text -> Maybe Owner
lookupUser username = do
x <- runSqlite "db.sqlite3" . asSqlBackendReader $ selectFirst [ElementOwnerId ==. username] []
case x of
Nothing -> Nothing
Just (Entity k v) -> Just $ Owner username (elementOwnerPw v)
asSqlBackendReader :: ReaderT SqlBackend m a -> ReaderT SqlBackend m a
asSqlBackendReader = id
但我收到以下错误,我不明白:
• Couldn't match type ‘Maybe’ with ‘IO’
arising from a functional dependency between:
constraint ‘MonadBaseControl IO Maybe’
arising from a use of ‘runSqlite’
instance ‘MonadBaseControl Maybe Maybe’ at <no location info>
• In the first argument of ‘(.)’, namely ‘runSqlite "db.sqlite3"’
In the expression: runSqlite "db.sqlite3" . asSqlBackendReader
In a stmt of a 'do' block:
x <- runSqlite "db.sqlite3" . asSqlBackendReader
$ selectFirst [ElementOwnerId ==. username] []
|
238 | x <- runSqlite "db.sqlite3" . asSqlBackendReader $ selectFirst [ElementOwnerId ==. username] []
| ^^^^^^^^^^^^^^^^^^^^^^^^
runSqlite 和 selectFirst 的签名:
runSqlite :: MonadCatchIO m => SqliteReader m a -> Pool Connection -> m a
selectFirst :: (PersistEntity val, PersistEntityBackend val ~ b) => [Filter val] -> [SelectOpt val] -> b m (Maybe (Entity val))