Skip to content

bgrusnak/cowboy_session

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cowboy session

Usage:

First you need to get dependencies (gproc and erlang-uuid) and compile the code:

make

or

make deps
make compile

Then you need the cowboy_session application to be started:

cowboy_session:start().

or manually

application:start(gproc),
application:start(uuid),
application:start(cowboy_session).

If you want session to be started for each request then use function cowboy_session:execute/2 as Cowboy middleware callback:

	{ok, _} = cowboy:start_clear(http, [{port, 8080}], #{
		env => #{dispatch => Dispatch},
		middlewares => [cowboy_session, cowboy_router,  cowboy_handler]
	}),

otherwise first call to cowboy_session:set/3 or cowboy_session:get/2/3 will initialize session

Set some key to some value:

{ok, Req2} = cowboy_session:set(Key, Value, Req)

Get previously setted key:

{Value, Req3} = cowboy_session:get(Key, Req2)

Get some key using default value to return if key will not be found:

{Value, Req3} = cowboy_session:get(Key, Default, Req2)

You can configure cowboy_session with following params:

  • cookie_name - cookie name. Default: <<session>>
  • cookie_options - list of cookie options. Default: [{path, <<"/">>}]
  • expire - session expiration time, in seconds. Default: 1440

To change any of them use cowboy_session_config:set/1/2:

ok = cowboy_session_config:set(cookie_options, [{path, <<"/">>}, {domain, <<".site.com">>}]),
ok = cowboy_session_config:set([
	{cookie_name, <<"new_cookie_name">>},
	{expire, 86400}
])

Also you can use your own storage for sessions. Just implement it (see behaviour cowboy_session_storage or cowboy_session_storage_ets as an example) and run cowboy_session_config:update_storage(New_storage_name) - this will call stop/0 callback for currently running storage and will initialize the new one. Default (and currently the only one) session storage is cowboy_session_storage_ets.

About

Cowboy session management middleware

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Erlang 98.7%
  • Makefile 1.3%