Skip to content

Commit

Permalink
Tidied up handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamrhay committed Feb 22, 2015
1 parent 3b03172 commit 1663920
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 63 deletions.
47 changes: 27 additions & 20 deletions src/login_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,33 @@ init(_, Req, _Opts) ->

handle(Req, State=#state{}) ->
{Method, _} = cowboy_req:method(Req),
case Method of
<<"GET">> ->
{ok, Body} = login_dtl:render(),
{ok, Reply} = cowboy_req:reply(200, [{<<"content-type">>, <<"text/html">>}], Body, Req),
{ok, Reply, State};
<<"POST">> ->
{ok, Body, Req2} = cowboy_req:body_qs(Req),
Email = proplists:get_value(<<"email">>, Body),
Password = proplists:get_value(<<"password">>, Body),
case stormpath:login(Email, Password) of
{ok, UserInfo} ->
{ok, Req3} = cowboy_session:set(<<"user">>, UserInfo, Req2),
{ok, Reply} = cowboy_req:reply(302, [{<<"Location">>, <<"/user/info">>}], Req3),
{ok, Reply, State};
{error, _, Error} ->
{ok, Body2} = login_dtl:render([{error, Error}, {email, Email}]),
{ok, Reply} = cowboy_req:reply(200, [{<<"content-type">>, <<"text/html">>}], Body2, Req),
{ok, Reply, State}
end
end.
{ok, Reply} = handle_req(Method, Req),
{ok, Reply, State}.

handle_req(<<"GET">>, Req) ->
render_login_page([], Req);

handle_req(<<"POST">>, Req) ->
{ok, Body, Req2} = cowboy_req:body_qs(Req),
Email = proplists:get_value(<<"email">>, Body),
Password = proplists:get_value(<<"password">>, Body),
case stormpath:login(Email, Password) of
{ok, UserInfo} ->
{ok, Req3} = cowboy_session:set(<<"user">>, UserInfo, Req2),
redirect_to(<<"/user/info">>, Req3);
{error, _, Error} ->
render_login_page([{error, Error}, {email, Email}], Req2)
end;

handle_req(_, Req) ->
cowboy_req:reply(405, [], Req).

render_login_page(Data, Req) ->
{ok, Body2} = login_dtl:render(Data),
cowboy_req:reply(200, [{<<"content-type">>, <<"text/html">>}], Body2, Req).

redirect_to(Location, Req) ->
cowboy_req:reply(302, [{<<"Location">>, Location}], Req).

terminate(_Reason, _Req, _State) ->
ok.
60 changes: 32 additions & 28 deletions src/signup_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,38 @@ init(_, Req, _Opts) ->

handle(Req, State=#state{}) ->
{Method, _} = cowboy_req:method(Req),
case Method of
<<"GET">> ->
{ok, Body} = signup_dtl:render(),
{ok, Reply} = cowboy_req:reply(200, [{<<"content-type">>, <<"text/html">>}], Body, Req),
{ok, Reply, State};
<<"POST">> ->
{ok, Body, Req2} = cowboy_req:body_qs(Req),
Email = proplists:get_value(<<"email">>, Body),
Password = proplists:get_value(<<"password">>, Body),
GivenName = proplists:get_value(<<"givenName">>, Body),
Surname = proplists:get_value(<<"surname">>, Body),
Res = stormpath:create_user(#{
email => Email,
password => Password,
givenName => GivenName,
surname => Surname
}),
case Res of
{ok, UserInfo} ->
{ok, Req3} = cowboy_session:set(<<"user">>, UserInfo, Req2),
{ok, Reply} = cowboy_req:reply(302, [{<<"Location">>, <<"/user/info">>}], Req3),
{ok, Reply, State};
{error, _, Error} ->
{ok, Body2} = signup_dtl:render([{error, Error}, {email, Email}, {given_name, GivenName}, {surname, Surname}]),
{ok, Reply} = cowboy_req:reply(200, [{<<"content-type">>, <<"text/html">>}], Body2, Req),
{ok, Reply, State}
end
end.
{ok, Reply} = handle_req(Method, Req),
{ok, Reply, State}.

handle_req(<<"GET">>, Req) ->
render_signup_page([], Req);

handle_req(<<"POST">>, Req) ->
{ok, Body, Req2} = cowboy_req:body_qs(Req),
Email = proplists:get_value(<<"email">>, Body),
Password = proplists:get_value(<<"password">>, Body),
GivenName = proplists:get_value(<<"givenName">>, Body),
Surname = proplists:get_value(<<"surname">>, Body),
Res = stormpath:create_user(#{
email => Email,
password => Password,
givenName => GivenName,
surname => Surname
}),
case Res of
{ok, UserInfo, Req2} ->
{ok, Req3} = cowboy_session:set(<<"user">>, UserInfo, Req2),
cowboy_req:reply(302, [{<<"Location">>, <<"/user/info">>}], Req3);
{error, _, Error} ->
render_signup_page([{error, Error}, {email, Email}, {given_name, GivenName}, {surname, Surname}], Req2)
end;

handle_req(_, Req) ->
cowboy_req:reply(405, [], Req).

render_signup_page(Data, Req) ->
{ok, Body2} = signup_dtl:render(Data),
cowboy_req:reply(200, [{<<"content-type">>, <<"text/html">>}], Body2, Req).

terminate(_Reason, _Req, _State) ->
ok.
29 changes: 14 additions & 15 deletions src/user_info_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@ init(_, Req, _Opts) ->

handle(Req, State=#state{}) ->
{Method, _} = cowboy_req:method(Req),
case Method of
<<"GET">> ->
{User, Req2} = cowboy_session:get(<<"user">>, Req),
case User of
undefined ->
{ok, Reply} = cowboy_req:reply(302, [{<<"Location">>, <<"/login">>}], Req2),
{ok, Reply, State};
_ ->
{ok, Body} = user_info_dtl:render(maps:to_list(User)),
{ok, Reply} = cowboy_req:reply(200, [{<<"content-type">>, <<"text/html">>}], Body, Req2),
{ok, Reply, State}
end;
{ok, Reply} = handle_req(Method, Req),
{ok, Reply, State}.

handle_req(<<"GET">>, Req) ->
{User, Req2} = cowboy_session:get(<<"user">>, Req),
case User of
undefined ->
cowboy_req:reply(302, [{<<"Location">>, <<"/login">>}], Req2);
_ ->
{ok, Reply} = cowboy_req:reply(405, [], Req),
{ok, Reply, State}
end.
{ok, Body} = user_info_dtl:render(User),
cowboy_req:reply(200, [{<<"content-type">>, <<"text/html">>}], Body, Req2)
end;

handle_req(_, Req) ->
cowboy_req:reply(405, [], Req).

terminate(_Reason, _Req, _State) ->
ok.

0 comments on commit 1663920

Please sign in to comment.