Clearance, Thoughtbot की टीम द्वारा बनाया गया ईमेल और पासवर्ड के साथ एक सरल प्रमाणीकरण प्रणाली है। इसके राय वाले डिफ़ॉल्ट हैं लेकिन इसे ओवरराइड करना आसान बनाने का इरादा है। सिस्टम सक्रिय रूप से बनाए रखा जाता है, और आप GitHub पर इसका अनुसरण कर सकते हैं।
इस ट्यूटोरियल में, आप देखेंगे कि Clearance को Rails एप्लिकेशन में कैसे एकीकृत किया जाए। हम एक लघु एप्लिकेशन का उपयोग करेंगे। चलिए शुरू करते हैं!
शुरुआत करना
आप अपना Rails एप्लिकेशन जनरेट करके शुरू करेंगे। इस ट्यूटोरियल के उद्देश्य के लिए, मैं इसका नाम tutsplus-clearance रखूंगा।
rails new tutsplus-clearance -T
यह जादू कर देगा।
आपको अपने एप्लिकेशन को अच्छा दिखाने के लिए बूटस्ट्रैप की आवश्यकता होगी। अपने Gemfile में बूटस्ट्रैप जेम जोड़ें।
#Gemfile ... gem 'bootstrap-sass'
bundle install चलाकर जेम इंस्टॉल करें।
अब application.scss को इस तरह दिखने के लिए संशोधित करें:
#app/assets/stylesheets/application.scss @import 'bootstrap-sprockets'; @import 'bootstrap';
Clearance सेटअप
Clearance जेम जोड़ने के लिए अपना Gemfile खोलें।
#Gemfile gem 'clearance'
अब जेम इंस्टॉल करें।
bundle install
इस बिंदु पर, clearance इंस्टॉल करने के लिए जनरेटर कमांड चलाएं।
rails generate clearance:install
यह आपके टर्मिनल पर कुछ आउटपुट जनरेट करेगा, जो नीचे दिए गए जैसा दिखता है:
create config/initializers/clearance.rb
insert app/controllers/application_controller.rb
create app/models/user.rb
create db/migrate/20161115101323_create_users.rb
*******************************************************************************
Next steps:
1. Configure the mailer to create full URLs in emails:
# config/environments/{development,test}.rb
config.action_mailer.default_url_options = { host: 'localhost:3000' }
In production it should be your app's domain name.
2. Display user session and flashes. For example, in your application layout:
<% if signed_in? %>
Signed in as: <%= current_user.email %>
<%= button_to 'Sign out', sign_out_path, method: :delete %>
<% else %>
<%= link_to 'Sign in', sign_in_path %>
<% end %>
<div id="flash">
<% flash.each do |key, value| %>
<div class="flash <%= key %>"><%= value %></div>
<% end %>
</div>
3. Migrate: rake db:migrate
*******************************************************************************
जब आपने कमांड चलाया, तो आपके एप्लिकेशन में कुछ फ़ाइलें जनरेट हुईं। ऐसी एक फ़ाइल clearance.rb है, जिसे आप config/initializers निर्देशिका में पा सकते हैं। एक User मॉडल भी जनरेट किया गया था, और इसके साथ आपको एक माइग्रेशन फ़ाइल भी मिलती है जो इस तरह दिखती है:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.timestamps null: false
t.string :email, null: false
t.string :encrypted_password, limit: 128, null: false
t.string :confirmation_token, limit: 128
t.string :remember_token, limit: 128, null: false
end
add_index :users, :email
add_index :users, :remember_token
end
end
आउटपुट के अनुसार, पहली चीज जो आप करना चाहते हैं वह है अपने कॉन्फ़िगरेशन एनवायरनमेंट को संपादित करना। ऐसा करने के लिए, config/environments/development.rb पर जाएं और नीचे दी गई लाइन को end डिलीमीटर के ठीक ऊपर जोड़ें।
...
config.action_mailer.default_url_options = { host: 'localhost:3000' }
end
अगला, config/initializers/clearance.rb पर जाएं इसे संपादित करने के लिए, और जब आप वहां हों, तो प्रेषक ईमेल पते को डिफ़ॉल्ट से अपनी पसंद के किसी भी पते पर बदलें। जब आप फ़ाइल खोलेंगे तो आपको यह दिखाई देगा।
#config/initializers/clearance.rb Clearance.configure do |config| config.mailer_sender = "reply@example.com" end
आप निम्न कोड स्निपेट पेस्ट करके डिफ़ॉल्ट कॉन्फ़िगरेशन को ओवरराइड कर सकते हैं और इसे अपनी आवश्यकताओं के अनुसार कॉन्फ़िगर कर सकते हैं।
#config/initializers/clearance.rb
Clearance.configure do |config|
config.allow_sign_up = true
config.cookie_domain = ".example.com"
config.cookie_expiration = lambda { |cookies| 1.year.from_now.utc }
config.cookie_name = "remember_token"
config.cookie_path = "/"
config.routes = true
config.httponly = false
config.mailer_sender = "reply@example.com"
config.password_strategy = Clearance::PasswordStrategies::BCrypt
config.redirect_url = "/"
config.secure_cookie = false
config.sign_in_guards = []
config.user_model = User
end
अपना डेटाबेस माइग्रेट करने के लिए कमांड चलाएं।
rake db:migrate
अपना PagesController खोलें और एक index एक्शन जोड़ें।
#app/controllers/pages_controller.rb class PagesController < ApplicationController def index end end
अगला, आपके द्वारा अभी बनाए गए index एक्शन के लिए एक व्यू बनाएं।
नीचे दिया गया कोड स्निपेट जोड़ें:
#app/views/pages/index.html.erb <h1>Tutsplus Clearance</h1> <p>Welcome to our Clearance Page.</p>
अपने रूट्स को संपादित करें:
#config/routes.rb Rails.application.routes.draw do root to: "pages#index" end
layouts निर्देशिका के अंदर _navigation.html.erb नामक एक पार्शियल बनाएं। इसका उपयोग आपके एप्लिकेशन पर नेविगेशन से संबंधित हर चीज को संभालने के लिए किया जाएगा।
निम्न कोड पेस्ट करें और सेव करें।
#app/views/layouts/_navigation.html.erb
<nav class="navbar navbar-inverse">
<div class="container">
<div class="navbar-header">
<%= link_to 'Tutsplus-Clearance', root_path, class: 'navbar-brand' %>
</div>
<div id="navbar">
<% if signed_in? %>
<ul class="nav navbar-nav">
<li><%= link_to 'Add Page', new_page_path %></li>
</ul>
<% end %>
<ul class="nav navbar-nav pull-right">
<% if signed_in? %>
<li><span><%= current_user.email %></span></li>
<li><%= link_to 'Sign out', sign_out_path, method: :delete %></li>
<% else %>
<li><%= link_to 'Sign in', sign_in_path %></li>
<% end %>
</ul>
</div>
</div>
</nav>
<div class="container">
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>">
<%= value %>
</div>
<% end %>
</div>
प्रतिबंधित पहुंच
Clearance के साथ, आप अपनी पसंद के अपने एप्लिकेशन के विशिष्ट पृष्ठों पर प्रतिबंधित पहुंच बना सकते हैं। आइए देखें कि यह कैसे किया जाता है।
app/views/pages में new एक्शन के लिए एक व्यू बनाएं, फ़ाइल का नाम new.html.erb होना चाहिए। नीचे दिया गया कोड पेस्ट करें।
#app/views/pages/new.html.erb <h1>Restricted Page</h1> <p>This page is restricted to authenticated users, if you can see this it means you are a superstar!</p>
अब आपको config/routes.rb में नीचे दी गई लाइन जोड़ने की आवश्यकता है।
#config/routes.rb ... resources :pages, only: :new ...
अंत में, अपने PagesController पर जाएं इसे नीचे दिए गए जैसा बनाएं।
#apps/controllers/pages_controller.rb class PagesController < ApplicationController before_action :require_login, only: [:new] def index end def new end end
उपरोक्त कोड में, हम Clearance हेल्पर, require_login, का उपयोग new एक्शन तक पहुंच को प्रतिबंधित करने के लिए कर रहे हैं। यह कैसे काम करता है यह देखने के लिए, rails server चलाकर अपना रेल्स सर्वर शुरू करें। अपने ब्राउज़र को https://localhost:3000/pages/new पर इंगित करें और यह आपको साइन इन पेज पर रीडायरेक्ट करना चाहिए।
Clearance रूटिंग बाधाएं भी प्रदान करता है जिनका उपयोग पहुंच को नियंत्रित करने के लिए किया जा सकता है।
#config/routes.rb
Rails.application.routes.draw do
constraints Clearance::Constraints::SignedOut.new do
root to: 'pages#index'
end
constraints Clearance::Constraints::SignedIn.new do
root to: "pages#new', as: :signed_in_root
end
end
उपरोक्त कोड में, प्रमाणित उपयोगकर्ताओं के लिए एक अलग रूट बनाया गया है।
Clearance डिफ़ॉल्ट को ओवरराइड करना
जब आप Clearance का उपयोग करना शुरू करते हैं तो पर्दे के पीछे बहुत कुछ होता है, चीजें जो आप नहीं देख सकते। एक समय आ सकता है जब आप अपनी एप्लिकेशन की विशिष्टता के आधार पर चीजों को अलग तरीके से अनुकूलित करना चाहें। Clearance आपको इसके साथ आने वाली डिफ़ॉल्ट कॉन्फ़िगरेशन को ओवरराइड करने की अनुमति देता है।
Clearance रूट्स को ओवरराइड (या जनरेट) करने के लिए, अपने टर्मिनल से यह कमांड चलाएं।
rails generate clearance:routes
आपकी रूट्स फ़ाइल अब इस तरह दिखनी चाहिए:
#config/routes.rb
Rails.application.routes.draw do
resources :passwords, controller: "clearance/passwords", only: [:create, :new]
resource :session, controller: "clearance/sessions", only: [:create]
resources :users, controller: "clearance/users", only: [:create] do
resource :password, controller: "clearance/passwords", only: [:create, :edit, :update]
end
get "/sign_in" => "clearance/sessions#new", as: "sign_in"
delete "/sign_out" => "clearance/sessions#destroy", as: "sign_out"
get "/sign_up" => "clearance/users#new", as: "sign_up"
root to: "pages#index"
resources :pages, only: :new
end
कमांड आपके config/initializers/clearance.rb फ़ाइल में config.routes सेटिंग को false पर भी सेट कर देगा। इसका मतलब है कि अभी जनरेट की गई कस्टम फ़ाइल का उपयोग किया जाएगा।
संशोधन के लिए व्यू जनरेट करने के लिए, चलाएं:
rails generate clearance:views
जनरेट की जाने वाली कुछ फ़ाइलों में शामिल हैं:
app/views/passwords/create.html.erb app/views/passwords/edit.html.erb app/views/passwords/new.html.erb app/views/sessions/_form.html.erb app/views/sessions/new.html.erb app/views/users/_form.html.erb app/views/users/new.html.erb config/locales/clearance.en.yml
आपको अपने टर्मिनल में एक प्रॉम्प्ट दिखाई देगा जो आपके app/views/layouts/application.html.erb फ़ाइल को ओवरराइट करने के लिए कहेगा। वह विकल्प चुनें जो आप चाहते हैं।
लेआउट
डिफ़ॉल्ट रूप से, Clearance आपके एप्लिकेशन के डिफ़ॉल्ट लेआउट का उपयोग करता है। यदि आप वह लेआउट बदलना चाहते हैं जिसका उपयोग Clearance अपने व्यू रेंडर करते समय करता है, तो बस इनिशियलाइज़र में लेआउट निर्दिष्ट करें।
Clearance::PasswordsController.layout "my_passwords_layout" Clearance::SessionsController.layout "my_sessions_layout" Clearance::UsersController.layout "my_admin_layout"
हेल्पर विधियां
Clearance आपको हेल्पर विधियां प्रदान करता है जिनका उपयोग आपके controllers, views, और helpers में किया जा सकता है। इन विधियों में signed_in?, signed_out?, और current_user शामिल हैं। उदाहरण के लिए:
<% if signed_in? %> <%= current_user.email %> <%= button_to "Sign out", sign_out_path, method: :delete %> <% else %> <%= link_to "Sign in", sign_in_path %> <% end %>
निष्कर्ष
जब प्रमाणीकरण की बात आती है तो Clearance आपको बहुत कुछ प्रदान करता है, इसलिए इसे अपनी अगली परियोजना में आज़माना सुनिश्चित करें। आप GitHub पेज देख कर अधिक जान सकते हैं।