From 9ebc1a57335e3b183be9a0494cdc377a19219222 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Tue, 15 Mar 2016 00:11:28 -0400 Subject: initial session support --- src/guff/handlers/api.cr | 11 ++++++++++- src/guff/handlers/session.cr | 21 +++++++++++++++++++++ src/guff/handlers/test-auth.cr | 10 ---------- 3 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 src/guff/handlers/session.cr (limited to 'src/guff/handlers') diff --git a/src/guff/handlers/api.cr b/src/guff/handlers/api.cr index 4401f75..6f5c528 100644 --- a/src/guff/handlers/api.cr +++ b/src/guff/handlers/api.cr @@ -10,8 +10,17 @@ private macro define_method_calls(hash) case method {% for method in methods %} when "{{ method }}" + params = case context.request.method + when "GET" + context.request.query_params + when "POST" + HTTP::Params.parse((context.request.body || "") as String) + else + raise "unsupported method" + end + do_{{ namespace.id }}_{{ method }}(context, get_method_args( - context.request.query_params, + params, "{{ namespace.id }}", "{{ method.id }}" )).to_json diff --git a/src/guff/handlers/session.cr b/src/guff/handlers/session.cr new file mode 100644 index 0000000..bc7a5bc --- /dev/null +++ b/src/guff/handlers/session.cr @@ -0,0 +1,21 @@ +require "json" +require "../handler" + +class Guff::Handlers::SessionHandler < Guff::Handler + def call(context : HTTP::Server::Context) + if cookie = context.request.cookies["guff_session"]? + # get session id + sid = cookie.value + puts "DEBUG: session_id = #{sid}" + context.request.headers["x-guff-session-id"] = sid + end + + call_next(context) + + if sid = context.request.headers["x-guff-session-id"]? + # FIXME: need way to delete cookie + cookie = HTTP::Cookie.new("guff_session", sid) + context.response.headers["set-cookie"] = cookie.to_set_cookie_header + end + end +end diff --git a/src/guff/handlers/test-auth.cr b/src/guff/handlers/test-auth.cr index 5cf5769..a0238f2 100644 --- a/src/guff/handlers/test-auth.cr +++ b/src/guff/handlers/test-auth.cr @@ -8,8 +8,6 @@ class Guff::Handlers::TestAuthHandler < Guff::Handler case context.request.method when "GET", "HEAD" draw_page(context) - when "POST" - set_auth(context) else raise "unknown method" end @@ -21,12 +19,4 @@ class Guff::Handlers::TestAuthHandler < Guff::Handler private def draw_page(context) TestAuthHTMLView.run(@models, context) end - - private def set_auth(context) - params = HTTP::Params.parse(context.request.body as String) - - # TODO: extract user id and set it in session - - redirect(context, "/test/blog") - end end -- cgit v1.2.3