LoginSignup
0
0

More than 1 year has passed since last update.

Laravel で Ruby on Rails チュートリアル【9章】リメンバートークン

Last updated at Posted at 2020-11-14

環境

  • Laravel 8.6
  • PHP 8.0
  • Bootstrap 5.1

9.1 Remember me 機能

9.1.1 記憶トークンと暗号化

9.1.2 ログイン状態の保持

/app/Http/Controllers/SessionController.php
    public function store(Request $request)
    {
        $user = User::where("email", strtolower($request->email))->first();
        if ($user && Hash::check($request->password, $user->password)) {
            Auth::login($user, true);
            return redirect()->route("users.show", $user);
        } else {
            session()->flash('message', ['danger' => 'Invalid email/password combination']);
            return back()->withInput();
        }
    }

9.1.3 ユーザーを忘れる

9.2 Remember me チェックボックス

/resources/views/sessions/create.blade.php
                {{ Form::hidden("remember_me", "0") }}
                {{ Form::checkbox("remember_me", "1", null, ["id" => "remember_me", "class" => "form-check-input"]) }}
                {{ Form::label('remember_me', 'Remember me on this computer', ['class' => 'form-check-label']) }}
/app/Http/Controllers/SessionController.php
    public function store(Request $request)
    {
        $user = User::where("email", Str::lower($request->email))->first();
        if ($user && Hash::check($request->password, $user->password)) {
            Auth::login($user, $request->remember_me === "1");
            return redirect()->route("users.show", $user);
        } else {
            session()->flash('message', ['danger' => 'Invalid email/password combination']);
            return back()->withInput();
        }
    }

9.3 Remember me のテスト

9.3.1 Remember me ボックスをテストする

ブラウザでログアウトするとcookieが削除されるが、phpunitでログアウトしても削除されないのでとりあえずコメントアウト。

/tests/Feature/UsersLoginTest.php
    public function testLoginRemember()
    {
        $response = $this->followingRedirects()
                        ->post(route("login"), [
                            'email' => $this->user->email,
                            'password' => $this->user_pass,
                            'remember_me' => "1"
                        ]);
        $i = 0;
        foreach ($response->headers->getCookies() as $cookie) {
            if (preg_match('/^remember_web_\w+$/', $cookie->getName())) {
                $i++;
            }
        }
        $this->assertSame(1, $i);
    }

    public function testLoginNotRemember()
    {
        /*
        $response = $this->followingRedirects()
            ->post(route("login"), [
                'email'       => $this->user->email,
                'password'    => $this->user_pass,
                'remember_me' => "1",
            ]);
        */
        $response = $this->followingRedirects()->delete('/logout');
        $response = $this->followingRedirects()
                        ->post(route("login"), [
                            'email' => $this->user->email,
                            'password' => $this->user_pass,
                            'remember_me' => "0"
                        ]);
        $i = 0;
        foreach ($response->headers->getCookies() as $cookie) {
            if (preg_match('/^remember_web_\w+$/', $cookie->getName())) {
                $i++;
            }
        }
        $this->assertSame(0, $i);
    }

9.3.2 Remember me をテストする

9.4 最後に

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