0

我正在尝试将硬编码身份验证添加到 Yesod。我刚刚简要修改了 Yesod 脚手架并按照文档添加了一个硬编码用户(http://hackage.haskell.org/package/yesod-auth-1.6.3/docs/Yesod-Auth-Hardcoded.html) . 所以我有以下代码:

instance YesodAuth App where
    type AuthId App = Either UserId Text

    -- Where to send a user after successful login
    loginDest :: App -> Route App
    loginDest _ = HomeR
    -- Where to send a user after logout
    logoutDest :: App -> Route App
    logoutDest _ = HomeR
    -- Override the above two destinations when a Referer: header is present
    redirectToReferer :: App -> Bool
    redirectToReferer _ = True

    authPlugins _ = [authHardcoded]

    authenticate Creds{..} =
        return
        (case credsPlugin of
            "hardcoded" ->
                case lookupUser credsIdent of
                    Nothing -> UserError InvalidLogin
                    Just m  -> Authenticated (Right (manUserName m)))


-- | Access function to determine if a user is logged in.
isAuthenticated :: Handler AuthResult
isAuthenticated = do
    muid <- maybeAuthId
    return $ case muid of
        Nothing -> Unauthorized "You must login to access this page"
        Just _ -> Authorized


...        

instance YesodAuthPersist App where
    type AuthEntity App = Either User SiteManager

    getAuthEntity (Left uid) =
      do x <- liftHandler $ runDB (get uid)
         return (fmap Left x)
    getAuthEntity (Right username) = return (fmap Right (lookupUser username))
...
instance PathPiece (Either UserId Text) where
    fromPathPiece = readMaybe . unpack
    toPathPiece = pack . show

lookupUser :: Text -> Maybe SiteManager
lookupUser username = find (\m -> manUserName m == username) siteManagers

instance YesodAuthHardcoded App where
    validatePassword u = return . validPassword u
    doesUserNameExist  = return . isJust . lookupUser

validPassword :: Text -> Text -> Bool
validPassword u p =
    case find (\m -> manUserName m == u && manPassWord m == p) siteManagers of
        Just _ -> True
        _      -> False

因此,似乎 getAuthEntity 已正确实施。但是,现在当我尝试使用 getAuthEntity 获取用户时,如下所示:

getProfileR :: Handler Html
getProfileR = do
    uid <- getAuthEntity   
    defaultLayout $ do
        setTitle . toHtml $ ("hola" :: Text )
        $(widgetFile "profile")

它只是因错误而失败:

    • Couldn't match expected type ‘HandlerFor App a0’
                  with actual type ‘AuthId (HandlerSite m0)
                                    -> m0 (Maybe (AuthEntity (HandlerSite m0)))’
   |
12 |     uid <- getAuthEntity
   |            ^^^^^^^^^^^^^

我完全不知道可能出了什么问题。提前感谢您的帮助。

4

1 回答 1

2

好的,所以我设法解决了它,正如评论所指出的,你不能只打电话getAuthEntity,但你也需要用户。所以我将代码更改为以下

getProfileR :: Handler Html
getProfileR = do
    uid <- requireAuthId  
    user <- getAuthEntity uid 

    defaultLayout $ do
        -- setTitle . toHtml $ userIdent user <> "'s User page"
        setTitle . toHtml $ ("hola" :: Text )
        $(widgetFile "profile")

然后它起作用了,但是使用maybeAuthId而不是requireAuthId会出错。至于为什么我还不确定。

于 2018-04-29T20:29:33.327 回答