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

Configure Cookie session store #143

Merged
merged 1 commit into from
Jul 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,40 @@ Example:
Path.expand("../../../some/path/to/ssl/key.pem", __DIR__)
```

#### Configuration for Sessions

Phoenix supports a session cookie store that can be easily configured. Just
add the following configuration settings to your application's config module:

```elixir
# your_app/lib/config/prod.ex
defmodule YourApp.Config.Prod do
use YourApp.Config

config :plugs, cookies: true

config :cookies, key: "_your_app_key", secret: "valid_secret"
end
```

Then you can access session data from your application controllers.
NOTE: that `:key` and `:secret` are required options.

Example:

```elixir
defmodule Controllers.Pages do
use Phoenix.Controller

def show(conn, _params) do
conn = put_session(:foo, "bar")
foo = get_session(conn, :foo)

text conn, foo
end
end
```

### Mix Tasks

```console
Expand Down
3 changes: 2 additions & 1 deletion lib/phoenix/config/app.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ defmodule Phoenix.Config.App do
consider_all_requests_local: false

config :plugs, code_reload: false,
serve_static_assets: true
serve_static_assets: true,
cookies: false

config :logger, level: :error
end
7 changes: 7 additions & 0 deletions lib/phoenix/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ defmodule Phoenix.Router do
if config.plugs[:code_reload] do
plug Plugs.CodeReloader
end
if config.plugs[:cookies] do
key = Keyword.fetch!(config.cookies, :key)
secret = Keyword.fetch!(config.cookies, :secret)

plug Plug.Session, store: :cookie, key: key, secret: secret
end

plug :dispatch

def dispatch(conn, []) do
Expand Down
4 changes: 2 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%{"cowboy": {:git, "git://github.com/extend/cowboy.git", "ee3ad5e51005b453230804036e092e1d2ceebe4f", []},
%{"cowboy": {:git, "git://github.com/extend/cowboy.git", "1a71a733c37df70c15ebfd28157b10915cd738d1", []},
"cowlib": {:git, "git://github.com/extend/cowlib.git", "e2ffefe828b918486e2cd76e44c54ae9b62c616e", [ref: "0.6.2"]},
"ex_conf": {:package, "0.1.3"},
"ex_doc": {:git, "git://github.com/elixir-lang/ex_doc.git", "ddf1cae6bf331d8cfd5ded61b3b120293b80642d", []},
"ex_doc": {:git, "git://github.com/elixir-lang/ex_doc.git", "ca71b84b9c3c7e23b4b661e485a909aee3fdbc3b", []},
"inflex": {:package, "0.2.4"},
"jazz": {:package, "0.1.2"},
"plug": {:package, "0.5.1"},
Expand Down