代码之家  ›  专栏  ›  技术社区  ›  Daiwen

是的根据应用设置进行硬编码身份验证

  •  1
  • Daiwen  · 技术社区  · 6 年前

    我正在尝试设置硬编码身份验证以依赖在中指定的登录信息 config/settings.yml 从Yesod脚手架上。我写了以下代码:

    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)
    

    从这些定义看来,我应该有办法 盖耶索德 自动处理程序 但我不知道怎么做。


    编辑: 我已经更新了 yesod 以及最新版本的脚手架。我也修改了 验证密码 如下:

    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
        |
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Daiwen    6 年前

    错误来自于我试图修改 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