Rails6에 namespase를 사용하여 devise 도입

18189 단어 Rails6Railsdevise
AdminLTE3 템플릿에 devise를 사용하여 로그인 인증을 설치합니다.
role 등의 설정은 다른 기사에서

devise 초기 설정



gem 설치



Gemfile
gem 'devise'
$ bundle install --path vendor/bundle

devise 설정



이번에는/admin 아래에 devise 설치
rails g devise manage_user
rails g devise:views admin/manage_users
rails g devise:controllers admin/manage_user

코멘트 아웃 해제



필요한 기능을 주석 처리합니다.

devise_create_manage_users.rb
# frozen_string_literal: true

class DeviseCreateManageUsers < ActiveRecord::Migration[6.0]
  def change
    create_table :manage_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 :manage_users, :email,                unique: true
    add_index :manage_users, :reset_password_token, unique: true
    add_index :manage_users, :confirmation_token,   unique: true
    add_index :manage_users, :unlock_token,         unique: true
  end
end

모델도 마찬가지로 코멘트 아웃

ManageUser.rb
class ManageUser < ApplicationRecord
  # Include default devise modules. Others available are:
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable,
         :confirmable, :lockable, :timeoutable, :trackable
end

routes 설정



routes에 root를 추가하고 devise_for 설정을 작성합니다.
이제 /admin/manage_users/sign_up에서 액세스 할 수 있습니다.

/config/routes.rb
Rails.application.routes.draw do
  root to: "home#index"
   :
   :
  scope :admin, module: :admin do
    devise_for :manage_users, controllers: {
        sessions: 'admin/manage_users/sessions',
        passwords: 'admin/manage_users/passwords',
        registrations: 'admin/manage_users/registrations',
        confirmations: 'admin/manage_users/confirmations',
        unlocks: 'admin/manage_users/unlocks'
    }
   :
   :
  end
end

기타 설정 수정


development.rb 에 다음 추가

/config/environments/development.rb
config.action_mailer.default_url_options = { host: '127.0.0.1', port: 5000 }

인증을 부모 클래스에 추가



/admin/ApapplicationController.rb
class Admin::ApapplicationController < ActionController::Base
  :
  :
  before_action :authenticate_manage_user! # 追記
end

migration 실행


$ rails db:migrate

AdminLTE3에 결합하는 혼합



이번에는 registration 화면만 포함
아래 URL을 복사하여 layout 만들기/node_modules/admin-lte/pages/examples/register.html
/layouts/admin/registration.html.haml
!!!
%html
  %head
    %meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
    %meta{:charset => "utf-8"}/
    %meta{:content => "IE=edge", "http-equiv" => "X-UA-Compatible"}/
    %title MyAdmin 3 | Registration Page
    / Tell the browser to be responsive to screen width
    %meta{:content => "width=device-width, initial-scale=1", :name => "viewport"}/
    = csrf_meta_tags
    = csp_meta_tag
    = stylesheet_link_tag 'admin', media: 'all', 'data-turbolinks-track': 'reload'
    = javascript_pack_tag 'admin', 'data-turbolinks-track': 'reload'

  %body.hold-transition.register-page
    .register-box
      .register-logo
        %a{:href => "../../index2.html"}
          %b> Admin
          LTE
      .card
        = yield
    / /.register-box

/node_modules/admin-lte/pages/examples/register.html 에서
view 파일 재작성

/views/admin/manage_users/registrations/new.html.haml
.card-body.register-card-body
  %p.login-box-msg Register a new membership
  = form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f|
    = render "devise/shared/error_messages", resource: resource
    .input-group.mb-3
      = f.email_field :email, class: "form-control", autofocus: true, autocomplete: "email", placeholder: "Email"
      .input-group-append
        .input-group-text
          %span.fas.fa-envelope
    .input-group.mb-3

      = f.password_field :password, class: "form-control", autocomplete: "new-password", placeholder: "Password"
      .input-group-append
        .input-group-text
          %span.fas.fa-lock
    .input-group.mb-3
      = f.password_field :password_confirmation, class: "form-control", autocomplete: "new-password", placeholder: "Retype password"
      .input-group-append
        .input-group-text
          %span.fas.fa-lock
    .row
      .col-8
        .icheck-primary
          %input#agreeTerms{:name => "terms", :type => "checkbox", :value => "agree"}/
          %label{:for => "agreeTerms"}
            I agree to the
            %a{:href => "#"} terms
      .col-4
        = f.submit "Sign up", class: "btn btn-primary btn-block", type: "submit"
  .social-auth-links.text-center
    %p - OR -
    %a.btn.btn-block.btn-primary{:href => "#"}
      %i.fab.fa-facebook.mr-2
      Sign up using Facebook
    %a.btn.btn-block.btn-danger{:href => "#"}
      %i.fab.fa-google-plus.mr-2
      Sign up using Google+
  = render "admin/manage_users/shared/links"

확인하다



h tp://127.0.0.1:5000/아 d 민

좋은 웹페이지 즐겨찾기