diff --git a/src/handlers/auth_handler.cr b/src/handlers/auth_handler.cr index 1482ec9..7fe3fce 100644 --- a/src/handlers/auth_handler.cr +++ b/src/handlers/auth_handler.cr @@ -43,12 +43,7 @@ class AuthHandler < Kemal::Handler token = verify_user value return false if token.nil? - # TODO use port number in token key - cookie = HTTP::Cookie.new "token", token - cookie.path = Config.current.base_url - cookie.expires = Time.local.shift years: 1 - env.response.cookies << cookie - + set_token_cookie env, token return true end end diff --git a/src/routes/main.cr b/src/routes/main.cr index 3dc4b40..cbcab43 100644 --- a/src/routes/main.cr +++ b/src/routes/main.cr @@ -26,10 +26,7 @@ class MainRouter < Router password = env.params.body["password"] token = @context.storage.verify_user(username, password).not_nil! - cookie = HTTP::Cookie.new "token-#{Config.current.port}", token - cookie.path = Config.current.base_url - cookie.expires = Time.local.shift years: 1 - env.response.cookies << cookie + set_token_cookie env, token redirect env, "/" rescue redirect env, "/login" diff --git a/src/util.cr b/src/util.cr index ff22c09..6afef02 100644 --- a/src/util.cr +++ b/src/util.cr @@ -135,3 +135,10 @@ end macro render_xml(path) send_file env, ECR.render({{path}}).to_slice, "application/xml" end + +def set_token_cookie(env, token) + cookie = HTTP::Cookie.new "token-#{Config.current.port}", token + cookie.path = Config.current.base_url + cookie.expires = Time.local.shift years: 1 + env.response.cookies << cookie +end