>100 Views
November 14, 25
スライド概要
Professional Ruby and Rails programmer in Tokyo.
rails g authentication 入門 @Omotesando.rb
もとつねの日常 @daily_mototsune Ruby on Railsを中心に扱っているフリーランスエンジニア ドメインモデリングからAWSでの運用、スクラム運営やチームビルディ ングなどなど楽しくやらせてもらってます 普段はOmotesando.rbやGinza.rbによくいます
今日は認証の話です
Railsで認証といえば - Devise - sorcery - authlogic
Rails 8の新機能として認証機能の生 成が追加 https://www.youtube.com/watch?v=-cEn_83zRFw
rails g authenticationとは - Rails 8から追加された認証機能を持つファイルを生成する generator - Rails本体に機能を内包しているわけではなく、認証機能が実 装されたファイルが生成されるのが特徴 - 基本的なメールアドレス&パスワードによるユーザー認証機能 を持つ
but why? - なぜRails自身がgeneratorを持つのか - なぜRails本体(例えばActionControllerなど)に認証機能を持 たないのか - magic linksやpasskeys、2FAには対応しないのか
Rails now include all the key building blocks needed to do basic authentication, but many new developers are still uncertain of how to put them together, so they end up leaning on all-in-one gems that hide the mechanics. 超意訳 Railsは基本的な認証機能を作るための機能を持っているけど新しい開 発者たちはそれをどうあって組み合わせて使うのかわからない。だから 仕組みが隠されている機能もりもりのgemを使おうとしてしまう。 https://github.com/rails/rails/issues/50446
While these gems are great, and many people enjoy using them, they should not be seen as a necessity. We can teach Rails developers how to use the basic blocks by adding a basic authentication generator that essentially works as a scaffold, but for authentication. 超意訳 それらのgemは素晴らしく、みんな楽しんで使っているけど 必需品のように見えるのはよくない。 僕たちは基本的な認証機能を作るファイルをscaffoldのように生成する ことで開発者にどうやってRailsの機能を使うか教えることができる。 https://github.com/rails/rails/issues/50446
It's merely intended to illuminate the basic path, and reveal that rolling your own authentication system is not some exotic adventure. So do not expect magic links or passkeys or 2FA. That's not going to happen with this generator. 超意訳 これはただ基本的な機能を提示して、自分で認証機能を作ることが特 別な冒険でないことを明らかにしている。 だからmagic linksやpassskeys、2FAを期待しないでね。実装しない よ。 https://github.com/rails/rails/pull/52328
answers - なぜRails自身がgeneratorを持つのか - 新しい開発者が基本的な認証機能を作る方法を示したかっ た - なぜRails本体(例えばActionControllerなど)に認証機能を持たな いのか - 認証機能の仕組みをブラックボックス化したくなかった - magic linksやpasskeys、2FAには対応しないのか - しない。やりたければ自分で実装してみてね。
rails g authenticationでなにが起こるか - app/models/ - - session.rb user.rb current.rb db/migrate/ - XXXX_create_users.rb XXXX_create_sessions.rb
rails g authenticationでなにが起こるか - app/controllers/ - - sessions_controller.rb passwords_controller.rb concerns/authentication.rb app/views/ - - passwords/ - new.html.erb - edit.html.erb sessions/new.html.erb
rails g authenticationでなにが起こるか - app/mailers/passwords_mailer.rb app/views/passwords_mailer/ - - reset.html.erb reset.text.erb test/mailers/previews/passwords_mailer_preview.rb
rails g authenticationでなにが起こるか - ApplicationControllerにAuthenticaion Concern追加 - routesにsessionとpasswordの設定追加
rails g authenticationでなにが起こるか - Gemfileにbcrypt gemを追加 & bundle install
Authentication Concern - 認証機能の基礎となるconcern Application Controllerでincludeされる - includedでbefore_action :require_authenticationされるので基本的に全 てのアクションで認証されている必要がある状態になる authenticated?がhelper_methodとして定義される -
認証を必要としないアクションでは - allow_unauthenticated_accessが定義されるのでControllerの中で呼んで あげる
こんな感じ
ユーザを作成するには - サインアップ機能はないので自作する必要がある /passwords/newがあるがこちらはパスワードを忘れた人用
ユーザを作成するには - 手っ取り早く試したい場合はbin/rails cから作成
その他見どころ - 生成される実装がきれい。無駄がないというか。 - - Authentication Concernとか PasswordsControllerの👇とか 覗いてみると何か発見があるかも
1時間でRailsアプリを本番deploy - Rails 8 dev container rails g authentication ActionText(ActiveStorage) kamal 2 上記を使って1時間でブログアプリを本番deployしました ことの顛末はこちらに https://blog.saeki-mototsune.com/articles/1