deviseã®How To: Allow users to sign in with something other than their email address, emailã¢ãã¬ã¹ä»¥å¤ã§ãµã¤ã³ã¤ã³ãè¡ãæ¹æ³ã«ã¤ãã¦ç¿»è¨³ãã¤ã¤å®è·µãã¦ããã¾ãã
wiki
ä»åã¯å
¬å¼wikiã«ããããã«ãusernameã¨ããfieldã§ãµã¤ã³ã¤ã³ã§ããããã«ãã¦ããã¾ãããé©å®ç½®ãæããã°å¥½ããªååã®ã«ã©ã ããµã¤ã³ã¤ã³ç¨ã®ã«ã©ã ã«ãããã¨ãã§ãã¾ãã
è¨èªãã©ã¤ãã©ãªã®ãã¼ã¸ã§ã³
- Ruby 2.6.3
- Rails 6.0.2.2
- devise 4.7.1
- ãã³ãã¬ã¼ãã¯slimã使ç¨
å°å
¥ããåºæ¬çãªå®è£
ãç¥ãããå ´åã¯æå°æ§æå°å
¥ãããã°ã¢ã¦ãã¾ã§ãåç
§ã
ã©ããªãã¨ãã§ããããã«ãªãã®ã
ãã®ããã«email以å¤ã®ã¦ãã¼ã¯ãªã«ã©ã ã§èªè¨¼ã§ããããã«ãªãã¾ãã
äºåæºå
äºåæºåã¨ãã£ã¦ãã大æããã«ã³ã¼ããæ¸ãæããå¿
è¦ã¯ããã¾ãããdeviseãæä¾ãã¦ããè¨å®ãå¤æ´ããèªè¨¼ãã¼ã«è¨å®ããã¦ããemailã«ã©ã ãå¤æ´ãã¾ãããã
ããã©ã«ãã§ã¯config/initializers/devise.rb
ã«config.authentication_keys = [:emil]
ã¨ããè¨å®ãã³ã¡ã³ãã¢ã¦ãããã¦ããã®ã§ãã³ã¡ã³ãã¢ã¦ããå¤ãã¦ä»»æã®ã«ã©ã ã«æ¸ãæãã¾ããä»åã§è¨ãã°username
ã¨ããã«ã©ã ã«ãããã®ã§ä¸è¨ã®ããã«ãã¾ãã
Devise.setup do |config|
`
`
ä¸ç¥ ~
config.authentication_keys = [:username] #ã³ã¡ã³ãã¢ã¦ããå¤ãã¦æ¸ãæã
`
`
end
ããã§èªè¨¼ãã¼ã«è¨å®ããã¦ããã«ã©ã ãusernameã«å¤æ´ããã¾ãã
è£è¶³ï¼ãè¤æ°ã®ã¢ãã«ã§deviseã使ç¨ãã¦ããå ´åã§ä¸é¨ã®ã¢ãã«ã®ãã¼ã ãemailã«ããããªãå ´å
ä¾ãã°userã¢ãã«ä»¥å¤ã«ã管çuserã¨ãã¦admin_user
ã¨ããã¢ãã«ãå©ç¨ãã¦ããç¶æ
ãæ³å®ãã¦ã¿ã¾ããã©ã¡ããdevise gemã使ç¨ãã¦èªè¨¼ãè¡ã£ã¦ããã¨ãã¾ãã
userã¢ãã«ã¯usernameã¨passwordã¨ããï¼ã¤ã®ãã¼ã§èªè¨¼ããadmin_user
ã¯ããã©ã«ãã®email, passwordã«ãã£ã¦èªè¨¼ãããã¨ããè¦ä»¶ãæ³å®ãã¦ã¿ã¾ãããã
ãã®è¦ä»¶ã¯ãæåã«ç´¹ä»ããconfig/initializers/devise.rb
ã«ãããã¼ãæ¸ãæããæ¹æ³ã¯å¾çã§ã¯ããã¾ããããã®ãã¡ã¤ã«ã®å
容ã¯deviseã®å
¨ã¦ã®è¨å®ãæ
ã£ã¦ãã¾ãã®ã§ãåå¥ã«å¤æ´ãããå ´åã¯ãã¡ãã®æ¹æ³ãæ¡ç¨ãã¾ãããã
deviseã使ç¨ãã¦ããmodelã«åå¥ã«è¨è¼ãå ãã¾ãã
app/models/user.rb
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
ããã©ã«ãã§ã¯Userã¢ãã«ã¯ãã®ããã«ãªã£ã¦ãã¾ãã®ã§ã
:authentication_keys => [:username]
ã¨ãããªãã·ã§ã³ã:database_authenticatableã«è¨å®ãã¾ãããã
追å å¾ã®userã¢ãã«ã¯ãã®ããã«ãªãã¾ãã
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, ï¼authentication_keys => [:username] :registerable,:recoverable, :rememberable, :validatable
end
ï¼è£è¶³çµäºï¼
ã§ã¯å®è£
ã«æ»ãã¾ããããconfig/initializers/devise.rbã«è¿½è¨ããã,è£è¶³ã«ãããããªæ¡ä»¶ã®å ´åã¯modelå´ã«åå¥ã«è¨è¼ãæ¸ãã ç¶æ
ã«ãªã£ã¦ããã¨æãã¾ãã
以éã®ä½æ¥ã¯å
±éã«ãªãã¾ããRnailså´ã§validationãè¨å®ãã¦ããããã®ã§ã
validates :email, uniqueness: true
validates :username, uniqueness: true
ãã¢ãã«ã«è¿½å ãã¦ããã¾ããemail, usernameã«ã©ã ãã¦ãã¼ã¯ã§ããã¨ããããªãã¼ã·ã§ã³ã§ããã追è¨ããUserã¢ãã«ã¯ä¸è¨ã®ããã«ãªãã¾ããemailã«ãã¦ãã¼ã¯å¶ç´ãã¤ãã¦ããã®ã¯deviseãæä¾ããä»ã®æ©è½ã§userãä¸æã§ããå¿
è¦ãããããã§ãã
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
validates :email, uniqueness: true
validates :username, uniqueness: true
end
ããã¾ã§ã§åæè¨å®ã®è¨è¿°ã¯ä¸æ¦çµäºã§ãã
ãã ãèå¿ã®usernameã«ã©ã ãã¾ã ç¨æãã¦ããªãã®ã§ãmigrationfileãä½æãã¦ããã¾ãã
$ rails g migration add_username_to_users username:string:uniq
ãã®è¨è¿°ã§ã¦ãã¼ã¯ãªå¶ç´ããããstringåã®usernameã«ã©ã ãusersãã¼ãã«ã«è¿½å ãã¾ãã
migrationãã¡ã¤ã«ãä½æããããmigrateãç¶ãã¦è¡ãã¾ãã
$ rails db:migrate
ããã¾ã§ã§ä¸åº¦schemaãã¡ã¤ã«ã確èªãã¦ããã¾ãããã
ActiveRecord::Schema.define(version: 2020_05_13_061723) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "username"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
t.index ["username"], name: "index_users_on_username", unique: true
end
end
å
ã»ã©ã®migrateã§ä¸è¨ã®ããã«usernameã«ã©ã ãä½æãããindexãè²¼ããã¦ãããã¨ã確èªãã¦ãã ããã
viewãã¡ã¤ã«ãã«ã¹ã¿ãã¤ãºãã¦usernameãæ°è¦ç»é²ã®éã«ç»é²ã§ããããã«ãã
ããã¾ã§ã§usernameãã¢ãã«ã®ãã¼ã«ãã¦ãuserã¢ãã«ã«ç»é²ã§ããããã«ãªãã¾ããããæ°è¦ç»é²ã®ç»é¢ã§ç»é²ã§ããããã«ãªã£ã¦ããªããããviewãã«ã¹ã¿ãã¤ãºãã¦ããã¾ãã
$ rails g devise:views user
ã§userã¢ãã«ã«å¯¾å¿ããviewãã¡ã¤ã«ãä½æã§ãã¾ã
æ°è¦ç»é²ã«é¢ããviewãã¡ã¤ã«ã¯çææã¯ä¸è¨ã®ããã«ãªã£ã¦ããã¯ãã§ãã
app/views/users/registrations/new.html.slim
h2
| Sign usp
= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f|
= render "devise/shared/error_messages", resource: resource
.field
= f.label :email
br
= f.email_field :email, autofocus: true, autocomplete: "email"
.field
= f.label :password
- if @minimum_password_length
em
| (
= @minimum_password_length
| characters minimum)
br
= f.password_field :password, autocomplete: "new-password"
.field
= f.label :password_confirmation
br
= f.password_field :password_confirmation, autocomplete: "new-password"
.actions
= f.submit "Sign up"
= render "users/shared/links"
ããã«usernameãå
¥åãããã©ã¼ã ãå
¥ãã¦ããããã®ã§ãä¸è¨ã®ããã«ãã©ã¼ã ã追å ãã¦ããã¾ãããã
h2
| Sign usp
= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f|
= render "devise/shared/error_messages", resource: resource
.field
= f.label :email
br
= f.email_field :email, autofocus: true, autocomplete: "email"
.field
= f.label :username
br
= f.text_field :username, autocomplete: "username"
.field
= f.label :password
- if @minimum_password_length
em
| (
= @minimum_password_length
| characters minimum)
br
= f.password_field :password, autocomplete: "new-password"
.field
= f.label :password_confirmation
br
= f.password_field :password_confirmation, autocomplete: "new-password"
.actions
= f.submit "Sign up"
= render "users/shared/links"
ãã®ããã«ãªãã¾ããã
ãã®æ®µéã§ç»é¢ã«ã¢ã¯ã»ã¹ãã¦ã¿ãã¨ã表示ã¯ãããã®ã§ãããå®éã«å¤ãéä¿¡ãã¦ç»é²ãããã¨ã¯ã§ãã¾ããã
å¤ã渡ãã¦ãããããã«ã¯controllerå´ã«ãå°ãæãå ãã¦ãããå¿
è¦ãããã¾ããæä½ããã®ã¯users/registrations_controller.rbã§ãå
·ä½çã«ã¯ä¸è¨ã®ããã«ãªã£ã¦ãã¾ãã
# frozen_string_literal: true
class Users::RegistrationsController < Devise::RegistrationsController
# before_action :configure_sign_up_params, only: [:create]
# before_action :configure_account_update_params, only: [:update]
# GET /resource/sign_up
# def new
# super
# end
# POST /resource
# def create
# super
# end
# GET /resource/edit
# def edit
# super
# end
# PUT /resource
# def update
# super
# end
# DELETE /resource
# def destroy
# super
# end
# GET /resource/cancel
# Forces the session data which is usually expired after sign
# in to be expired now. This is useful if the user wants to
# cancel oauth signing in/up in the middle of the process,
# removing all OAuth session data.
# def cancel
# super
# end
# protected
# If you have extra params to permit, append them to the sanitizer.
# def configure_sign_up_params
# devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute])
# end
# If you have extra params to permit, append them to the sanitizer.
# def configure_account_update_params
# devise_parameter_sanitizer.permit(:account_update, keys: [:attribute])
# end
# The path used after sign up.
# def after_sign_up_path_for(resource)
# super(resource)
# end
# The path used after sign up for inactive accounts.
# def after_inactive_sign_up_path_for(resource)
# super(resource)
# end
end
ãã®ãã¡ã¤ã«ã®ä¸ã®
before_action :configure_sign_up_params, only: [:create]
ã¨
def configure_sign_up_params
devise_parameter_sanitizer.permit(:account_update, keys:[:attribute])
end
ã®ã³ã¡ã³ãã¢ã¦ããå¤ãã¦ããã¾ãã
nameã«ã©ã ã«å¤ãå
¥ãããã®ã§ã:attribute
ã¨ãªã£ã¦ããé¨åã:username
ã«å¤ãã¦ããã¾ããããã§ãusernameãåãåããç»é²ãããã¨ãã§ããããã«ãªãã¾ãã
ããã¾ã§ã§ä¸æ¦åä½ç¢ºèªããã¦ããã¾ãã
usernameã¨passwordã§loginã§ããããã«ãã
æ°è¦ç»é²ãç¡äºã§ããããã«ãªã£ãã®ã§ãç»é²ããusernameã§ãã°ã¤ã³ã§ããããã«ãã¦ããã¾ãããã
ãã°ã¤ã³ã«å¯¾å¿ããviewãã¡ã¤ã«ã¯app/views/users/sessions/new.html.slim
ã«ãªãã¾ãã
å
容ã¯ããã©ã«ãã ã¨
h2
| Log in
= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f|
.field
= f.label :email
br
= f.email_field :email, autofocus: true, autocomplete: "email"
.field
= f.label :password
br
= f.password_field :password, autocomplete: "current-password"
- if devise_mapping.rememberable?
.field
= f.check_box :remember_me
= f.label :remember_me
.actions
= f.submit "Log in"
= render "users/shared/links"
ãã®ããã«ãªã£ã¦ãã¾ãã
ãã®emailã®é¨åãusernameã«å¤ãã¦ããã¾ãããã
h2
| Log in
= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f|
.field
= f.label :username
br
= f.email_field :username, autocomplete: "username"
.field
= f.label :password
br
= f.password_field :password, autocomplete: "current-password"
- if devise_mapping.rememberable?
.field
= f.check_box :remember_me
= f.label :remember_me
.actions
= f.submit "Log in"
= render "users/shared/links"
ãã®ããã«ãusernameã¨passwordã§ç»é²ãããã©ã¼ã ã«å¤æ´ãã¾ããã
ã§ã¯å®éã«ã¢ã¯ã»ã¹ãã¦ã試ãã¦ã¿ã¾ãã
ãã®ããã«usernameã¨passwordã§ãã°ã¤ã³ã§ãã¾ããï¼
validetableã使ç¨ãã¦ããå ´åã®å¯¾å¦ã«ã¤ãã¦
ããã§ç¡äºçµäºã¨è¡ãããã¨ãããªã®ã§ãããemailããã¼ã«ãã¦ããªã以ä¸ããã¼ã¿ã®æ´æ°ãå¤æ´ã«emailãå¿
é ã¨ããã®ã¯ãã³ã»ã³ã¹ãªã®ã§ãmodelã«å¹¾ã¤ãã¡ã½ããã追è¨ãã¦ããªãã¼ã·ã§ã³ãç¡å¹åããã¾ãã
def email_required?
false
end
def email_changed?
false
end
ãã®ï¼ã¤ã®ã¡ã½ããã追å ãã¾ããUserã¢ãã«ã«deviseãå
¥ãã¦ããã®ã§ã
追è¨ããå ´æã¯app/models/user.rbã«ãªãã¾ãã
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
validates :email, uniqueness: true
validates :username, uniqueness: true
def email_required?
false
end
def email_changed?
false
end
end
ããã§usernameãå¤æ´ãããå ´åã«emailãå¿
é ã¨ããªãããã«ã§ãã¾ããï¼
æå¾ã«
å®éã®ã¢ããªããµã¼ãã¹ã§ã¯emailã使ã£ã¦èªè¨¼ãããã¨ãå¤ãã¨æãã¾ãããç°¡åãªã¢ããªã社å
ã§çºè¡ãããIDããã¼ã«ãã¦èªè¨¼ãè¡ãããå ´åãªã©ãemailã¢ãã¬ã¹ä»¥å¤ããã¼ã«ãã¦deviseã®èªè¨¼ãå©ç¨ãããã¨ã§ãèªè¨¼ãã¸ãã¯ãdeviseã«ä»»ãããã¨ãã§ããã®ã¯ã¨ã¦ãè¯ãã§ããã