haskellyesodyesod-forms

How get the user ID from the Session at (Yesod / Haskell Project


guys i got a little projet and i need to extrat de ID of the user from the Session.

I can't put it in a Text/Int because it says that the Session carry an Key (Sql Key i think) how can i converte it to Int to use in other methods from my project

I Tried to do it to recover the ID from session

getInicioR :: Handler Html
getInicioR = do
        uid <- lookupSession "_ID"
        user <- runDB $ get404 uid 

Shows the follow error message:

Couldn't match expected type ‘Key t0’ with actual type ‘Maybe Text’
In the first argument of ‘get404’, namely ‘uid’
In the second argument of ‘($)’, namely ‘get404 uid’

Solution

  • Use keyToValues to get a list of PersistValue values.

    keyToValues :: Key record -> [PersistValue]
    

    If you know, for instance, that the key is a Text value, then your list will consist of a single PersistText value and you could proceed like this:

    do uid <- lookupSession "_ID"
       let pvals = keyToValues uid
           [ PersistText txt ] = pvals
       liftIO $ print pvals            -- to see what pvals is
       -- now txt is a Text value
       ...