aboutsummaryrefslogtreecommitdiff
path: root/src/guff/handlers/test-auth.cr
blob: 5cf5769028ef250e8eaac9d3f29593648448efc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
require "../handler"
require "../views/html/test/auth"

class Guff::Handlers::TestAuthHandler < Guff::Handler
  PATH_RE = %r{^/test/auth/?$}
  def call(context : HTTP::Server::Context)
    if PATH_RE.match(context.request.path.not_nil!)
      case context.request.method
      when "GET", "HEAD"
        draw_page(context)
      when "POST"
        set_auth(context)
      else
        raise "unknown method"
      end
    else
      call_next(context)
    end
  end

  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