Deviseã¯ãrailsã¢ããªã±ãŒã·ã§ã³ã§èªèšŒæ©èœãæäŸããruby gemã§ãã Deviseã¯ãWarden gemãšé£æºããŠåäœããŸããWardengemã¯ãã©ãã¯ããŒã¹ã®ã«ããŒã¢ããªã±ãŒã·ã§ã³ã§ã®èªèšŒã¡ã«ããºã èªäœãæäŸããŸãã Deviseã®äž»ãªæ©èœã¯æ¬¡ã®ãšããã§ãã
- ã©ãã¯ããŒã¹
- RailsããŒã¹ã®å®å šãªMVCãœãªã¥ãŒã·ã§ã³ã§ãã
- è€æ°ã®ã¢ãã«ã®ã·ã¹ãã ã«åæã«ãã°ã€ã³ã§ããŸãã
- ã¢ãžã¥ãŒã«æ§ã«åºã¥ãïŒæ¬åœã«å¿ èŠãªãã®ã ãã䜿çšããŸãã
ããã§ã¯ãRailsã¢ããªã±ãŒã·ã§ã³çšã«Deviseã®ã€ã³ã¹ããŒã«ãšæ§æãå§ããŸãããã èªã¿ãããã®ããã®ãããªãããã»ã¹ã¯ãå¥ã ã®ã¹ãããã«åããããŸãã
泚 ïŒç§ã«ãããã¹ãŠã®ã€ã³ã¹ããŒã«ãšæ§æã¯ãRailsããŒãžã§ã³4.0.1çšã«äœæãããŸããã
ã¹ããã1. Gemfileã«Gemãè¿œå ãã
gem 'devise'
ãŸãã¯æ£ç¢ºãªããŒãžã§ã³ã瀺ããŸãïŒæ¬¡ã®ããŒãžã§ã³ã¯ç§ã®ããã«rails 4.0.1ã§å®å®ããŠåäœããŸãïŒ
gem 'devise', '3.2.2'
ãã³ãã«ãå®è¡ããŠæ°ããgemãã€ã³ã¹ããŒã«ããŸã
bundle install
次ã®gemãäŸåé¢ä¿ãšããŠã€ã³ã¹ããŒã«ãããŸãã
ç£èŠå¡-Rackã¢ããªã±ãŒã·ã§ã³ã«èªèšŒæ©èœãæäŸããããã«ãŠã§ã¢ã
orm_adapter - Ruby ORMã®åºæ¬æ©èœã䜿çšããããã®åäžã®ãšã³ããªãã€ã³ããæäŸããŸãã
bcrypt-ruby-ãã¹ã¯ãŒããæäœããããã®ã·ã³ãã«ãªã©ãããŒãæäŸããŸãã æå·åããã·ã¥é¢æ°bcryptïŒïŒã«åºã¥ããŠããŸãã
thread_safe -Rubyçšã®ã¹ã¬ããã»ãŒããªã³ã¬ã¯ã·ã§ã³ãšãŠãŒãã£ãªãã£ãæäŸããŸãã
railtiesã¯ãã¢ããªã±ãŒã·ã§ã³ããŒããŒããã©ã°ã€ã³ããžã§ãã¬ãŒã¿ãŒãrakeã¿ã¹ã¯ãªã©ã®å éšRailsã³ã³ããŒãã³ãã§ãã
ã¹ããã2.ããã§ãgemã¯ããŠã³ããŒããããŸããããã¢ããªã±ãŒã·ã§ã³ãšã¯å¯Ÿè©±ããŸããã Deviseã«ã¯äŸ¿å©ãªãžã§ãã¬ãŒã¿ãŒãããããã®ãã¡ã®1ã€ã䜿çšããŸãã 次ã®ãžã§ãã¬ãŒã¿ãŒãå®è¡ããŠDeviseãã€ã³ã¹ããŒã«ããŸãã
rails generate devise:install
ãã®ãžã§ãã¬ãŒã¿ãŒã¯ãæäœã«å¿ èŠãªãã¹ãŠã®Deviseæ§æèšå®ãšãåºæ¬ãã±ãŒã«ïŒè±èªïŒã®ãã¡ã€ã«ãèšè¿°ããåæååãã€ã³ã¹ããŒã«ããŸãã ãŸããã€ã³ã¹ããŒã©ãŒã¯åºæ¬çãªæ§æãå®è¡ããããã«æäŸããŸãã
ã¹ããã3.ã€ã³ã¹ããŒã«åŸã«gemã®åºæ¬èšå®ãè¡ããŸããã
3.1ã 次ã«ãåã©ã³ã¿ã€ã ã®ã¡ãŒã©ãŒïŒéä¿¡è ïŒèšå®ãèšå®ããå¿ èŠããããŸãã éçºç°å¢ã®å Žåã次ã®è¡ãconfig / environment / development.rbãã¡ã€ã«ã«è¿œå ããŸãã
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
å®çšŒåç°å¢ã§ã¯ãããŒå€ïŒhostãçŸåšã®å€ã«çœ®ãæããå¿ èŠããããŸãã
3.2ã ãŠãŒã¶ãŒããã°ã€ã³ããã¢ã«ãŠã³ãã確èªãããããã¹ã¯ãŒããæŽæ°ãããšãDeviseã¯ããã«ãªãã€ã¬ã¯ãããããã®ãã¹ãæ¢ããŸãã ããã©ã«ãã§ã¯ã user_root_pathãååšããå Žåã¯ãªãã€ã¬ã¯ããããŸãã ãã以å€ã®å ŽåãDeviseã¯root_pathã«ãªãã€ã¬ã¯ãããŸãã ãããã£ãŠããã®ãã¹ã¯ã¢ããªã±ãŒã·ã§ã³ã§å®çŸ©ããå¿ èŠããããŸãã config / routes.rb routes ãã¡ã€ã«ã§æ¬¡ã®ãããªè¡ã確èªããŸãã
root 'home#index'
ãã°ã€ã³åŸããŠãŒã¶ãŒãã¢ããªã±ãŒã·ã§ã³ã®ã¡ã€ã³ããŒãžã«ãªãã€ã¬ã¯ãããããšã¯ããŸãè«ççã§ã¯ãªãããšã«åæããŠããã ãããšæããŸãã ãã®ç¹ã§ããŠãŒã¶ãŒãããã¡ã€ã«ããŒãžã«æ£åžžã«ãã°ã€ã³ããåŸããªãã€ã¬ã¯ããæ§æããŸãããã ãããè¡ãã«ã¯ã次ã®ã«ãŒããconfig / routes.rbã«è¿œå ããŸãã
get 'persons/profile', as: 'user_root'
泚 ïŒ
1ïŒãŸããã³ã³ãããŒã©ãŒãšã¢ã¯ã·ã§ã³ãäœæããå¿ èŠããããŸãã ããã¯ã䟿å©ãªãžã§ãã¬ãŒã¿ãŒã䜿çšããŠã³ã³ãœãŒã«ããå®è¡ã§ããŸãã
rails generate controller persons profile
2ïŒãã®çµæããžã§ãã¬ãŒã¿ãŒã¯ã1ã€ã®ãããã¡ã€ã«ã¡ãœããïŒã¢ã¯ã·ã§ã³ïŒãšãã®ã¢ã¯ã·ã§ã³ã®ãã¥ãŒãåããpersons_controllerã³ã³ãããŒã©ãŒãäœæããŸãã
ã«ãŒãã®ãªã¹ãã¯ãã³ã³ãœãŒã«ããååŸã§ããŸãã 次ã®ã³ãã³ããå ¥åããã ãã§ãïŒ
bundle exec rake routes
ç§ãã¡ã®å Žåã次ã®ã«ãŒã¿ãŒãå©çšå¯èœã§ãªããã°ãªããŸãã
user_root GET /persons/profile(.:format) persons#profile
user_root_path ãã«ããŒã䜿çšå¯èœã«ãªããŸã
ãŸããå ¥ååŸãšçµäºåŸã®äž¡æ¹ã§ç¬èªã®ãªãã€ã¬ã¯ããèšå®ã§ããå¥ã®æ¹æ³ããããŸãã ãããè¡ãã«ã¯ã ApplicationController ïŒ app / controllers / application_controller.rb ïŒã®æ¢åã®deviseã¡ãœããafter_sign_in_path_forããã³after_sign_out_path_forããªãŒããŒã©ã€ãããå¿ èŠããããŸãã å人çã«ã¯ããªãã€ã¬ã¯ããæ§æãããã®ãªãã·ã§ã³ã奜ãã§ããïŒ
def after_sign_in_path_for(resource) current_user_path end
ãã°ã€ã³åŸããŠãŒã¶ãŒã¯current_user_pathãã«ããŒã«ãã£ãŠèšè¿°ãããããŒãžã«ãªãã€ã¬ã¯ããããŸã
def after_sign_out_path_for(resource_or_scope) request.referrer end
ãã°ã¢ãŠãåŸããŠãŒã¶ãŒã¯åãããŒãžã«æ®ããŸãã
3.3ã éç¥ãšèŠåã®åºåããã³ãã¬ãŒãã«è¿œå ããå¿ èŠããããŸã
<p class="notice"><%= notice %></p> <p class="alert"><%= alert %></p>
3.4ã RailsããŒãžã§ã³3.2ã䜿çšããŠHerokuã«ã¢ããªã±ãŒã·ã§ã³ããããã€ããå Žåã次ã®è¡ãconfig / application.rbã«è¿œå ããŸã
config.assets.initialize_on_precompile = false
3.5ã ãã¥ãŒãã¡ã€ã«ãããŒãºã«åãããŠã«ã¹ã¿ãã€ãºã§ããŸãã ãããè¡ãã«ã¯ã次ã®ã³ãã³ããå®è¡ããŠãgemããã¢ããªã±ãŒã·ã§ã³ã«ããããã³ããŒããå¿ èŠããããŸãã
rails generate devise:views
app / views / deviseãã£ã¬ã¯ããªã«ã¯ã gemã䜿çšãããã¹ãŠã®ãã¥ãŒãã¡ã€ã«ããããŸãã å¿ èŠã«å¿ããŠãã¢ããªã±ãŒã·ã§ã³ã®å šäœçãªã¹ã¿ã€ã«ã«åãããŠã«ã¹ã¿ãã€ãºã§ããŸãã
ã¹ããã4.次ã«ãèªèšŒãè¡ããŠãŒã¶ãŒã¢ãã«ãäœæããŸãã ãŠãŒã¶ãŒã¢ãã«ãçæããã®ã§ããããUserãšåŒã³ãŸã ã ã¢ãã«ã¯adminãšåŒã°ããããšããããŸãïŒ
rails generate devise User
ãã®ãžã§ãã¬ãŒã¿ãŒã¯ã以åã«ååšããªãã£ãå Žåã«æ°ããã¢ãã«ãäœæããããã©ã«ãã§äœ¿çšãããã¢ãžã¥ãŒã«ãèæ ®ããŠæ§æããŸãã ã»ãšãã©ã®å Žåã app / models / user.rbã§æ¬¡ã®ã³ãŒããååŸããŸãïŒã³ãŒãã¯å€å°ãã©ãŒããããããŠããŸãïŒïŒ
class User < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable end
Deviseã«ã¯10åã®ã¢ãžã¥ãŒã«ããããŸãã ããã©ã«ãã§ã¯ã6ã€ã®ã¢ãžã¥ãŒã«ãæ¥ç¶ãããŠããŸãã ãã®ãªã¹ããç·šéã§ããŸãã å©çšå¯èœãªãã¹ãŠã®ã¢ãžã¥ãŒã«ã®èª¬æã以äžã«ç€ºããŸãã
1. ããŒã¿ããŒã¹èªèšŒå¯èœ ïŒæå·åãããããŒã¿ããŒã¹ã«ä¿åããããã¹ã¯ãŒãã«åºã¥ããŠã·ã¹ãã ã«å ¥ãæ©èœãæäŸããŸãã ãã°ã€ã³ããã«ã¯ãPOSTãªã¯ãšã¹ããéä¿¡ããããHTTPåºæ¬èªèšŒã䜿çšããŸãã
2. Omniauthable ïŒOmniauthãµããŒããè¿œå ããŸã ïŒ https://github.com/intridea/omniauth ïŒã
3. 確èªå¯èœïŒç»é²æã«äœæãããã¢ã«ãŠã³ãã確èªããæé ãèšèŒããã¡ãŒã«ãéä¿¡ã§ããŸãã
4. å埩å¯èœ ïŒå¿ãããã¹ã¯ãŒããå埩ã§ããŸãã å埩æ瀺ãã¡ãŒã«ã§éä¿¡ããŸãã
5. ç»é²å¯èœ ïŒãŠãŒã¶ãŒç»é²ã管çããã¢ã«ãŠã³ããç·šéããã³åé€ã§ããŸãã
6. Rememberable ïŒãŠãŒã¶ãŒã¯Cookieã«åºã¥ããŠCookieãèšæ¶ã§ããŸãã ããŒã¯ã³ã®äœæãšåé€ãå¶åŸ¡ããŸãã
7. 远跡å¯èœ ïŒå ¥åæ°ã®çµ±èšãä¿æããæéãšITã¢ãã¬ã¹ãèæ ®ããŸãã
8. ã¿ã€ã ã¢ãŠãå¯èœ ïŒã·ã¹ãã ã§ã®ãŠãŒã¶ãŒã¢ã¯ãã£ããã£ã»ãã·ã§ã³ã®æéãæ åœããŸãã
9.æ€èšŒå¯èœïŒé»åã¡ãŒã«ãšãã¹ã¯ãŒãã®æ€èšŒããŒã«ãæäŸããŸãã ã¢ãžã¥ãŒã«ã¯ç°¡åã«æ§æã§ããç¬èªã®ããªããŒã¿ãŒãå®çŸ©ã§ããŸãã
10. ããã¯å¯èœ ïŒèšå®ã§æå®ããã倱æããèªèšŒè©Šè¡åæ°ã®åŸã«ã¢ã«ãŠã³ããããã¯ããŸãã ã¢ã«ãŠã³ãã¯ãé»åã¡ãŒã«ãŸãã¯äžå®æéåŸã«ããã¯è§£é€ã§ããŸãã
ãŸããäžèšã§å®è¡ããã³ãã³ãã¯ãããŒã¿ããŒã¹ç§»è¡ãã¡ã€ã«ãšã«ãŒããäœæããŸãã 移è¡ãã¡ã€ã«ã¯æ¬¡ã®ãšããã§ãã
class DeviseCreateUsers < ActiveRecord::Migration def change create_table(:users) do |t| ## Database authenticatable t.string :email, :null => false, :default => '' t.string :encrypted_password, :null => false, :default => '' ## Recoverable t.string :reset_password_token t.datetime :reset_password_sent_at ## Rememberable t.datetime :remember_created_at ## Trackable t.integer :sign_in_count, :default => 0, :null => false t.datetime :current_sign_in_at t.datetime :last_sign_in_at t.string :current_sign_in_ip t.string :last_sign_in_ip ## Confirmable # t.string :confirmation_token # t.datetime :confirmed_at # t.datetime :confirmation_sent_at # t.string :unconfirmed_email # Only if using reconfirmable ## Lockable # t.integer :failed_attempts, :default => 0, :null => false # Only if lock strategy is :failed_attempts # t.string :unlock_token # Only if unlock strategy is :email or :both # t.datetime :locked_at t.timestamps end add_index :users, :email, :unique => true add_index :users, :reset_password_token, :unique => true # add_index :users, :confirmation_token, :unique => true # add_index :users, :unlock_token, :unique => true end end
ããã§ããŠãŒã¶ãŒããŒãã«ãäœæãããŠããããšãããããŸãïŒã³ãŒãã®3è¡ç®ïŒã Userã¢ãã«ã«ååãä»ãããããããŒãã«ã®ååã¯åãã§ãããè€æ°ãããŸãïŒ ActiveRecordã®åœåèŠåã«åŸã£ãŠïŒã 次ã«ãäœææã«ããŒãã«ã«è¿œå ããããã£ãŒã«ãã«ã€ããŠèª¬æããŸãã ã³ãŒããããããããã«ã6ã€ã®Deviseã¢ãžã¥ãŒã«ã®ã¿ãæ©èœããããã«ããŒãã«å ã®ãã£ãŒã«ããå¿ èŠãšããå ŽåããããŸãã
ã¢ãžã¥ãŒã«ã䜿çšããŠããªãå Žåã¯ã移è¡ã®å¯Ÿå¿ãããã£ãŒã«ããã³ã¡ã³ãåããããšãå¿ããªãã§ãã ããã ãŸããéã«ãã¢ãžã¥ãŒã«ã®1ã€ãã¢ã¯ãã£ãã«ãããšãã¯ã察å¿ããè¡ã®ã³ã¡ã³ããå€ãããšãå¿ããªãã§ãã ããã
次ã®ã«ãŒããconfig / routes.rbãã¡ã€ã«ã«äœæãããŸã ïŒ
devise_for :users
ã³ãã³ãã§æ¢åã®ã«ãŒãã®ãªã¹ãã衚瀺ã§ããããšãæãåºãããŠãã ããïŒ
bundle exec rake routes
ã¹ããã5.ããã§ãå¿ èŠãªã¢ãžã¥ãŒã«ãæ¥ç¶ããã¢ãžã¥ãŒã«ãèŠã€ããŠã移è¡ãã¡ã€ã«ã調ã¹ãŸããã 次ã«ã移è¡ãå®è¡ããå¿ èŠããããŸããããã«ãããå¿ èŠãªãã£ãŒã«ãã®ã»ãããæã€ãŠãŒã¶ãŒããŒãã«ãäœæãããŸãã 次ã®ã³ãã³ããå®è¡ããŸãã
bundle exec rake db:migrate
æé 6.次ã«ããã¹ãŠã®å€æŽãæå¹ã«ããã«ã¯ãã¢ããªã±ãŒã·ã§ã³ãåèµ·åããå¿ èŠããããŸãã ãããè¡ããªããšãããŸããŸãªçš®é¡ã®ãšã©ãŒãçºçããå¯èœæ§ããããŸãã
ã¢ããªã±ãŒã·ã§ã³ã¯UnicornãµãŒããŒãå®è¡ããŠãããããåèµ·åããŸãã WebrickãµãŒããŒã§ã¢ããªã±ãŒã·ã§ã³ããã¹ãããå Žåã¯ãããŒã®çµã¿åããCTRL + Cã䜿çšããŠåæ¢ããåèµ·åããã ãã§ååã§ãã
rails server
ã¹ããã7.ã¢ãžã¥ãŒã«ãšã¢ããªã±ãŒã·ã§ã³ã¯åäœããæºåãã§ããŠããŸãã 次ã®ã¹ãããã¯ããã°ã€ã³ããã³ç»é²ãªã³ã¯ããã³ãã¬ãŒãã«è¿œå ããããšã§ãã ããã¯æ¬¡ã®ããã«å®è¡ã§ããŸãã
<% if user_signed_in? %> <span>, <%= current_user.email %></span> <%= link_to '', destroy_user_session_path, :method => :delete %> <% else %> <%= link_to '', new_user_session_path %> <%= link_to '', new_user_registration_path %> <% end %>
ãŠãŒã¶ãŒããµã€ãã«ãã°ã€ã³ããŠããå Žåããããã«ã¡ã¯ã[ãŠãŒã¶ãŒã®ã¡ãŒã«]ããšæžããŠã次ã«çµäºããããã®ãªã³ã¯ãé 眮ããŸãã ãŠãŒã¶ãŒããŸã ãµã€ãã«ã¢ã¯ã»ã¹ããŠããªãå Žåãè¿ãã«2ã€ã®ããã°ã€ã³ãããã³ãç»é²ããªã³ã¯ããããŸãã æ¢åã®ãã«ããŒããªã³ã¯ã®ãã¹ãšããŠäœ¿çšããŸãã ã«ãŒãã4çªç®ã®ã¹ãããã§ã€ã³ã¹ããŒã«ããããªã¹ãã¯æ¬¡ã®ã³ãã³ãã§è¡šç€ºã§ããããšãæãåºãããŠãã ããã ãã«ããŒã¯ãã¬ãã£ãã¯ã¹ïŒå·Šç«¯ã®å+ '_path'ïŒã§æ§æãããŸã
bundle exec rake routes
ã¹ããã8.ä»åºŠã¯ãæå³ããç®çã§Deviseã®äœ¿çšãéå§ããŸãããµã€ãã®ç¹å®ã®ã»ã¯ã·ã§ã³ãžã®äžæ£ãŠãŒã¶ãŒã¢ã¯ã»ã¹ãå¶éããŸãã èš±å¯ããããŠãŒã¶ãŒã®ã¿ã泚æãå®äºããããšãèš±å¯ãããšããŸãã ãããè¡ãã«ã¯ãé©åãªã³ã³ãããŒã©ãŒã«ãã£ã«ã¿ãŒãè¿œå ããå¿ èŠããããŸãã
class OrdersController < ApplicationController before_filter :authenticate_user!, except => [:show, :index] def index # do something end def show # do something end def create # do something end end
ããã§ã¯ãã³ãŒãã§ããã£ã«ã¿ãªã³ã°ããªãã³ã³ãããŒã©ãŒã¡ãœããã®ãªã¹ãã2çªç®ã®ãã©ã¡ãŒã¿ãŒãšããŠbefore_filterã¡ãœããã«æž¡ããŸãã ãããã£ãŠãæ¿èªã¯æ³šæã®å®è¡ïŒ 泚æã¡ãœããã®æäœãžã®ã¢ã¯ã»ã¹ïŒã«ã®ã¿å¿ èŠã§ãã ãã¹ãŠã®ã³ã³ãããŒã©ãŒã¡ãœããããã£ã«ã¿ãŒããå¿ èŠãããå Žåã2çªç®ã®ãã©ã¡ãŒã¿ãŒãéä¿¡ããå¿ èŠã¯ãããŸããã æžãã ãïŒ
before_filter :authenticate_user!
䟿å©ãªãã«ããŒã¡ãœããã®ãªã¹ã
Deviseã«ã¯ãã³ã³ãããŒã©ãŒããã³ãã¥ãŒå ã§äœ¿çšã§ãã䟿å©ãªãã«ããŒãå«ãŸããŠããŸãã ãããã®ããã€ãã以äžã«ç€ºããŸãã
次ã®ãã«ããŒã䜿çšããŠãã·ã¹ãã ãžã®ãŠãŒã¶ãŒãã°ã€ã³ã®äºå®ã確èªã§ããŸãã
user_signed_in?
次ã®ãã«ããŒã䜿çšããŠããµã€ãã§çŸåšèš±å¯ãããŠãããŠãŒã¶ãŒã®ãªããžã§ã¯ããååŸããŸã
current_user
次ã®ããã«ãŠãŒã¶ãŒã®ã¡ãŒã«ã衚瀺ã§ããŸã
current_user.email
çŸåšã®ã»ãã·ã§ã³ãžã®ã¢ã¯ã»ã¹ã¯ã次ã®ããã«ããŠååŸã§ããŸãã
user_session
ããã§æåã®ããŒãã¯çµäºããŸãã次ã®ããŒãã§ã¯ããã®çŽ æŽãããèªèšŒã®å®ç³ã®ãããã»ã©éèŠã§ã¯ãªããããã»ã©èå³æ·±ããã®ã§ããªãä»ã®èšå®ã«ã€ããŠæžããŸãã
䜿çšãããæç®ã®ãªã¹ãïŒ
çŽ æã®ã¡ã€ã³ãœãŒã¹ãšããŠãå ¬åŒã®gemããã¥ã¡ã³ã-github.com/plataformatec/deviseã䜿çšãããŸãããããã¯ãç¬èªã®ã«ã¹ã¿ãã€ãºãšã¯ã¹ããªãšã³ã¹ã«ãã£ãŠè£å®ãããŠããŸãã