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

Access Rails session from Faraday middleware #15

Closed
evansj opened this issue May 30, 2012 · 6 comments
Closed

Access Rails session from Faraday middleware #15

evansj opened this issue May 30, 2012 · 6 comments

Comments

@evansj
Copy link

evansj commented May 30, 2012

The README file has the following comment:

Her::API.setup :url => "https://api.example.com" do |builder|
  # This token could be stored in the client session
  builder.use MyAuthentication, :token => "..."
  # ...
end

Can you give me an example of how you could access the current Rails session from that code block? I've put the code in a Rails initialiser file.

Thanks

@remi
Copy link
Owner

remi commented May 30, 2012

Well that's actually a bit more complicated than I thought (since you can only interact with the session in a controller, but here’s how I would do it. I haven’t tested it in Rails though, it’s just

[redacted]
See https://github.com/remiprev/her#authentication for a better example

Hope that helps!

@remi
Copy link
Owner

remi commented Jun 27, 2012

I’m going to close this issue since it wasn’t active in the last month. Hopefully you found a solution that fit your needs!

@remi remi closed this as completed Jun 27, 2012
@incorvia
Copy link

@remiprev, just a quick question about the solution you provided. Is that thread safe? if the class MyApp is long lived and someone else comes in and sets the current_user before the FaradayMiddleware has access to it.. wouldn't that be bad? I ask because I don't know.

@remi
Copy link
Owner

remi commented Dec 3, 2012

Indeed, this is not thread safe. A better solution would be to use Thread.current (or something like this) to store the current user, like the audited gem does.

@remi
Copy link
Owner

remi commented Dec 5, 2012

@incorvia I added a thread-safe example in README.md. I’m going to remove the non-thread-safe example above :)

@incorvia
Copy link

incorvia commented Dec 5, 2012

Thank you @remiprev a very good example and a great resource for future use.

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

3 participants