Browse Source

:recycle: Redefine the UserProvider with tokens

master
Clement Desmidt 1 month ago
parent
commit
c75081cf09

+ 1
- 1
app/Http/Controllers/LinksController.php View File

@@ -13,7 +13,7 @@ class LinksController extends Controller
13 13
     public function add(Request $request)
14 14
     {
15 15
         /** @var User $user */
16
-        $user   = $request->user();
16
+        $user   = $request->user()->user;
17 17
         $email  = $user->email;
18 18
 
19 19
         $url = $request->input('u');

+ 2
- 2
app/Http/Controllers/UsersController.php View File

@@ -34,8 +34,8 @@ class UsersController extends Controller
34 34
             $user->save();
35 35
         }
36 36
 
37
-        if ($user->confirmed ) {
38
-            return response()->json(['error' => 'Your account already exists'], 422);
37
+        if ( $user->confirmed ) {
38
+            return response()->json(['error' => 'Your account already exists.'], 422);
39 39
         }
40 40
 
41 41
         Mail::to($user->email)->send(new Confirmation($user));

+ 1
- 1
app/Http/Middleware/Authenticate.php View File

@@ -28,7 +28,7 @@ class Authenticate extends \Illuminate\Auth\Middleware\Authenticate
28 28
             return response('Unauthorized.', 401);
29 29
         }
30 30
 
