0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

railsチュートリアル第八章 ログアウト

Posted at

##ログアウト
ログアウト機能を追加する。

ユーザーが明示的にログアウトするまではログイン状態を保他なくてはならない。
ユーザーセッションを破棄するための有効なアクションをコントローラで作成。

####log_outメソッド
app/helpers/sessions_helper.rb

module SessionsHelper
.
.
.  
  # 現在のユーザーをログアウトする
  def log_out
    session.delete(:user_id)
    # ユーザーセッションを削除
    @current_user = nil
    # セキュリティ上nilの設定する。
    # 本来ならこれはしなくて良い。
  end
end

####セッションを破棄する(ユーザーのログアウト)
app/controllers/sessions_controller.rb

class SessionsController < ApplicationController
.
.
.
  def destroy
  # ユーザー削除
    log_out
    # log_outメソッドを発動
    redirect_to root_url
    # ホーム画面に戻る
  end
end

####ユーザーログアウトのテスト(無効なログインテストも1箇所改良)
test/integration/users_login_test.rb

require 'test_helper'

class UsersLoginTest < ActionDispatch::IntegrationTest
  
  def setup
    @user = users(:michael)
    # ハッシュで呼び出す。
    # なんでもハッシュで呼び出せるようになるのか?
  end
  
  test "login with valid email/invalid password" do
    get login_path
    assert_template 'sessions/new'
    post login_path, params: { session: { email:    @user.email,
                                          password: "invalid" } }
    assert_not is_logged_in?
    assert_template 'sessions/new'
    assert_not flash.empty?
    get root_path
    assert flash.empty?
  end
  
  test "login with valid information followed by logout" do
    get login_path
    # ログイン画面にいく
    post login_path, params: { session: { email:    @user.email,
                                          password: 'password' } }
    # データをデータベースに投稿
    assert is_logged_in?
    # ログインされているよね?
    assert_redirected_to @user
    # ユーザー情報を取り出すのか?
    follow_redirect!
    # もう一回確認
    assert_template 'users/show'
    # ユーザーページに移動する
    assert_select "a[href=?]", login_path, count: 0
    assert_select "a[href=?]", logout_path
    assert_select "a[href=?]", user_path(@user)
    delete logout_path
    assert_not is_logged_in?
    assert_redirected_to root_url
    follow_redirect!
    assert_select "a[href=?]", login_path
    # これは表示する
    assert_select "a[href=?]", logout_path,      count: 0
    assert_select "a[href=?]", user_path(@user), count: 0
    # 上2行は非表示になっていることを確認。
  end
end

ubuntu:~/environment/sample_app (basic-login) $ rails t
Running via Spring preloader in process 9774
Started with run options --seed 65113

  24/24: [============================] 100% Time: 00:00:02, Time: 00:00:02

Finished in 2.15159s
24 tests, 61 assertions, 0 failures, 0 errors, 0 skips

###演習
1.
ブラウザから[Log out]リンクをクリックし、どんな変化が起こるか確認してみましょう。また、リスト 8.35で定義した3つのステップを実行してみて、うまく動いているかどうか確認してみましょう
確認

2.cookiesの内容を調べてみて、ログアウト後にはsessionが正常に削除されていることを確認してみましょう。
どうしてもcookiesが消えない。
みている場所が違うのか。
わからない。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?