✨ Ajoute les 2 controllers et l'authentification
This commit is contained in:
parent
8c48c8daa3
commit
7ebb888bb2
32
app/Http/Controllers/LinksController.php
Normal file
32
app/Http/Controllers/LinksController.php
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\User;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Validation\ValidationException;
|
||||||
|
use Laravel\Lumen\Routing\Controller as BaseController;
|
||||||
|
|
||||||
|
class LinksController extends BaseController
|
||||||
|
{
|
||||||
|
public function add(Request $request)
|
||||||
|
{
|
||||||
|
/** @var User $user */
|
||||||
|
$user = $request->user()->first();
|
||||||
|
$email = $user->email;
|
||||||
|
|
||||||
|
$link = $request->input('link');
|
||||||
|
$link = filter_var($link, FILTER_VALIDATE_URL);
|
||||||
|
if ($link === false) {
|
||||||
|
return response()->json(['error' => 'Invalid link.'], 422);
|
||||||
|
}
|
||||||
|
|
||||||
|
$email_sent = mail($email, 'New link', $link);
|
||||||
|
|
||||||
|
if ($email_sent) {
|
||||||
|
return response()->json(['info' => 'Email sent.']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json(['error' => 'An error occured while sending email'], 500);
|
||||||
|
}
|
||||||
|
}
|
33
app/Http/Controllers/UsersController.php
Normal file
33
app/Http/Controllers/UsersController.php
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\User;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Validation\ValidationException;
|
||||||
|
use Laravel\Lumen\Routing\Controller as BaseController;
|
||||||
|
|
||||||
|
class UsersController extends BaseController
|
||||||
|
{
|
||||||
|
public function create(Request $request)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$data = $this->validate($request, [
|
||||||
|
'email' => 'required|email|unique:users'
|
||||||
|
]);
|
||||||
|
|
||||||
|
$token = bin2hex(random_bytes(72));
|
||||||
|
|
||||||
|
$user = new User(['email' => $data['email']]);
|
||||||
|
$user->token = $token;
|
||||||
|
$user->save();
|
||||||
|
|
||||||
|
return response()->json(['token' => $token, 'email' => $data['email']]);
|
||||||
|
|
||||||
|
} catch (ValidationException $e) {
|
||||||
|
return response()->json(['error' => $e->errors()], 422);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return response()->json(['error' => $e->getMessage()], 422);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@
|
|||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
use App\User;
|
use App\User;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Gate;
|
use Illuminate\Support\Facades\Gate;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
@ -30,9 +31,11 @@ class AuthServiceProvider extends ServiceProvider
|
|||||||
// should return either a User instance or null. You're free to obtain
|
// should return either a User instance or null. You're free to obtain
|
||||||
// the User instance via an API token or any other method necessary.
|
// the User instance via an API token or any other method necessary.
|
||||||
|
|
||||||
$this->app['auth']->viaRequest('api', function ($request) {
|
$this->app['auth']->viaRequest('api', function ( Request $request) {
|
||||||
if ($request->input('api_token')) {
|
if ($request->input('token') && $request->input('email')) {
|
||||||
return User::where('api_token', $request->input('api_token'))->first();
|
return User::where('token', $request->input('token'))
|
||||||
|
->where('email', $request->input('email'))
|
||||||
|
->first();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
14
app/User.php
14
app/User.php
@ -1,8 +1,18 @@
|
|||||||
<?php namespace App;
|
<?php
|
||||||
|
|
||||||
|
namespace App;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
class User extends Model {
|
class User extends Model
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The table associated with the model.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $table = 'users';
|
||||||
|
|
||||||
protected $fillable = ["email"];
|
protected $fillable = ["email"];
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ try {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
$app = new Laravel\Lumen\Application(
|
$app = new Laravel\Lumen\Application(
|
||||||
realpath(__DIR__.'/../')
|
dirname(__DIR__) . '/'
|
||||||
);
|
);
|
||||||
|
|
||||||
// $app->withFacades();
|
// $app->withFacades();
|
||||||
@ -63,9 +63,9 @@ $app->singleton(
|
|||||||
// App\Http\Middleware\ExampleMiddleware::class
|
// App\Http\Middleware\ExampleMiddleware::class
|
||||||
// ]);
|
// ]);
|
||||||
|
|
||||||
// $app->routeMiddleware([
|
$app->routeMiddleware([
|
||||||
// 'auth' => App\Http\Middleware\Authenticate::class,
|
'auth' => App\Http\Middleware\Authenticate::class,
|
||||||
// ]);
|
]);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
@ -79,7 +79,7 @@ $app->singleton(
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// $app->register(App\Providers\AppServiceProvider::class);
|
// $app->register(App\Providers\AppServiceProvider::class);
|
||||||
// $app->register(App\Providers\AuthServiceProvider::class);
|
$app->register(App\Providers\AuthServiceProvider::class);
|
||||||
// $app->register(App\Providers\EventServiceProvider::class);
|
// $app->register(App\Providers\EventServiceProvider::class);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -13,8 +13,9 @@
|
|||||||
/** @var \Laravel\Lumen\Routing\Router $router */
|
/** @var \Laravel\Lumen\Routing\Router $router */
|
||||||
$router->get('/', 'HomeController@home');
|
$router->get('/', 'HomeController@home');
|
||||||
|
|
||||||
$router->post('/register', 'HomeController@iframe');
|
$router->post('/api/register', 'UsersController@create');
|
||||||
|
|
||||||
$router->post('/api/{token}', function($token) {
|
$router->post('/api/links', [
|
||||||
|
'middleware' => 'auth',
|
||||||
});
|
'uses' => 'LinksController@add'
|
||||||
|
]);
|
Loading…
Reference in New Issue
Block a user