31
-        if (! $this->auth->user()->confirmed) {
31
+        if (! $this->auth->user()->user->confirmed) {
32 32
             return response('Unauthorized.', 401);
33 33
         }
34 34
 

+ 5
- 6
app/Providers/AuthServiceProvider.php View File

@@ -2,11 +2,9 @@
2 2
 
3 3
 namespace App\Providers;
4 4
 
5
-use App\User;
6
-use App\Services\Auth\TokenGuard;
5
+use App\Token;
7 6
 use Illuminate\Http\Request;
8 7
 use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
9
-use Illuminate\Support\Facades\Auth;
10 8
 
11 9
 class AuthServiceProvider extends ServiceProvider
12 10
 {
@@ -28,10 +26,11 @@ class AuthServiceProvider extends ServiceProvider
28 26
     {
29 27
         $this->registerPolicies();
30 28
 
31
-        $this->app['auth']->viaRequest('custom', function (Request $request) {
32
-            if ($request->input('token')) {
33
-                return User::where('token', $request->input('token'))->first();
29
+        $this->app['auth']->viaRequest('custom', static function (Request $request) {
30
+            if ($request->input('api_token')) {
31
+                return Token::where('api_token', $request->input('api_token'))->first();
34 32
             }
33
+            return null;
35 34
         });
36 35
     }
37 36
 }

+ 1
- 1
app/Services/Auth/TokenGuard.php View File

@@ -84,7 +84,7 @@ class TokenGuard implements Guard
84 84
      */
85 85
     public function validate(array $credentials = [])
86 86
     {
87
-        if (!array_key_exists('token', $credentials)) {
87
+        if (!array_key_exists('api_token', $credentials)) {
88 88
             return false;
89 89
         }
90 90
 

+ 80
- 1
app/Token.php View File

@@ -2,9 +2,88 @@
2 2
 
3 3
 namespace App;
4 4
 
5
+use Illuminate\Contracts\Auth\Authenticatable;
5 6
 use Illuminate\Database\Eloquent\Model;
7
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
6 8
 
7
-class Token extends Model
9
+class Token extends Model implements Authenticatable
8 10
 {
9 11
     protected $table = 'tokens';
12
+
13
+    public function user(): BelongsTo
14
+    {
15
+        return $this->belongsTo(User::class);
16
+    }
17
+
18
+    /**
19
+     * @return string
20
+     *
21
+     * @throws \Exception
22
+     */
23
+    public static function generateToken()
24
+    {
25
+        return bin2hex(random_bytes(72));
26
+    }
27
+
28
+    /**
29
+     * Get the name of the unique identifier for the user.
30
+     *
31
+     * @return string
32
+     */
33
+    public function getAuthIdentifierName()
34
+    {
35
+        return 'api_token';
36
+    }
37
+
38
+    /**
39
+     * Get the unique identifier for the user.
40
+     *
41
+     * @return mixed
42
+     */
43
+    public function getAuthIdentifier()
44
+    {
45
+        return $this->{$this->getAuthIdentifierName()};
46
+    }
47
+
48
+    /**
49
+     * Get the password for the user.
50
+     *
51
+     * @return string
52
+     */
53
+    public function getAuthPassword()
54
+    {
55
+        return $this->api_token;
56
+    }
57
+
58
+    /**
59
+     * Get the token value for the "remember me" session.
60
+     *
61
+     * @return string
62
+     */
63
+    public function getRememberToken()
64
+    {
65
+        return null;
66
+    }
67
+
68
+    /**
69
+     * Set the token value for the "remember me" session.
70
+     *
71
+     * @param  string $value
72
+     *
73
+     * @return void
74
+     */
75
+    public function setRememberToken($value)
76
+    {
77
+        return null;
78
+    }
79
+
80
+    /**
81
+     * Get the column name for the "remember me" token.
82
+     *
83
+     * @return string
84
+     */
85
+    public function getRememberTokenName()
86
+    {
87
+        return null;
88
+    }
10 89
 }

+ 2
- 76
app/User.php View File

@@ -2,12 +2,10 @@
2 2
 
3 3
 namespace App;
4 4
 
5
-use Illuminate\Contracts\Auth\Authenticatable;
6 5
 use Illuminate\Database\Eloquent\Model;
7 6
 
8
-class User extends Model implements Authenticatable
7
+class User extends Model
9 8
 {
10
-
11 9
     /**
12 10
      * The table associated with the model.
13 11
      *
@@ -22,78 +20,6 @@ class User extends Model implements Authenticatable
22 20
     ];
23 21
 
24 22
     /**
25
-     * @return string
26
-     *
27
-     * @throws \Exception
28
-     */
29
-    public static function generateToken()
30
-    {
31
-        return bin2hex(random_bytes(72));
32
-    }
33
-
34
-    /**
35
-     * Get the name of the unique identifier for the user.
36
-     *
37
-     * @return string
38
-     */
39
-    public function getAuthIdentifierName()
40
-    {
41
-        return 'api_token';
42
-    }
43
-
44
-    /**
45
-     * Get the unique identifier for the user.
46
-     *
47
-     * @return mixed
48
-     */
49
-    public function getAuthIdentifier()
50
-    {
51
-        return $this->{$this->getAuthIdentifierName()};
52
-    }
53
-
54
-    /**
55
-     * Get the password for the user.
56
-     *
57
-     * @return string
58
-     */
59
-    public function getAuthPassword()
60
-    {
61
-        return $this->api_token;
62
-    }
63
-
64
-    /**
65
-     * Get the token value for the "remember me" session.
66
-     *
67
-     * @return string
68
-     */
69
-    public function getRememberToken()
70
-    {
71
-        return null;
72
-    }
73
-
74
-    /**
75
-     * Set the token value for the "remember me" session.
76
-     *
77
-     * @param  string $value
78
-     *
79
-     * @return void
80
-     */
81
-    public function setRememberToken($value)
82
-    {
83
-        return null;
84
-    }
85
-
86
-    /**
87
-     * Get the column name for the "remember me" token.
88
-     *
89
-     * @return string
90
-     */
91
-    public function getRememberTokenName()
92
-    {
93
-        return null;
94
-    }
95
-
96
-    /**
97 23
      * @return Token[]
98 24
      */
99 25
     public function getTokens()
@@ -111,7 +37,7 @@ class User extends Model implements Authenticatable
111 37
         try {
112 38
             $new_token = new Token();
113 39
             $new_token->user_id = $this->id;
114
-            $new_token->api_token = self::generateToken();
40
+            $new_token->api_token = Token::generateToken();
115 41
             $new_token->token_name = $name;
116 42
 
117 43
             $new_token->save();

+ 2
- 2
config/auth.php View File

@@ -15,7 +15,7 @@ return [
15 15
 
16 16
     'defaults' => [
17 17
         'guard' => 'api',
18
-        'passwords' => 'users',
18
+        'passwords' => 'tokens',
19 19
     ],
20 20
 
21 21
     /*
@@ -67,7 +67,7 @@ return [
67 67
     'providers' => [
68 68
         'users' => [
69 69
             'driver' => 'eloquent',
70
-            'model' => App\User::class,
70
+            'model' => App\Token::class,
71 71
         ],
72 72
 
73 73
         // 'users' => [

+ 1
- 1
database/factories/UserFactory.php View File

@@ -5,6 +5,6 @@ use Faker\Generator as Faker;
5 5
 $factory->define(App\User::class, function (Faker $faker) {
6 6
     return [
7 7
         'email' => $faker->unique()->safeEmail,
8
-        'token' => \App\User::generateToken(),
8
+        'token' => \App\Token::generateToken(),
9 9
     ];
10 10
 });

Loading…
Cancel
Save