我实现了一个类似于此处描述的 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))