Skip to content

Instantly share code, notes, and snippets.

@wookay
Last active January 17, 2020 12:00
Show Gist options
  • Save wookay/fed24f2334d3be3524661b83f98fde30 to your computer and use it in GitHub Desktop.
Save wookay/fed24f2334d3be3524661b83f98fde30 to your computer and use it in GitHub Desktop.
bukdu auth
# https://github.com/wookay/Bukdu.jl/tree/master
using Bukdu
struct Authorization <: Plug.AbstractPlug
end
function user_authorized(conn::Conn)
conn.params[:pass] == "1"
end
function plug(::Type{Authorization}, conn::Conn)
req = conn.request
conn.private[:auth] = user_authorized(conn)
end
pipeline(:auth) do conn::Conn
plug(Authorization, conn)
end
struct IndexController <: ApplicationController
conn::Conn
end
function authorized_index(::IndexController)
render(Text, "authorized_index")
end
function login(::IndexController)
render(Text, "login")
end
function authorized(conn::Conn)::Bool
get(conn.private, :auth, false)
end
function index(c::IndexController)
!authorized(c.conn) && return redirect_to(c.conn, "/login")
render(Text, "index")
end
routes(:auth) do
get("/authorized_zone", IndexController, authorized_index)
get("/", IndexController, index)
end
routes() do
get("/login", IndexController, login)
end
Bukdu.start(8080)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment