Ajoute les 2 controllers et l'authentification

This commit is contained in:
Shikiryu 2018-04-26 23:02:06 +02:00
parent 8c48c8daa3
commit 7ebb888bb2
6 changed files with 93 additions and 14 deletions

View 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);
}
}

View 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);
}
}
}

View File

@ -3,6 +3,7 @@
namespace App\Providers;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
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
// the User instance via an API token or any other method necessary.
$this->app['auth']->viaRequest('api', function ($request) {
if ($request->input('api_token')) {
return User::where('api_token', $request->input('api_token'))->first();
$this->app['auth']->viaRequest('api', function ( Request $request) {
if ($request->input('token') && $request->input('email')) {
return User::where('token', $request->input('token'))
->where('email', $request->input('email'))
->first();
}
});
}

View File

@ -1,8 +1,18 @@
<?php namespace App;
<?php
namespace App;
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"];

View File

@ -20,7 +20,7 @@ try {
*/
$app = new Laravel\Lumen\Application(
realpath(__DIR__.'/../')
dirname(__DIR__) . '/'
);
// $app->withFacades();
@ -63,9 +63,9 @@ $app->singleton(
// App\Http\Middleware\ExampleMiddleware::class
// ]);
// $app->routeMiddleware([
// 'auth' => App\Http\Middleware\Authenticate::class,
// ]);
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
]);
/*
|--------------------------------------------------------------------------
@ -79,7 +79,7 @@ $app->singleton(
*/
// $app->register(App\Providers\AppServiceProvider::class);
// $app->register(App\Providers\AuthServiceProvider::class);
$app->register(App\Providers\AuthServiceProvider::class);
// $app->register(App\Providers\EventServiceProvider::class);
/*

View File

@ -13,8 +13,9 @@
/** @var \Laravel\Lumen\Routing\Router $router */
$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'
]);