<div dir="ltr">You might be able to do something with a cowboy middleware:<div><br></div><div><a href="http://ninenines.eu/docs/en/cowboy/HEAD/guide/middlewares/" target="_blank">http://ninenines.eu/docs/en/cowboy/HEAD/guide/middlewares/</a><br></div><div><br></div><div>Or, failing that, you could just extract that case to a module somewhere,</div><div>and pass in "do something here" as a fun.</div><div class="gmail_extra"><br><div class="gmail_quote">On 7 December 2015 at 09:03, avinash D'silva <span dir="ltr"><<a href="mailto:evnix.com@gmail.com" target="_blank">evnix.com@gmail.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"><div>Hi,</div><div><br></div><div><br></div><div>I am using cowboy+cowboy_session for building websites. </div><div><br></div><div>each route has something similar as shown below:</div><div><br></div><div><br></div><div><div>-module(get_pageX).</div><div>-behaviour(cowboy_http_handler).</div><div><br></div><div>-export([init/3]).</div><div>-export([handle/2]).</div><div>-export([terminate/3]).</div><div><br></div><div>-record(state, {</div><div>}).</div><div><br></div><div>init(_, Req, _Opts) -></div><div><span style="white-space:pre-wrap"> </span>{ok, Req, #state{}}.</div><div><br></div><div>handle(Req, State=#state{}) -></div><div><br></div><div> </div><div><br></div><div> case cowboy_session:get("loggedin", Req) of</div><div><br></div><div> true -> </div><div> #do something here</div><div> </div><div> _ -></div><div> #not logged in.</div><div><br></div><div> end,</div><div><br></div><div><br></div><div><br></div><div><span style="white-space:pre-wrap"> </span>{ok, ReqFinal} = cowboy_req:reply(200,</div><div><span style="white-space:pre-wrap"> </span>[{<<"content-type">>, <<"text/html">>}],</div><div> <span style="white-space:pre-wrap"> <<"Some response">></span>,</div><div> <span style="white-space:pre-wrap"> </span>Req),</div><div><br></div><div><span style="white-space:pre-wrap"> </span>{ok, ReqFinal, State}.</div><div><br></div><div>terminate(_Reason, _Req, _State) -></div><div><span style="white-space:pre-wrap"> </span>ok.</div></div><div><br></div><div><br></div><div>The above CASE(code) is repeated for every route to check if the user is logged in, is there a better way to organize this?</div><div><br></div><div>what I am looking for is a way to check if the user is logged in and if not, redirect to login route.</div><div><br></div><div><br></div><div>PS: I am new to Erlang</div><div><br></div><div><br></div><div>Regards,</div><div>Avinash D' Silva <br></div><div><br></div><div><br></div>
</div>
<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div></div>