GithubHelp home page GithubHelp logo

cowboy_session's Introduction

Sessions for Cowboy

Provides a session API for the Cowboy HTTP server.

How do I use this thing?

First you need a session handler module:

-module(my_session_handler).
-extends(cowboy_session_default_handler).
-export([validate/1, init/2, handle/3]).

validate(_Session) ->
   ?MODULE:generate().

init(_Session, _SessionName) ->
    dict:new().

handle({get, Key}, _Session, State) ->
    Value = case dict:find(Key, State) of
                error -> undefined;
                {ok, V} -> V
            end,

    {Value, State};

handle({set, Key, Value}, _Session, State) ->
    {ok, dict:store(Key, Value, State) };

handle(dict, _Session, State) ->
    {State, State}.

then you can use the session from a cowboy HTTP handler as follows:

    %% get the session
    {Session, Req2} = cowboy_session:get_or_create(Req, my_session_handler),
    %% set the user id
    ok = cowboy_session_server:command(Session, {set, user_id, Id}),
    %% later get the session id later
    UserId = cowboy_session_server:command(Session, {get, user_id}),

This is how I use it, but you're free to store your session in any way you like.

NOTE: I've been unable to get it to with under R16

Tests

make test

Todo

  • Implement the signed stateless session

cowboy_session's People

Contributors

dparnell avatar hairyhum avatar omarkj avatar yrashk avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.