问题
I'm playing with scaffolded site and i want to send user to the registration page after he logged in for the first time with OpenID or Google Account.
I've came up with this:
getAuthId creds = runDB $ do
x ← getBy $ UniqueUser $ credsIdent creds
case x of
Just (Entity uid _) → return $ Just uid
Nothing → do
return $ Just $ Key (PersistInt64 0)
And in HomeR
handler i check for UserId value, showing registration form in case of zero.
This approach works, but seems hackish. What is the proper way to deal with such problem?
回答1:
I would recommend splitting the information up into two entities: a User
entity that tracks the user's credentials, and a Profile
entity containing the registration information. For example:
User
ident Text
UniqueUser ident
Profile
user UserId
displayName Text
UniqueProfile user
In getAuthId
, you'll either return an existing UserId
, or if one doesn't exist, create a new entry. In HomeR
, you'll get if there's a Profile
(getBy $ UniqueProfile uid
), and if not, display the registration form.
来源:https://stackoverflow.com/questions/11760958/yesod-auth-redirecting-user-to-registration-page