Skip to content
This repository has been archived by the owner on Aug 8, 2019. It is now read-only.

Commit

Permalink
Configure Cookie session store
Browse files Browse the repository at this point in the history
  • Loading branch information
hashrocketeer committed Jul 1, 2014
1 parent 3da1b63 commit 309de7f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
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

0 comments on commit 309de7f

Please sign in to comment.