Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use 'users' package #8

Open
BartAdv opened this issue Jul 24, 2015 · 17 comments
Open

Use 'users' package #8

BartAdv opened this issue Jul 24, 2015 · 17 comments

Comments

@BartAdv
Copy link

BartAdv commented Jul 24, 2015

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

@agrafix
Copy link
Owner

agrafix commented Jul 24, 2015

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...

@BartAdv
Copy link
Author

BartAdv commented Jul 24, 2015

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 BartAdv closed this as completed Jul 24, 2015
@BartAdv BartAdv reopened this Jul 24, 2015
@agrafix
Copy link
Owner

agrafix commented Jul 24, 2015

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

@BartAdv
Copy link
Author

BartAdv commented Jul 24, 2015

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

@agrafix
Copy link
Owner

agrafix commented Jul 24, 2015

You can add dependencies that are not on stackage to stack

@BartAdv
Copy link
Author

BartAdv commented Jul 24, 2015

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
Copy link
Author

BartAdv commented Jul 25, 2015

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
Copy link
Author

BartAdv commented Jul 27, 2015

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?

@agrafix
Copy link
Owner

agrafix commented Jul 27, 2015

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

@BartAdv
Copy link
Author

BartAdv commented Jul 27, 2015

@agrafix
Copy link
Owner

agrafix commented Jul 28, 2015

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.

@simonyangme
Copy link

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

@BartAdv
Copy link
Author

BartAdv commented Oct 19, 2015

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

@paldepind
Copy link

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.

@agrafix
Copy link
Owner

agrafix commented Oct 27, 2015

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

@paldepind
Copy link

As in something similar to what Persistent does?

What about simply moving the backend parameter to the end?

@agrafix
Copy link
Owner

agrafix commented Oct 27, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants