2023-05-29 - Authentication and Devise
main topics
- devise for authentication
- https://github.com/heartcombo/devise
Starting with devise
bundle add devise
rails generate devise:install
rails generate devise User
rails db:migrate
# optional
rails generate devise:views
Helpers:
# true / false
user_signed_in?
# user instance / nil
current_user
# redirect to sign in if needed
authenticate_user!
Logout link:
link_to(
"Logout",
destroy_user_session_path,
data: { turbo_method: :delete }
)
Usually a controller has:
before_action :authenticate_user!, except: %i[index show]