Browse Source

:sparkles: Ajoute les 2 controllers et l'authentification

Shikiryu 11 months ago
parent
commit
7ebb888bb2

+ 32
- 0
app/Http/Controllers/LinksController.php View File

@@ -0,0 +1,32 @@
1
+<?php
2
+
3
+namespace App\Http\Controllers;
4
+
5
+use App\User;
6
+use Illuminate\Http\Request;
7
+use Illuminate\Validation\ValidationException;
8
+use Laravel\Lumen\Routing\Controller as BaseController;
9
+
10
+class LinksController extends BaseController
11
+{
12
+    public function add(Request $request)
13
+    {
14
+        /** @var User $user */
15
+        $user = $request->user()->first();
16
+        $email = $user->email;
17
+        
18
+        $link = $request->input('link');
19
+        $link = filter_var($link, FILTER_VALIDATE_URL);
20
+        if ($link === false) {
21
+            return response()->json(['error' => 'Invalid link.'], 422);
22
+        }
23
+
24
+        $email_sent = mail($email, 'New link', $link);
25
+
26
+        if ($email_sent) {
27
+            return response()->json(['info' => 'Email sent.']);
28
+        }
29
+
30
+        return response()->json(['error' => 'An error occured while sending email'], 500);
31
+    }
32
+}

+ 33
- 0
app/Http/Controllers/UsersController.php View File

@@ -0,0 +1,33 @@
1
+<?php
2
+
3
+namespace App\Http\Controllers;
4
+
5
+use App\User;
6
+use Illuminate\Http\Request;
7
+use Illuminate\Validation\ValidationException;
8
+use Laravel\Lumen\Routing\Controller as BaseController;
9
+
10
+class UsersController extends BaseController
11
+{
12
+    public function create(Request $request)
13
+    {
14
+        try {
15
+            $data = $this->validate($request, [
16
+                'email' => 'required|email|unique:users'
17
+            ]);
18
+
19
+            $token = bin2hex(random_bytes(72));
20
+
21
+            $user = new User(['email' => $data['email']]);
22
+            $user->token = $token;
23
+            $user->save();
24
+
25
+            return response()->json(['token' => $token, 'email' => $data['email']]);
26
+
27
+        } catch (ValidationException $e) {
28
+            return response()->json(['error' => $e->errors()], 422);
29
+        } catch (\Exception $e) {
30
+            return response()->json(['error' => $e->getMessage()], 422);
31
+        }
32
+    }
33
+}

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

@@ -3,6 +3,7 @@
3 3
 namespace App\Providers;
4 4
 
5 5
 use App\User;
6
+use Illuminate\Http\Request;
6 7
 use Illuminate\Support\Facades\Gate;
7 8
 use Illuminate\Support\ServiceProvider;
8 9
 
@@ -30,9 +31,11 @@ class AuthServiceProvider extends ServiceProvider
30 31
         // should return either a User instance or null. You're free to obtain
31 32
         // the User instance via an API token or any other method necessary.
32 33
 
33
-        $this->app['auth']->viaRequest('api', function ($request) {
34
-            if ($request->input('api_token')) {
35
-                return User::where('api_token', $request->input('api_token'))->first();
34
+        $this->app['auth']->viaRequest('api', function ( Request $request) {
35
+            if ($request->input('token') && $request->input('email')) {
36
+                return User::where('token', $request->input('token'))
37
+                    ->where('email', $request->input('email'))
38
+                    ->first();
36 39
             }
37 40
         });
38 41
     }

+ 12
- 2
app/User.php View File

@@ -1,8 +1,18 @@
1
-<?php namespace App;
1
+<?php
2
+
3
+namespace App;
2 4
 
3 5
 use Illuminate\Database\Eloquent\Model;
4 6
 
5
-class User extends Model {
7
+class User extends Model
8
+{
9
+
10
+    /**
11
+     * The table associated with the model.
12
+     *
13
+     * @var string
14
+     */
15
+    protected $table = 'users';
6 16
 
7 17
     protected $fillable = ["email"];
8 18
     

+ 5
- 5
bootstrap/app.php View File

@@ -20,7 +20,7 @@ try {
20 20
 */
21 21
 
22 22
 $app = new Laravel\Lumen\Application(
23
-    realpath(__DIR__.'/../')
23
+    dirname(__DIR__) . '/'
24 24
 );
25 25
 
26 26
 // $app->withFacades();
@@ -63,9 +63,9 @@ $app->singleton(
63 63
 //    App\Http\Middleware\ExampleMiddleware::class
64 64
 // ]);
65 65
 
66
-// $app->routeMiddleware([
67
-//     'auth' => App\Http\Middleware\Authenticate::class,
68
-// ]);
66
+ $app->routeMiddleware([
67
+     'auth' => App\Http\Middleware\Authenticate::class,
68
+ ]);
69 69
 
70 70
 /*
71 71
 |--------------------------------------------------------------------------
@@ -79,7 +79,7 @@ $app->singleton(
79 79
 */
80 80
 
81 81
 // $app->register(App\Providers\AppServiceProvider::class);
82
-// $app->register(App\Providers\AuthServiceProvider::class);
82
+ $app->register(App\Providers\AuthServiceProvider::class);
83 83
 // $app->register(App\Providers\EventServiceProvider::class);
84 84
 
85 85
 /*

+ 5
- 4
routes/web.php View File

@@ -13,8 +13,9 @@
13 13
 /** @var \Laravel\Lumen\Routing\Router $router */
14 14
 $router->get('/', 'HomeController@home');
15 15
 
16
-$router->post('/register', 'HomeController@iframe');
16
+$router->post('/api/register', 'UsersController@create');
17 17
 
18
-$router->post('/api/{token}', function($token) {
19
-
20
-});
18
+$router->post('/api/links', [
19
+    'middleware' => 'auth',
20
+    'uses' => 'LinksController@add'
21
+]);

Loading…
Cancel
Save