teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

12

リネーム

2019/10/16 09:37

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -1,8 +1,13 @@
1
- いろいろ方法はありますが,標準の `Auth::attempt()` を使う方法に乗るのであれば,`UserProvider` を自分で実装する方法が一番きれいなのでお勧めしておきます。
1
+ いろいろ方法はありますが,標準の `Auth::attempt()` を使う方法に乗るのであれば,`UserProvider` を自分で実装する方法が一番きれいなのでお勧めしておきます。
2
2
 
3
3
  - 方法: [認証 6.0 Laravel](https://readouble.com/laravel/6.0/ja/authentication.html)
4
4
  - 継承するデフォルト実装: [auth/EloquentUserProvider.php](https://github.com/illuminate/auth/blob/6.x/EloquentUserProvider.php)
5
5
 
6
+ `LoginController::credentials()` をオーバーライドした上でこちらも実装してください。
7
+
8
+ - HTTP リクエストからクレデンシャル配列を取り出す責務 → `LoginController::credentials()`
9
+ - クレデンシャル配列を使ってデータベースから取ってきてユーザオブジェクトまたはNULLを返す責務 → `UserProvider::retrieveByCredentials(array $credentials)`
10
+
6
11
  ### 実装例 (PHP7.1以降向け)
7
12
 
8
13
  ```PHP
@@ -15,7 +20,7 @@
15
20
  use Illuminate\Auth\EloquentUserProivder;
16
21
  use Illuminate\Contracts\Auth\Authenticatable;
17
22
 
18
- class AppUserProvider extends EloquentUserProivder
23
+ class StatusAwareEloquentUserProvider extends EloquentUserProivder
19
24
  {
20
25
  public function retrieveByCredentials(array $credentials): ?Authenticatable
21
26
  {
@@ -47,8 +52,8 @@
47
52
  {
48
53
  $this->registerPolicies();
49
54
 
50
- Auth::provider('app', function (Application $app, array $config) {
55
+ Auth::provider('status_aware_eloquent', function (Application $app, array $config) {
51
- return new AppUserProvider($app['hash'], $config['model']);
56
+ return new StatusAwareEloquentUserProvider($app['hash'], $config['model']);
52
57
  });
53
58
  }
54
59
  }
@@ -57,7 +62,7 @@
57
62
  ```PHP
58
63
  'providers' => [
59
64
  'users' => [
60
- 'driver' => 'app',
65
+ 'driver' => 'status_aware_eloquent',
61
66
  'model' => App\User::class,
62
67
  ],
63
68
  ],

11

バージョン追従

2019/10/16 09:37

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -1,7 +1,7 @@
1
1
  いろいろ方法はありますが,標準の `Auth::attempt()` を使う方法に乗るのであれば,`UserProvider` を自分で実装する方法が一番きれいなのでお勧めしておきます。
2
2
 
3
3
  - 方法: [認証 6.0 Laravel](https://readouble.com/laravel/6.0/ja/authentication.html)
4
- - 継承するデフォルト実装: [auth/EloquentUserProvider.php](https://github.com/illuminate/auth/blob/a8a03aa650eadfa8e2a1a11709fa7de47ca859f3/EloquentUserProvider.php)
4
+ - 継承するデフォルト実装: [auth/EloquentUserProvider.php](https://github.com/illuminate/auth/blob/6.x/EloquentUserProvider.php)
5
5
 
6
6
  ### 実装例 (PHP7.1以降向け)
7
7
 

10

newModelQuery() が生えていたのでショートハンドに変更

2019/10/16 09:27

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -1,6 +1,6 @@
1
1
  いろいろ方法はありますが,標準の `Auth::attempt()` を使う方法に乗るのであれば,`UserProvider` を自分で実装する方法が一番きれいなのでお勧めしておきます。
2
2
 
3
- - 方法: [認証 5.6 Laravel](https://readouble.com/laravel/5.6/ja/authentication.html)
3
+ - 方法: [認証 6.0 Laravel](https://readouble.com/laravel/6.0/ja/authentication.html)
4
4
  - 継承するデフォルト実装: [auth/EloquentUserProvider.php](https://github.com/illuminate/auth/blob/a8a03aa650eadfa8e2a1a11709fa7de47ca859f3/EloquentUserProvider.php)
5
5
 
6
6
  ### 実装例 (PHP7.1以降向け)
@@ -21,7 +21,7 @@
21
21
  {
22
22
  if (!isset($credentials['email'])) return null;
23
23
 
24
- return $this->createModel()->newQuery()
24
+ return $this->newModelQuery()
25
25
  ->whereEmail($credentials['email'])
26
26
  ->whereIn('status', [1, 4])
27
27
  ->first();

9

remove unused

2019/10/16 09:25

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -13,7 +13,6 @@
13
13
  namespace App\Auth;
14
14
 
15
15
  use Illuminate\Auth\EloquentUserProivder;
16
- use Illuminate\Support\Arr;
17
16
  use Illuminate\Contracts\Auth\Authenticatable;
18
17
 
19
18
  class AppUserProvider extends EloquentUserProivder

8

implements → extends

2019/06/11 08:32

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -16,7 +16,7 @@
16
16
  use Illuminate\Support\Arr;
17
17
  use Illuminate\Contracts\Auth\Authenticatable;
18
18
 
19
- class AppUserProvider implements EloquentUserProivder
19
+ class AppUserProvider extends EloquentUserProivder
20
20
  {
21
21
  public function retrieveByCredentials(array $credentials): ?Authenticatable
22
22
  {

7

typo

2019/06/11 08:31

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -16,7 +16,7 @@
16
16
  use Illuminate\Support\Arr;
17
17
  use Illuminate\Contracts\Auth\Authenticatable;
18
18
 
19
- class AppUserProvider implements EloquentUserProivder;
19
+ class AppUserProvider implements EloquentUserProivder
20
20
  {
21
21
  public function retrieveByCredentials(array $credentials): ?Authenticatable
22
22
  {

6

space

2018/04/14 09:46

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -48,7 +48,7 @@
48
48
  {
49
49
  $this->registerPolicies();
50
50
 
51
- Auth::provider('app', function(Application $app, array $config) {
51
+ Auth::provider('app', function (Application $app, array $config) {
52
52
  return new AppUserProvider($app['hash'], $config['model']);
53
53
  });
54
54
  }

5

app

2018/04/14 09:45

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -39,6 +39,7 @@
39
39
 
40
40
  use App\Auth\AppUserProvider;
41
41
  use Illuminate\Support\Facades\Auth;
42
+ use Illuminate\Foundation\Application;
42
43
  use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
43
44
 
44
45
  class AuthServiceProvider extends ServiceProvider
@@ -47,8 +48,8 @@
47
48
  {
48
49
  $this->registerPolicies();
49
50
 
50
- Auth::provider('app', function($app, array $config) {
51
+ Auth::provider('app', function(Application $app, array $config) {
51
- return new AppUserProvider($this->app['hash'], $config['model']);
52
+ return new AppUserProvider($app['hash'], $config['model']);
52
53
  });
53
54
  }
54
55
  }

4

追記

2018/04/14 09:44

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  ### 実装例 (PHP7.1以降向け)
7
7
 
8
- ```
8
+ ```PHP
9
9
  <?php
10
10
 
11
11
  declare(strict_types=1);
@@ -28,4 +28,37 @@
28
28
  ->first();
29
29
  }
30
30
  }
31
+ ```
32
+
33
+ ```PHP
34
+ <?php
35
+
36
+ declare(strict_types=1);
37
+
38
+ namespace App\Providers;
39
+
40
+ use App\Auth\AppUserProvider;
41
+ use Illuminate\Support\Facades\Auth;
42
+ use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
43
+
44
+ class AuthServiceProvider extends ServiceProvider
45
+ {
46
+ public function boot(): void
47
+ {
48
+ $this->registerPolicies();
49
+
50
+ Auth::provider('app', function($app, array $config) {
51
+ return new AppUserProvider($this->app['hash'], $config['model']);
52
+ });
53
+ }
54
+ }
55
+ ```
56
+
57
+ ```PHP
58
+ 'providers' => [
59
+ 'users' => [
60
+ 'driver' => 'app',
61
+ 'model' => App\User::class,
62
+ ],
63
+ ],
31
64
  ```

3

strict_types

2018/04/14 09:32

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -3,9 +3,13 @@
3
3
  - 方法: [認証 5.6 Laravel](https://readouble.com/laravel/5.6/ja/authentication.html)
4
4
  - 継承するデフォルト実装: [auth/EloquentUserProvider.php](https://github.com/illuminate/auth/blob/a8a03aa650eadfa8e2a1a11709fa7de47ca859f3/EloquentUserProvider.php)
5
5
 
6
+ ### 実装例 (PHP7.1以降向け)
7
+
6
8
  ```
7
9
  <?php
8
10
 
11
+ declare(strict_types=1);
12
+
9
13
  namespace App\Auth;
10
14
 
11
15
  use Illuminate\Auth\EloquentUserProivder;

2

型指定

2018/04/14 09:20

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -10,18 +10,13 @@
10
10
 
11
11
  use Illuminate\Auth\EloquentUserProivder;
12
12
  use Illuminate\Support\Arr;
13
+ use Illuminate\Contracts\Auth\Authenticatable;
13
14
 
14
15
  class AppUserProvider implements EloquentUserProivder;
15
16
  {
16
- /**
17
- * Retrieve a user by the given credentials.
18
- *
19
- * @param array $credentials
20
- * @return \Illuminate\Contracts\Auth\Authenticatable|null
21
- */
22
- public function retrieveByCredentials(array $credentials)
17
+ public function retrieveByCredentials(array $credentials): ?Authenticatable
23
18
  {
24
- if (!isset($credentials['email'])) return;
19
+ if (!isset($credentials['email'])) return null;
25
20
 
26
21
  return $this->createModel()->newQuery()
27
22
  ->whereEmail($credentials['email'])

1

追加

2018/04/14 09:18

投稿

mpyw
mpyw

スコア5223

answer CHANGED
@@ -1,4 +1,32 @@
1
1
  いろいろ方法はありますが,標準の `Auth::attempt()` を使う方法に乗るのであれば,`UserProvider` を自分で実装する方法が一番きれいなのでお勧めしておきます。
2
2
 
3
- - [認証 5.6 Laravel](https://readouble.com/laravel/5.6/ja/authentication.html)
4
- - [auth/EloquentUserProvider.php at a8a03aa650eadfa8e2a1a11709fa7de47ca859f3 · illuminate/auth](https://github.com/illuminate/auth/blob/a8a03aa650eadfa8e2a1a11709fa7de47ca859f3/EloquentUserProvider.php)
3
+ - 方法: [認証 5.6 Laravel](https://readouble.com/laravel/5.6/ja/authentication.html)
4
+ - 継承するデフォルト実装: [auth/EloquentUserProvider.php](https://github.com/illuminate/auth/blob/a8a03aa650eadfa8e2a1a11709fa7de47ca859f3/EloquentUserProvider.php)
5
+
6
+ ```
7
+ <?php
8
+
9
+ namespace App\Auth;
10
+
11
+ use Illuminate\Auth\EloquentUserProivder;
12
+ use Illuminate\Support\Arr;
13
+
14
+ class AppUserProvider implements EloquentUserProivder;
15
+ {
16
+ /**
17
+ * Retrieve a user by the given credentials.
18
+ *
19
+ * @param array $credentials
20
+ * @return \Illuminate\Contracts\Auth\Authenticatable|null
21
+ */
22
+ public function retrieveByCredentials(array $credentials)
23
+ {
24
+ if (!isset($credentials['email'])) return;
25
+
26
+ return $this->createModel()->newQuery()
27
+ ->whereEmail($credentials['email'])
28
+ ->whereIn('status', [1, 4])
29
+ ->first();
30
+ }
31
+ }
32
+ ```