Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save binarytemple/10d3bcc40b0e5eefc753 to your computer and use it in GitHub Desktop.

Select an option

Save binarytemple/10d3bcc40b0e5eefc753 to your computer and use it in GitHub Desktop.

Rather than redirect, replay?

Hi,

I'm working away on my first Chicagoboss demo application, and I'm trying to implement an identification, authorization, access workflow.


I started by implementing a controller_filter:

-module(training_filter).

-export([before_filter/2]).

before_filter(_FilterConfig, RequestContext) ->
  SessionId = proplists:get_value(session_id, RequestContext),
%%   Request = proplists:get_value(request, RequestContext),
  lager:log(info, self(), " before filter invoked", []),
  Action = proplists:get_value(action, RequestContext),
  case boss_session:get_session_data(SessionId, username) of
    undefined when Action == "login" ->
      {ok, RequestContext};
    undefined ->
      lager:log(error, self(), " failed to grab user from session/RequestContext ~p", [RequestContext]),
      boss_session:set_session_data(SessionId, original_request_context, RequestContext),
      {redirect, "/login"}
  end.

I then implemented a (laughably) trivial login module:

-module(training_security_controller, [Req, SessionID]).
-compile(export_all).

login('POST', []) ->
  User = Req:post_param("username"),
  Password = Req:post_param("password"),
  lager:log(info, self(), "~p ~p", [User, Password]),
  case Password of
    "foo" ->
      boss_session:set_session_data(SessionID, username, User),
      OriginalRequestContext = boss_session:get_session_data(SessionID, original_request_context),
      %but I really want to replay the original request
      {redirect, "/"}

  ;
    _ ->
      boss_flash:add(SessionID, notice, "Login", "Bad credentials, try again!"),
      {ok, login}
  end
;

login('GET', []) ->
  boss_flash:add(SessionID, notice, "Login", "Enter your password!"),
  {ok, login}.

What I would like to do is call the originally requested controller, with the OriginalRequestContext taken from the session, and let it handle the return/output.

Is such a thing possible ? I've done some digging but can't see any examples of this sort of thing.

Thanks,

Bryan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment