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

## 環境
- Laravel 8.6
- PHP 8.0
- Bootstrap 5.1

## 9.1 Remember me 機能

### 9.1.1 記憶トークンと暗号化

### 9.1.2 ログイン状態の保持

```php:/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 チェックボックス

```html:/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']) }}
```
```php:/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でログアウトしても削除されないのでとりあえずコメントアウト。

```php:/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 最後に