Comments (18)
Plug.Session.fetch_session/1
is called every time Plug.Session
is invoked: https://github.com/elixir-lang/plug/blob/master/lib/plug/session.ex#L54.
If you call Plug.Conn.fetch_session/1
it will raise "cannot fetch session without a configured session plug"
if you don't have the session plug in the stack. If you call Plug.Conn.get_session/1
it will raise "session not fetched, call fetch_session/1"
. Since Plug.Session
already does what's needed, it seems like Plug.Conn.fetch_session/1
is redundant.
Thoughts?
from plug.
I don't know why the plug/2
macro fails but have you started the ETS table?
from plug.
I had left that out because it was throwing an argument error when added to start/2
. I just added it outside the module and it doesn't blow up. But, I still have to do:
get "/" do
opts = Plug.Session.init(store: :ets, key: "_my_app_session", secure: true, table: :session)
conn = Plug.Session.call(conn, opts)
conn = fetch_session(conn)
conn = put_session(conn, :foo, "bar")
foo = get_session(conn, :foo)
send_resp(conn, 200, foo)
end
Plus I forgot to mention, once you add those lines you can remove plug/2
completely and everything works the same. It's very curious and because other plugs seem to be working, I was thinking the problem was something I'm doing wrong. Especially, because there were no other reports of issues.
from plug.
Johnny, sounds like you already tried this? https://github.com/elixir-lang/plug/blob/master/lib/plug/session/ets.ex#L17
from plug.
@scrogson Yeah, this is a really simple plug app that's why I'm a little confused. Do you have a working example of sessions?
from plug.
I had left that out because it was throwing an argument error when added to start/2. I just added it outside the module and it doesn't blow up.
This is the issue. A ETS table is owned by the its process. Given compilation and runtime are distinct in Elixir, the process that created the ETS table (which is the one that compiled your code) is long gone. You need to create a ETS table in your supervisor or as part of your supervision tree and make it public.
There are two things we need to do from this discussion:
- Improve the all session docs to use cookie store by default
- Improve the ETS specific docs to mention the semantics above
from plug.
@josevalim There also doesn't appear to be a need to add Plug.Session as a plug.
I don't mind updating the docs but I want to make sure the functionality is correct. The tests indicate that you don't need to add Plug.Session
as a plug. Can you confirm the expected behavior?
from plug.
You need to add it as plug, otherwise fetch_session must fail.
from plug.
Ok, then there may be an issue because the following code runs without issue:
defmodule Plugger.Router do
import Plug.Conn
use Plug.Router
plug :match
plug :dispatch
@valid_secret String.duplicate("abcdef0123456789", 8)
get "/" do
opts = Plug.Session.init(store: :cookie, key: "_plugger", secret: @valid_secret)
conn = Plug.Session.call(conn, opts)
conn = fetch_session(conn)
conn = put_session(conn, :foo, "bar")
foo = get_session(conn, :foo)
send_resp(conn, 200, foo)
end
get "/hello" do
opts = Plug.Session.init(store: :cookie, key: "_plugger", secret: @valid_secret)
conn = Plug.Session.call(conn, opts)
conn = fetch_session(conn)
foo = get_session(conn, :foo)
send_resp(conn, 200, "Say hello to #{foo}")
end
match _ do
send_resp(conn, 404, "oops")
end
end
from plug.
What you are doing manaully in the two examples above is exactly what the plug macro does! The only difference is that the plug macro caches the Plug.Session.init/1
result.
from plug.
Kind of. I walked through the Plug.Builder
code and it appears to be doing it, however, it doesn't. The following code produces the error "cannot fetch session without a configured session plug".
defmodule Plugger.Router do
import Plug.Conn
use Plug.Router
plug :match
plug :dispatch
@valid_secret String.duplicate("abcdef0123456789", 8)
plug Plug.Session, store: :cookie, key: "_plugger", secret: @valid_secret
get "/" do
conn = fetch_session(conn)
conn = put_session(conn, :foo, "bar")
foo = get_session(conn, :foo)
send_resp(conn, 200, foo)
end
get "/hello" do
conn = fetch_session(conn)
foo = get_session(conn, :foo)
send_resp(conn, 200, "Say hello to #{foo}")
end
match _ do
send_resp(conn, 404, "oops")
end
end
from plug.
That's because the order matters, if you Plug.Session
after plug :dispatch
, the session will be added only after the route is dispatched!
from plug.
Ah!!! Ok, it works like a champ! As I said, there must be something I'm doing wrong because other plugs seem to be working. I know that there is mention that order is reversed but I don't believe there is anything that specifically says which order to add Plug.Session
.
I'll look at updating the docs today and providing some clear examples.
from plug.
Also, fetch_session/1
is called for you automatically.
On Tuesday, July 1, 2014, Johnny Winn [email protected] wrote:
Ah!!! Ok, it works like a champ! As I said, there must be something I'm
doing wrong because other plugs seem to be working. I know that there is
mention that order is reversed but I don't believe there is anything that
specifically says which order to add Plug.Session.I'll look at updating the docs today and providing some clear examples.
—
Reply to this email directly or view it on GitHub
#55 (comment).
from plug.
Thanks @josevalim! I went back to phoenix and tested a cookie session plug and it's working as expected. The gotcha was the order of loading. I'll work on the docs today to help clarify usage.
from plug.
@scrogson just figured that out too :)
from plug.
It is called automatically for you on put, but not on get afaik.
from plug.
@scrogson the idea is that a framework like Phoenix can always add the Session plug, but the session will only be fetch when you explicitly do it. This allows you to always plug it, but control exactly when it must be loaded.
from plug.
Related Issues (20)
- Mismatch spec for `Plug.Conn.Query.decode` HOT 1
- Plug.RequestId: put the `request_id` in `:private` or `:assigns`? HOT 1
- 1.15 changes decoding behaviour of `[]` HOT 5
- Request: Support MFA tuple for Plug.Session opts HOT 4
- Plug.Upload's random-tempdir generation is not very random HOT 1
- `strip_spaces` fails on `nil` since 1.15.0 HOT 1
- Plug.Conn.Query has changed the way it decodes parameters from 1.14 to 1.15 without warning HOT 1
- More flexible dynamic routes with `:` parameters HOT 2
- Compatibility issue with plug_crypto 2.0.0 HOT 3
- Error: There isn't enough space to open the popup HOT 1
- Docs inconsistency between Plug moduledoc and Plug.run HOT 1
- Plug.Upload: demonitor previous owner when giving away HOT 1
- Compilation issues on Erlang 26.x HOT 3
- html array items are dropped. HOT 1
- Plug.Conn is not closed when user closes the browser's tab (SSE) HOT 2
- setting max_age to nil in put_resp_cookie/4 causes crash HOT 1
- Sending a zip created on-the-fly via Plug.Conn.chunk fails after around 200Mb HOT 8
- Performance Regression in Plug.Conn.Cookies.decode/1 after Upgrading Elixir and Erlang HOT 3
- Plug.Router macros and functional plugs HOT 2
- Wrong editor links when running inside a container HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from plug.