gpt4 book ai didi

haskell - Yesod 1.4 的基本电子邮件/密码身份验证

转载 作者:行者123 更新时间:2023-12-02 01:33:55 26 4
gpt4 key购买 nike

我正在尝试整合 Yesod.Auth.Email进入 Yesod 1.4 的默认脚手架(你用 stack exec -- yesod init --bare && stack init 得到的)。所以我从authentication and authorization example from the Yesod book复制了相关部分到我的 Foundation.hs并开始将其调整为新的 Yesod 版本:

instance YesodAuthEmail App where
type AuthEmailId App = UserId

afterPasswordRoute _ = HomeR

addUnverified email verkey = do
userId <- runDB $ insert $ User email Nothing
runDB $ insert $ Email email (Just userId) (Just verkey)
return userId

sendVerifyEmail email _ verurl =
liftIO $ renderSendMail (emptyMail $ Address Nothing "noreply")
{ mailTo = [Address Nothing email]
, mailHeaders =
[ ("Subject", "Verify your email address")
]
, mailParts = [[textPart, htmlPart]]
}
where
textPart = Part
{ partType = "text/plain; charset=utf-8"
, partEncoding = None
, partFilename = Nothing
, partContent = Data.Text.Lazy.Encoding.encodeUtf8
[stext|
Please confirm your email address by clicking on the link below.

#{verurl}

Thank you
|]
, partHeaders = []
}
htmlPart = Part
{ partType = "text/html; charset=utf-8"
, partEncoding = None
, partFilename = Nothing
, partContent = withUrlRenderer
[shamlet|
<p>Please confirm your email address by clicking on the link below.
<p>
<a href=#{verurl}>#{verurl}
<p>Thank you
|]
, partHeaders = []
}
getVerifyKey = runDB . fmap (join . fmap emailVerkey) . getBy
setVerifyKey uid key = runDB $ update uid [EmailVerkey =. Just key]
verifyAccount uid = runDB $ do
mu <- get uid
case mu of
Nothing -> return Nothing
Just u -> do
update uid [EmailVerkey =. Nothing]
return $ Just uid
getPassword = runDB . fmap (join . fmap userPassword) . get
setPassword uid pass = runDB $ update uid [UserPassword =. Just pass]
getEmailCreds email = runDB $ do
mu <- getBy $ UniqueUser email
case mu of
Nothing -> return Nothing
Just (Entity uid u) -> return $ Just EmailCreds
{ emailCredsId = uid
, emailCredsAuthId = Just uid
, emailCredsStatus = isJust $ userPassword u
, emailCredsVerkey = emailVerkey u
, emailCredsEmail = email
}
getEmail = runDB . fmap (fmap emailEmail) . get
config/modelsyesod init 相同脚手架:
User
ident Text
password Text Maybe
UniqueUser ident
deriving Typeable
Email
email Text
user UserId Maybe
verkey Text Maybe
UniqueEmail email

我收到一堆类型错误,主要是因为函数(例如 getVerifyKey )应该接受/返回 User s,不是 Email s。

为什么他们改为使用单独的 Email表而不是保留 verkeyemail User 中的列表呢?我相当肯定我的用户永远不需要注册多个电子邮件地址。您是否建议保持这种方式,以便与不同的身份验证插件配合使用?

最佳答案

看起来你可以切换 UserIdEmailIdtype AuthEmailId App = UserId它可能会毫无问题地工作。

我认为脚手架表是出于说明目的(唯一列、连接)并展示了用户和电子邮件之间很好的概念分离,但并不是绝对必要的。

关于haskell - Yesod 1.4 的基本电子邮件/密码身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32354892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com