[99s-extend] Cowboy pre request filter
Mark Nijhof
mark.nijhof at cre8ivethought.com
Thu Mar 6 10:52:01 CET 2014
I also found the answer to my own question: custom middleware
I just created:
1 -module(authentication_middleware).
2
3 -behaviour(cowboy_middleware).
4
5 -export([execute/2]).
6
7 execute(Req, Env) ->
8
9 {Path, Req1} = cowboy_req:path(Req),
10
11 case Path of
12 <<"/login.html">> ->
13 {ok, Req1, Env};
14 <<"/do_login">> ->
15 {ok, Req1, Env};
16 _ ->
17 case id3as_security:is_request_authenticated(Req1) of
18 {error, eauth, Req2} ->
19 {ok, Req4} = cowboy_req:reply(303,
[{<<"Location">>, <<"/login.html">>}], "", Req2),
20 {halt, Req4};
21 {authenticated, _Id, Req2} ->
22 {ok, Req2, Env}
23 end
24 end.
And put this between the cowboy_router and cowboy_handler and life is all
good.
-Mark
On Thu, Mar 6, 2014 at 12:47 AM, Mark Nijhof <mark.nijhof at cre8ivethought.com
> wrote:
> Hi,
>
> I want to create a module that basically sits between the incoming request
> and the http handler for that request to ensure a request is authenticated
> (using a cookie), if the request is not authenticated then I like to
> redirect to a specific login page (which should not be filtered).
>
> Is this possible with Cowboy? Should I use the onrequest hook (not sure if
> I can force redirects from there) for that or is there a better way?
>
> Cheers,
>
> -Mark
>
> --
> Mark Nijhof
> t: @MarkNijhof <https://twitter.com/MarkNijhof>
> s: marknijhof
>
>
--
Mark Nijhof
t: @MarkNijhof <https://twitter.com/MarkNijhof>
s: marknijhof
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ninenines.eu/archives/extend/attachments/20140306/24422ef2/attachment.html>
More information about the Extend
mailing list