구 원트노/루비 온 레일즈

devise 젬을 통해 로그인 기능 구현하기

루비 온 레일즈에서는 여러가지 젬이 존재합니다. 

여기서 젬이란 대부분의 프로그래밍 언어에서의 라이브러리라고 생각하시면 됩니다. 

음 쉽게 말하자면 남이 만들어 놓은 기능들??이라고 생각할 수 있습니다.

이 젬을 잘 사용하면 개발을 더욱 효율적으로 효과적으로 할 수 있습니다.


오늘 사용할 devise 젬은 로그인 기능을 구현해놓은 젬인데 한 번 사용해 보겠습니다. 

모든 내용은  https://github.com/plataformatec/devise 에 설명되어져 있습니다.



일단 Gemfile 안에

1
gem 'devise'
cs

을 추가하고 bash에 bundle install 해줍니다.


그러면 레일즈 안에 devise젬이 설지가 되는데요.

그 이후에 레일즈를 사용하려면 bash에 rails generate devise:install 라고 입력하면 

이런 식으로 창이 뜹니다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
$ rails generate devise:install
Running via Spring preloader in process 1660
      create  config/initializers/devise.rb
      create  config/locales/devise.en.yml
===============================================================================
 
Some setup you must do manually if you haven't yet:
  1. Ensure you have defined default url options in your environments files. Here
     is an example of default_url_options appropriate for a development environment
     in config/environments/development.rb:
       config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
     In production, :host should be set to the actual host of your application.
  2. Ensure you have defined root_url to *something* in your config/routes.rb.
     For example:
       root to: "home#index"
  3. Ensure you have flash messages in app/views/layouts/application.html.erb.
     For example:
       
<%= notice %>
       
<%= alert %>
  4. You can copy Devise views (for customization) to your app by running:
       rails g devise:views
===============================================================================
cs


이어서 bash에 rails generate devise user 입력하면

1
2
3
4
5
6
7
8
9
10
rails generate devise user
Running via Spring preloader in process 1682
      invoke  active_record
      create    db/migrate/20160924141815_devise_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/models/user_test.rb
      create      test/fixtures/users.yml
      insert    app/models/user.rb
       route  devise_for :users
cs

devise 젬을 통해 만들어지는 user모델이 생성되어집니다. 


모델을 만들었으니 rake db:migrate 를 해줍니다.


여기서 로그인 이후의 페이지를 만들기 위해  rails generate controller home index를 통해 컨트롤러와 뷰를 만들어 줍니다.



이 후 생성된 app-views-home-index.html.erb 파일에

1
2
3
<%= current_user.email%>
 
<a href="users/sign_out" date-method="delete">로그아웃</a>
cs

을 추가시켜줍니다. 이는 로그인 했을때 로그인 한 사람의 이메일을 보여주고 로그아웃을 할 수 있는 버튼을 추가시켜준 것 입니다. 


추가로 app-controllers-home_controller.rb를 

1
2
3
4
5
6
7
class HomeController < ApplicationController
  def index
    unless user_signed_in?
      redirect_to "/users/sign_in"
    end
  end
end
cs

이렇게 변경해줍니다. 이는 아까 만든 index페이지를 로그인하지 않은 상태로 접근한다면 로그인 페이지로 가게 하는 코드입니다.


이 후 config-routes.rb 에서 

1
root 'home#index'
cs

를 추가해주면 첫 페이지가 아까 만든 index.html.erb로 변경되어집니다. 


그리고 rails 서버를 실행해보면 로그인 페이지가 구현이 된 것을 알 수 있습니다. 


이 예제 코드는 https://github.com/jomno/devise_exam 올려놓았습니다.