Problem description
I’ve been unable to get a compiling example of using Yesod.Auth.Hardcoded. My problem is in trying to interrogate the user in a hamlet template. My Foundation.hs
is set up as per the documentation in the link for hardcoded. My handler looks like this:
getHomeR :: Handler Html
getHomeR = do
uid <- maybeAuthId
(widget, enctype) <- generateFormPost . renderBootstrap3 BootstrapBasicForm $ blurbForm Nothing
currentPost <- runDB $ selectFirst [] [Desc BlogId]
currentBlurb <- runDB $ selectFirst [] [Desc BlurbId]
defaultLayout $ do
setTitle "My site"
addScriptRemote "https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"
addScriptRemote "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/js/collapse.js"
$(widgetFile "homepage")
My site compiles and renders happily until I try to do anything useful with the uid
being assigned in the do
block above.
What I’ve tried
I’ve tried both the $maybe
and $case
constructs in the hamlet documentation. $maybe
looked like this:
$maybe user <- uid
<p>There's a name
$nothing
<p>No name
This succeeded regardless of whether I logged in as the hardcoded user.
The $case
version looked like this:
$case uid
$of Left _
<p>There's no name
$of Right username
<p>It worked
and failed with:
Exception when trying to run compile-time code:
Inside a $case there may only be $of. Use '$of _' for a wildcard.
Code: widgetFile "homepage"
In the splice: $(widgetFile "homepage")
Question(s)
Am I setting the uid
correctly in my handler code and, if so, how should I access the hardcoded SiteManager
in my templates?
2
Answers
As often posting the question made me think of an answer, though I'd still be grateful for any better ones. Using a combination of
$maybe
and$case
like so:got me the correct username. Please post another answer if there is a better way.
The AuthId type here is
Either UserId String
.A Right value represents a “hardcoded” user which does not appear in the User table. A Left value represents a User row in the Users table.
If you want to show a name for an AuthId, you can call
getAuthEntity
which returns anEither User SiteManager
and then process it like this: