before_ and require_login not playing nice...

81 views
Skip to first unread message

jim rosenblum

unread,
Nov 1, 2013, 11:32:09 AM11/1/13
to chica...@googlegroups.com
I am having trouble with authentication. I am doing what many others are doing taken from the evening with chicago boss tutorial.

In my require_login, if I return {ok, User} as in 

require_login(Req) ->

    case Req:cookie("user_id") of
        undefined -> {redirect, "/user/login"};
        Id ->
            case boss_db:find(Id) of
                undefined -> {redirect, "/user/login"};
                SentryUser ->
                    case SentryUser:session_identifier() =:= Req:cookie("session_id") of
                        false -> {redirect, "/user/login"};
                        true ->  {ok, SentryUser}
                    end
            end
     end.

I get: 
{function_clause,[{proplists,delete,[language,{sentry_user,"sentry_user-3","SMITH","9c2e39128e567fede96662c87d1e971a","SM...@xxx.com",true,true}],[{file,"proplists.erl"},{line,366}]},{boss_lang_filter,before_filter,2,[{file,"src/boss/filters/boss_lang_filter.erl"},{line,8}]},{boss_web_controller,'-apply_before_filters/3-fun-0-',5,[{file,"src/boss/boss_web_controller.erl"},{line,935}]},{lists,foldl,3,[{file,"lists.erl"},{line,1248}]},{boss_web_controller,execute_action,4,[{file,"src/boss/boss_web_controller.erl"},{line,877}]},{boss_web_controller,process_dynamic_request,4,[{file,"src/boss/boss_web_controller.erl"},{line,536}]},{boss_web_controller,process_request,4,[{file,"src/boss/boss_web_controller.erl"},{line,525}]},{timer,tc,3,[{file,"timer.erl"},{line,194}]}]}

which implies something is expecting the returned User to be a proplist? But, I know this isn't correct either.

Any smart ideas

kotedo

unread,
Nov 1, 2013, 11:40:00 AM11/1/13
to chica...@googlegroups.com
How does your controller code look like where you use that before_ filter result?

—Kai

--
You received this message because you are subscribed to the Google Groups "ChicagoBoss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chicagoboss...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

jim rosenblum

unread,
Nov 1, 2013, 12:22:19 PM11/1/13
to chica...@googlegroups.com
Kai,
 
Thank you so much for trying to help me!
 
if I navigate to localhost:8001/status/customers I crash
 
I know that the before_ is getting results (I was able to print them out before returning them), and I know that I crash before the first line of the customers('GET', [], SUser) function.
 
My controller code looks like this (the before_ is the last line of code).
 
-module(ehc_server_status_controller, [Req]).
-compile(export_all).

customers('GET', [], SUser) ->
    Customers = boss_db:find(customer, []),
    Selected = hd(Customers),
    SelectedId = Selected:id(),
    Timestamp = boss_mq:now("changes"),
    R = boss_db:count(facility,[{status, equals, "red"}]),
    Y = boss_db:count(facility,[{status, equals, "yellow"}]),
    {ok, [{customers, Customers},{selected, Selected},{selected_id, SelectedId},
   {facilities, []}, {timestamp, Timestamp},{reds, R},
   {yellows, Y},{s_user, SUser}]};
 
customers('GET', ["since", LastTimeStamp], SUser) ->
    {ok, Timestamp, Changes} = boss_mq:pull("changes", list_to_integer(LastTimeStamp)),
    Customers = [Customer || {customer, Customer} <- Changes],
    Facilities = [Facility || {facility, Facility} <- Changes],
    R = boss_db:count(facility,[{status, equals, "red"}]),
    Y = boss_db:count(facility,[{status, equals, "yellow"}]),
    {json, [{customers, Customers},{facilities, Facilities}, {timestamp, Timestamp},
     {reds, R},{yellows, Y}, {s_user, SUser}]};

customers('GET', ["facilities", CustId], SUser) ->
    Facilities =  boss_db:find(facility, [customer_id, equals, CustId]),
    [Customer] = boss_db:find(customer, [id, equals, CustId]),
    {render_other, [{action, "facilities"}],[{facilities, Facilities},{customer, Customer}, {s_user, SUser}]}.


facility('GET', [LastTimeStamp]) ->
    {ok, Timestamp, Facilities} = boss_mq:pull("facility", list_to_integer(LastTimeStamp)),
    R = boss_db:find(facility,[{status, equals, "red"}]),
    Y = boss_db:find(facility,[{status, equals, "yellow"}]),
    {json, [{facilities, Facilities},{f_timestamp, Timestamp},
     {reds, length(R)},{yellows, length(Y)}]}.
 

response('GET', [RespId]) ->
    [R] = boss_db:find(response, [id, equals, RespId]),
    {json, [{response, R}]}.
 
responses('GET', [FacId]) ->
    [F] =  boss_db:find(facility, [id, equals, FacId]),
    R = F:responses(),
    Fs = boss_db:find(failures, [facility_id, equals, F:id()]),
    {ok, [{facility, F}, {responses, R}, {failures, Fs}]}.
 
pull('GET',["facilities", CustId]) ->
    F = boss_db:find(facility,[{customer_id, equals, CustId}]),
    {json, [{facilities,F}]};
 
pull('GET', [LastTimestamp]) ->
    {ok, Timestamp, Responses} = boss_mq:pull("new-response",
           list_to_integer(LastTimestamp)),
    {json, [{timestamp, Timestamp}, {responses, Responses}]}.
 
before_("customers",_, _)->
    user_lib:require_login(Req).


--
You received this message because you are subscribed to a topic in the Google Groups "ChicagoBoss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/chicagoboss/AKpDJAEpi6U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to chicagoboss...@googlegroups.com.

kotedo

unread,
Nov 1, 2013, 1:32:41 PM11/1/13
to jim rosenblum, chica...@googlegroups.com
This is how I usually do the pattern match:

before_(Action,_,_) ->
    case Action of
        "customers" ->
            user_lib:require_login(Req);
        _ ->
            %% Do something else or nothing ..                                                                                                    
            {ok, [{dummy, Dummy}]}
    end.

otherwise ... 
before_(“customers”, _, _) ->
.... code ...
last line of code;
before_(_,_,_) ->
... code ...
last line of code and last line of pattern match .   %% <— Note the period?  End of pattern match

On Nov 1, 2013, at 12:22, jim rosenblum <jim.ro...@gmail.com> wrote:

user_lib:require_login(Req).

jim rosenblum

unread,
Nov 1, 2013, 2:13:48 PM11/1/13
to kotedo, chica...@googlegroups.com
Again, just the presence of that before_/3 causes the error. Even when my Action doesn't make use of the function, I get the error...  

Lucas Introne

unread,
Nov 12, 2013, 11:46:22 AM11/12/13
to chica...@googlegroups.com, kotedo
hi jim,

things have recently changed, and the tutorial hasn't yet been updated to match.  the before filters are indeed expected to return a proplist.  check out the documentation here: https://github.com/evanmiller/ChicagoBoss/blob/master/README_FILTERS.md

hope this helps,
-lucas-

jim rosenblum

unread,
Nov 14, 2013, 4:29:01 PM11/14/13
to chica...@googlegroups.com, kotedo

thanks

ENTR0PY

unread,
Nov 4, 2014, 9:54:40 PM11/4/14
to chica...@googlegroups.com, kot...@gmail.com
I am also having the same problem where the very presence of the before_(_,_,_) causes problems.
It seems an error happends in the auth check and the redirect failes to work

controler call:
dashboard('GET', [], Account) -> {ok,[{}]}.

%%run this before letting a user do stuff
before_(_, _, _) -> user_lib:require_login(Req).



user_lib function

require_login(Req) ->
  case Req:cookie("session_id") of
    undefined -> {ok, [{controller, "user"}, {action, "login"}]};
    Sess ->
      case Req:cookie("user_id") of
        Id ->
          case boss_db:find(Id) of
            Account ->
              try Account:session_identifier() of
                Identifier ->
                  case Sess =:= Identifier of
                    false -> {ok, [{controller, "user"}, {action, "login"}]};
                    true -> {ok, Account}
                  end
              catch
                _:Msg -> {redirect, [{controller, "user"}, {action, "login"}, {error_message, Msg}]}
              end
          end;
        undefined -> {redirect, [{controller, "user"}, {action, "login"}]}
      end;
    undefined -> {redirect, [{controller, "user"}, {action, "login"}]}
  end.

, Note , the code here is contined effort to get the app to wrok ..... i get too may redirect errors in chrome


Reply all
Reply to author
Forward
0 new messages