<div dir="ltr">I also found the answer to my own question: custom middleware<div><br></div><div>I just created:</div><div><br></div><div><div>   1 -module(authentication_middleware).</div><div>   2</div><div>   3 -behaviour(cowboy_middleware).</div>
<div>   4</div><div>   5 -export([execute/2]).</div><div>   6</div><div>   7 execute(Req, Env) -></div><div>   8</div><div>   9     {Path, Req1} = cowboy_req:path(Req),</div><div>  10</div><div>  11     case Path of</div>
<div>  12         <<"/login.html">> -></div><div>  13             {ok, Req1, Env};</div><div>  14         <<"/do_login">> -></div><div>  15             {ok, Req1, Env};</div>
<div>  16         _ -></div><div>  17             case id3as_security:is_request_authenticated(Req1) of</div><div>  18                 {error, eauth, Req2} -></div><div>  19                     {ok, Req4} = cowboy_req:reply(303, [{<<"Location">>, <<"/login.html">>}], "", Req2),</div>
<div>  20                     {halt, Req4};</div><div>  21                 {authenticated, _Id, Req2} -></div><div>  22                    {ok, Req2, Env}</div><div>  23             end</div><div>  24     end.</div></div>
<div><br></div><div>And put this between the cowboy_router and cowboy_handler and life is all good.</div><div><br></div><div>-Mark</div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Mar 6, 2014 at 12:47 AM, Mark Nijhof <span dir="ltr"><<a href="mailto:mark.nijhof@cre8ivethought.com" target="_blank">mark.nijhof@cre8ivethought.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi,<div><br></div><div>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).</div>

<div><br></div><div>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?</div><div><br></div><div>Cheers,</div><div><br></div><div>

-Mark<span class="HOEnZb"><font color="#888888"><br clear="all"><div><br></div>-- <br><div dir="ltr">Mark Nijhof<br><div><div>t:   <a href="https://twitter.com/MarkNijhof" target="_blank">@MarkNijhof</a><br>s:  marknijhof</div>
</div><div><br></div></div>
</font></span></div></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div dir="ltr">Mark Nijhof<br><div><div>t:   <a href="https://twitter.com/MarkNijhof" target="_blank">@MarkNijhof</a><br>s:  marknijhof</div></div><div><br></div>
</div>
</div>