Skip to content

Instantly share code, notes, and snippets.

@robacarp
Last active December 21, 2018 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robacarp/ef45825ba665f4f8b46cd78a82dce670 to your computer and use it in GitHub Desktop.
Save robacarp/ef45825ba665f4f8b46cd78a82dce670 to your computer and use it in GitHub Desktop.
module Auth::RedirectIfSignedIn
macro included
include Auth::SkipRequireSignIn
before redirect_if_signed_in
# Most notably, this is removed everywhere. All actions now depend on a current_user.
# unexpose current_user
end
# ...
def current_user
end
end
module SessionManagement
@user : User?
# ...
def current_user?
@user ||= begin
# #get? instead of #get
if id = session.get? SESSION_KEY
UserQuery.find id
end
end
end
end
abstract class GuestLayout < Layout
# nothing in this file except the 'needs'
needs current_user : User?
end
abstract class Layout
# ...
def login_button
li class: "nav-item" do
# @current_user will be a User? when using GuestLayout, and a User when using MainLayout
if @current_user
link "Log Out", to: Session::Delete, class: "nav-link"
else
link "Log In", to: Session::New, class: "nav-link"
end
end
end
end
abstract class MainLayout < Layout
# nothing in this file except the 'needs'
needs current_user : User
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment