Unlocking the ‘Devise’ Gem in Rails 8

If we have to don our wizarding hat and take out our crystal balls, we would still be harping that Ruby on Rails is going to be hot in 2026 too! 2026 is also going to bring in only 24 hours a day (how we wish, we could stretch the time) and time would still spell money for businesspeople. When time is money, anything which can save time is gold! And that’s where Ruby on Rails come in picture. What could take other programming languages months to complete, takes only weeks, sometimes just days for Ruby on Rails. Shaping ideas into applications was never so easy. With Ruby on Rails, agility is the norm and convention over configuration the benchmark. And what make Ruby on Rails more powerful are the bunch of pre-packaged, categorized, reusable codes, named as gems. With gems, Rails is a super power to behold. One of the most popular gems is called as Devise Gems, which is a complete authentication package. Read on to know more about Devise gem.

Introducing Devise for Ruby on Rails 8

Devise is a widely used authentication library for Ruby on Rails 8 applications, built on top of Warden, a Rack-based authentication framework.

Warden manages user sessions by securely validating the identity of authenticated users. It also ensures that unauthenticated users are restricted from accessing protected routes and resources within a Rails application.

Since Warden operates strictly at the Rack level, it does not provide Rails-specific features such as controllers, views, helpers, or configuration generators required for a complete authentication system. Devise bridges this gap by adding these Rails integrations, making it easier to implement a robust and production-ready authentication solution in Rails 8.

A major advantage of Devise is its modular architecture. It includes around ten authentication modules that allow you to customize how authentication behaves in your application. You can enable only the modules you need—such as Registerable, Omniauthable, Trackable, and others—keeping your authentication setup clean and flexible.

 

With this understanding, we can now begin building our Tasks application and set up Devise in a Rails 8 environment.

「デバイス」ジェムとは何ですか?

  1. Devise は、Rails 用の強力かつ柔軟な認証ソリューションです。
  2. これは完全に MVC 構造に基づいています。
  3. 複数のモデルを同時にサインインさせることができます。

Deviceのさまざまなモジュール

Devise は 10 個の重要なモジュールで構成されています。

  1. データベース認証可能: サインイン時にユーザーの信頼性を検証できるように、パスワードを暗号化してデータベースに保存します。認証は、POST リクエストまたは HTTP 基本認証の両方を通じて実行できます。
  2. オムニア認証可能: 追加 オムニ認証 サポート、つまり OmniAuth によるログインを設定することが可能です。
  3. 確認可能: 確認手順を記載した電子メールを送信し、サインイン時にアカウントがすでに確認されているかどうかを確認します。
  4. 回復可能: ユーザーのパスワードをリセットし、リセット手順を送信します。
  5. 登録可能: ユーザーがサインアップすることで登録プロセスを処理し、ユーザーが自分のアカウントを編集および破棄できるようにします。
  6. 記憶可能: 名前が示すように、保存された Cookie を通じてユーザーを記憶するためのトークンの管理、生成、クリアに役立ちます。
  7. 追跡可能: サインイン数、タイムスタンプ、IP アドレスを追跡します。
  8. タイムアウト可能: 指定された期間アクティブでなかったセッションを期限切れにします。
  9. 検証可能: 電子メールとパスワードの検証を提供します。これはオプションでカスタマイズできるため、独自の検証を定義できます。
  10. ロック可能: サインイン試行が特定の回数失敗した後にアカウントをロックするのに役立ちます。アカウントは電子メール経由で、または特定の期間後にロックを解除できます。

デバイスのインストール、モデルの作成、表示と構成

  1. Rails アプリを作成する
  2. gem 'devise' を Gemfile に追加します。
  3. コマンドラインでバンドルインストールを実行します。
  4. コマンドラインで「rails generated Device:install」を実行します。
  5. Execute rails generate devise <Model Name> in your command line to create model
Run the following command:
 
rails generate devise User
After running this command, a user.rb model will be generated in the app/models directory.
 
class User < ApplicationRecord
  # Include default Devise modules.
  # Other available modules are:
  # :confirmable, :lockable, :timeoutable, and :omniauthable
  devise :database_authenticatable,
         :registerable,
         :recoverable,
         :rememberable,
         :trackable,
         :validatable
終わり
  1. 必要に応じて、ユーザーの移行ファイルを変更できます。次に、 rake db:merge を実行します。
  2. コマンドラインでrailsgenerate Device:viewsを実行してビューを作成します
  3. 各環境で Devise メーラーのデフォルトの URL オプションを設定する必要があります。 config/environments/development.rb の例: config.action_mailer.default_url_options = { host: 'localhost'、port: 3000 }

要件に応じてモデル、コントローラー、ビューをカスタマイズできます。詳細については、github ドキュメントに従ってください。

一部のコントローラー フィルターとヘルパー

  • ユーザー認証を使用してコントローラーをセットアップするには before_action :authenticate_user!
  • ユーザーがサインインしているかどうかを確認するには => user_signed_in?
  • 現在サインインしているユーザーの場合 => current_user
  • このスコープのセッションにアクセスします: => user_session
  • after_sign_in_path_for と after_sign_out_path_for をオーバーライドして、リダイレクト パスをカスタマイズすることもできます。

「activeadmin」gem、「devise_invitable」gemをdevicesと統合できます。

時間の節約という点では、Ruby on Rails に勝るものはありません。アイデアがあり、それを実装したい場合は、Ruby on Rails を選択するのが最善の策です。 RailsCarma は、開発、トレーニング、デプロイ、Rails コミュニティへの貢献のために初期段階から Ruby on Rails を実装し、最高の Ruby on Rails 開発サービスを提供しています。 RailsCarma は、コンサルティング、アーキテクチャ、構築、管理、拡張などのエンドツーエンドの Ruby on Rails サービスを世界中の企業に提供します。あなたもすることができます Ruby on Rails 開発者を雇う 簡単な雇用プロセスで。 お問い合わせ もっと知るために。

他の宝石について読む:

最新のアップデートを購読する

関連記事

投稿者について

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です


jaJapanese