funblog icon indicating copy to clipboard operation
funblog copied to clipboard

Use 'users' package

Open BartAdv opened this issue 10 years ago • 17 comments

It's mentioned in Spock page in 'works great with' section, shouldn't it be used here?

BartAdv avatar Jul 24 '15 05:07 BartAdv

Yes, that's a great idea! Funblog is older than users (and has not been updated since), that why it does not use it yet. If you or anyone would like to give it a shot: go ahead :-) Otherwise I will look into it when I some time for that...

agrafix avatar Jul 24 '15 07:07 agrafix

Hm, I could take a stab as I was about to learn how to use Spock with some kind of authentication.

However, I am beaten by cabal issues :)

BartAdv avatar Jul 24 '15 21:07 BartAdv

You could try using https://github.com/commercialhaskell/stack to skip aroud those issues ;)

agrafix avatar Jul 24 '15 21:07 agrafix

Well, I did start with stack. I see that blaze-bootstrap is not on stackage for example.

BartAdv avatar Jul 24 '15 21:07 BartAdv

You can add dependencies that are not on stackage to stack

agrafix avatar Jul 24 '15 22:07 agrafix

I think it's not there for a reason, there is an upper bound on blaze-html dependency...sorry, it's just I am not proficient at all in those things:)

BartAdv avatar Jul 24 '15 22:07 BartAdv

Ok, finally built it. Had to fiddle a bit with extra-deps (packages with extra-dep: true) in stack to use my locally built dependencies (as they had some upper bound problems).

BartAdv avatar Jul 25 '15 09:07 BartAdv

Not sure if I'm on the correct way, but I've run into some problems, so I'm looking for help. I've defined a helper:

runUsers action =
  runQuery $ \conn ->
    let b = Users.Persistent (flip runSqlConn conn)
    in action b

Then I'm using it to register user, and it works fine. However, when trying to handle login:

    runUsers $ \b -> Users.authUser b (lr_user loginReq) (Users.PasswordPlain $ lr_password loginReq) 100

I receive sqlite3 returned ErrorError while attempting to perform step, and I nailed it to the fact that authUser uses withAuthUser where actionFn (in this case createToken) runs runPersistent again (so it's run inside another runPersistent).

Is this because of what I chose for runPersistent field in user backend?

BartAdv avatar Jul 27 '15 13:07 BartAdv

What's the error? Are you running two queries in parallel? Can you maybe share all your code?

agrafix avatar Jul 27 '15 20:07 agrafix

Hi, my WIP changes are here: https://github.com/BartAdv/funblog/commit/8dc4c91093669289c58782a4cef22968ab6e1186

BartAdv avatar Jul 27 '15 20:07 BartAdv

Hm, currently the tests for persistent run with sqlite, too, w/o a problem. (See https://github.com/agrafix/users/blob/767ebed500a6268826f8550c5e03b5b8223be82d/users-persistent/test/Spec.hs#L18 ). I'd love to look in to it in detail, but it will have to wait until the weekend.

agrafix avatar Jul 28 '15 06:07 agrafix

Is this still WIP? The repo by @BartAdv is now missing.

simonyangme avatar Oct 19 '15 09:10 simonyangme

I haven't investigated it further, I've forgot that repo had some stuff from me and I've removed it as I thought it's just a clean fork

BartAdv avatar Oct 19 '15 09:10 BartAdv

I'm currently using Spock together with Users. Maybe this will be useful.

My state looks like this:

data AppState = AppState { userBackend :: Persistent
                         }

And I'm using this helper function:

runUser :: (Persistent -> IO a) -> AppAction ctx foo a
runUser action = do
  state <- getState
  liftIO $ action (userBackend state)

And I use it like this:

-- Create a user
  post ("users") $ do -- Create user
    user <- jsonBody'
    res <- runUser (\b -> createUser (b :: Persistent) (user :: User String))
    case res of
      Right id -> do
        setStatus status200
        text $ pack . show . fromSqlKey $ (id :: UserId Persistent)
      Left err -> do
        setStatus status500
        text . pack . show $ err

I don't know if that is the best way though. The functions in Users takes the backend as their first argument so using runUser is a bit awkward. You have to use a lambda all the time.

paldepind avatar Oct 19 '15 09:10 paldepind

I think a good way to fix this would be to switch over to a monadic user interface holding the backend inside a reader.

agrafix avatar Oct 27 '15 09:10 agrafix

As in something similar to what Persistent does?

What about simply moving the backend parameter to the end?

paldepind avatar Oct 27 '15 10:10 paldepind

Yes, like persistent. Moving the parameter to the end will make the interface akward if you run multiple actions in runUser?

agrafix avatar Oct 27 '15 18:10 agrafix