환경
- 루비 버전 : ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin17]
- 레일즈 버전 : Rails 5.1.6
구현
- Devise 버전 : 4.4.3
- mailgun 버전 : 1.1.6
$ rails g devise User
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable
end
class DeviseCreateUsers < ActiveRecord::Migration[5.1]
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 null: false
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
이제 마이그레이션을 해준다.
$ bin/rake db:migrate
그 다음 https://www.mailgun.com 해당 홈피에 들어가 가입한다. 도메인 없이 300개 무료 이메일을 발송해준다.
가입하면 이렇게 메일 전송 도메인을 획득할 수 있다.
상단 네비게이션 바에서 Account Settings > Authorized Recipients 을 선택해서 보낼 이메일 주소를 적는다.
이메일로 이동해서 인증하면 준비완료
그다음 config/environments/development.rb 로 이동해서 아래의 내용을 적어 준다.
config.action_mailer.delivery_method = :mailgun
config.action_mailer.mailgun_settings = {
api_key: 'fb0XXXXXXXXXXXXXXXXXXXXXX-3XXXXXXXf-5XXXXXX2',
domain: 'sandboxHAHAHAHAHAHAHAHAHAHAc6b4e.mailgun.org',
}
config.action_mailer.default_url_options = { host: 'localhost:3000' }
Delivery_method와 mailgun_settings는 사용할 메일링 API의 정보이다. 하단의 default_url_options 는 확인 이메일에 붙을 도메인주소를 위해 사용된다. 아래와 같이 적으면 테스트 환경에서도 링크를 클릭하여 인증이 가능하다. 만일 포트를 변경하여 서버를 올렸다면 해당 포트를 변경해주도록
프로뎍션 환경도 동일하다. default_url_options를 본인의 도메인으로 변경하면 된다.
'Ruby on Rails' 카테고리의 다른 글
문자열 내에 링크가 있으면 <a href> 구문을 추가해주는 헬퍼 (0) | 2021.03.09 |
---|---|
루비 정규식 테스트 사이트 (0) | 2018.07.17 |
인덱스가 걸려있는 컬럼을 지우면 어떻게 될까? (0) | 2018.03.13 |
레일즈 벨리데이션은 믿을 수 있는가? (0) | 2017.09.07 |